diff --git a/lab-3/lab3-1.cpp b/lab-3/lab3-1.cpp index 6c70f58..521d328 100644 --- a/lab-3/lab3-1.cpp +++ b/lab-3/lab3-1.cpp @@ -20,19 +20,19 @@ int main() fread(grey, sizeof(unsigned char), H * W, f); fclose(f); - // 模板 + // prewitt模板 int tw = 3; int th = 3; int template1[9]={-1,0,1,-1,0,1,-1,0,1}; int template2[9]={-1,-1,-1,0,0,0,1,1,1}; - int i,j,k,l; - unsigned char *result_grey; - result_grey = new unsigned char[H * W]; + int i,j,k,l; // 中间变量 + unsigned char *result; + result = new unsigned char[H * W]; for (i = 0; i < H * W; i++) { - result_grey[i] = 255; + result[i] = 255; } for (i = th / 2; i < H - th / 2; i++) { @@ -53,23 +53,27 @@ int main() sum=abs(sum1)+abs(sum2); if(sum>100) { - result_grey[i * W + j] = 0; + result[i * W + j] = 0; } else { - result_grey[i * W + j] = 255; + result[i * W + j] = 255; } } } - FILE *fnew; - fopen_s(&fnew, "./数据/prewitt.raw", "wb"); - if (fnew) + + // 将结果写入文件 + FILE *fresult; + fopen_s(&fresult, "./数据/prewitt.raw", "wb"); + if (fresult) { - fwrite(result_grey, sizeof(unsigned char), H * W, fnew); + fwrite(result, sizeof(unsigned char), H * W, fresult); + fclose(fresult); } else { cout << "创建文件失败" << endl; return 0; } + return 0; } \ No newline at end of file diff --git a/lab-3/lab3-2.cpp b/lab-3/lab3-2.cpp index 8c6c615..b577f32 100644 --- a/lab-3/lab3-2.cpp +++ b/lab-3/lab3-2.cpp @@ -12,7 +12,7 @@ int main() unsigned char *grey; grey = new unsigned char[H * W]; FILE *f; - fopen_s(&f, "./数据/blood.raw", "rb"); + fopen_s(&f, ".//blood.raw", "rb"); if (f) { fread(grey, sizeof(unsigned char), H * W, f); @@ -20,30 +20,28 @@ int main() } else { - cout << "无法打开文件" << endl; + cout << "޷ļ" << endl; return 0; } - fread(grey, sizeof(unsigned char), H * W, f); - fclose(f); - // 模板 + // ˹ģ int tw = 3; int th = 3; int template1[9]={0,1,0,1,-4,1,0,1,0}; - int i,j,k,l; - unsigned char *result_grey; - result_grey = new unsigned char[H * W]; + int i,j,k,l; // м + unsigned char *result; + result = new unsigned char[H * W]; for (i = 0; i < H * W; i++) { - result_grey[i] = 255; + result[i] = 255; } for (i = th / 2; i < H - th / 2; i++) { for (j = tw / 2; j < W - tw / 2; j++) { - // 应用拉普拉斯模板 + // Ӧ˹ģ int sum = 0; for (k = -th / 2; k <= th / 2; k++) { @@ -52,25 +50,31 @@ int main() sum += grey[(i + k) * W + (j + l)] * template1[(k + th / 2) * tw + (l + tw / 2)]; } } - if (sum > 100) + sum = abs(sum); + if (sum < 1) { - result_grey[i * W + j] = 0; + result[i * W + j] = 0; } else { - result_grey[i * W + j] = 255; + result[i * W + j] = 255; } } } - FILE *fnew; - fopen_s(&fnew, "./数据/laplacian.raw", "wb"); - if (fnew) + + // дļ + FILE *fresult; + fopen_s(&fresult, ".//laplacian.raw", "wb"); + if (fresult) { - fwrite(result_grey, sizeof(unsigned char), H * W, fnew); + fwrite(result, sizeof(unsigned char), H * W, fresult); + fclose(fresult); } else { - cout << "创建文件失败" << endl; + cout << "ļʧ" << endl; return 0; } + + return 0; } \ No newline at end of file diff --git a/lab-3/lab3-3.cpp b/lab-3/lab3-3.cpp index 1ef82d5..2e71db7 100644 --- a/lab-3/lab3-3.cpp +++ b/lab-3/lab3-3.cpp @@ -24,18 +24,20 @@ int main() return 0; } + // sobel模板 int tw = 3; int th = 3; + // xy方向的sobel模板 int sobel_x[9]={-1,0,1,-2,0,2,-1,0,1}; int sobel_y[9]={-1,-2,-1,0,0,0,1,2,1}; - int i,j,k,l; - unsigned char *result_grey; - result_grey = new unsigned char[H * W]; + int i,j,k,l; // 中间变量 + unsigned char *result; + result = new unsigned char[H * W]; for (i = 0; i < H * W; i++) { - result_grey[i] = 255; + result[i] = 255; } for (i = th / 2; i < H - th / 2; i++) { @@ -56,20 +58,22 @@ int main() int sum = (int)sqrt(gx * gx + gy * gy); if (sum > 100) { - result_grey[i * W + j] = 0; + result[i * W + j] = 0; } else { - result_grey[i * W + j] = 255; + result[i * W + j] = 255; } } } - FILE *fnew; - fopen_s(&fnew, "./数据/sobel.raw", "wb"); - if (fnew) + + // 将结果写入文件 + FILE *fresult; + fopen_s(&fresult, "./数据/sobel.raw", "wb"); + if (fresult) { - fwrite(result_grey, sizeof(unsigned char), H * W, fnew); - fclose(fnew); + fwrite(result, sizeof(unsigned char), H * W, fresult); + fclose(fresult); } else { diff --git a/lab-3/数据/laplacian.raw b/lab-3/数据/laplacian.raw index b77936c..5a78f47 100644 Binary files a/lab-3/数据/laplacian.raw and b/lab-3/数据/laplacian.raw differ