Compare commits

...

1 Commits

4 changed files with 52 additions and 40 deletions
+15 -11
View File
@@ -20,19 +20,19 @@ int main()
fread(grey, sizeof(unsigned char), H * W, f); fread(grey, sizeof(unsigned char), H * W, f);
fclose(f); fclose(f);
// 模板 // prewitt模板
int tw = 3; int tw = 3;
int th = 3; int th = 3;
int template1[9]={-1,0,1,-1,0,1,-1,0,1}; 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 template2[9]={-1,-1,-1,0,0,0,1,1,1};
int i,j,k,l; int i,j,k,l; // 中间变量
unsigned char *result_grey; unsigned char *result;
result_grey = new unsigned char[H * W]; result = new unsigned char[H * W];
for (i = 0; i < H * W; i++) for (i = 0; i < H * W; i++)
{ {
result_grey[i] = 255; result[i] = 255;
} }
for (i = th / 2; i < H - th / 2; i++) for (i = th / 2; i < H - th / 2; i++)
{ {
@@ -53,23 +53,27 @@ int main()
sum=abs(sum1)+abs(sum2); sum=abs(sum1)+abs(sum2);
if(sum>100) if(sum>100)
{ {
result_grey[i * W + j] = 0; result[i * W + j] = 0;
} }
else 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 else
{ {
cout << "创建文件失败" << endl; cout << "创建文件失败" << endl;
return 0; return 0;
} }
return 0;
} }
+22 -18
View File
@@ -12,7 +12,7 @@ int main()
unsigned char *grey; unsigned char *grey;
grey = new unsigned char[H * W]; grey = new unsigned char[H * W];
FILE *f; FILE *f;
fopen_s(&f, "./数据/blood.raw", "rb"); fopen_s(&f, "./数据/blood.raw", "rb");
if (f) if (f)
{ {
fread(grey, sizeof(unsigned char), H * W, f); fread(grey, sizeof(unsigned char), H * W, f);
@@ -20,30 +20,28 @@ int main()
} }
else else
{ {
cout << "无法打开文件" << endl; cout << "无法打开文件" << endl;
return 0; return 0;
} }
fread(grey, sizeof(unsigned char), H * W, f);
fclose(f);
// 模板 // 拉普拉斯模板
int tw = 3; int tw = 3;
int th = 3; int th = 3;
int template1[9]={0,1,0,1,-4,1,0,1,0}; int template1[9]={0,1,0,1,-4,1,0,1,0};
int i,j,k,l; int i,j,k,l; // 中间变量
unsigned char *result_grey; unsigned char *result;
result_grey = new unsigned char[H * W]; result = new unsigned char[H * W];
for (i = 0; i < H * W; i++) for (i = 0; i < H * W; i++)
{ {
result_grey[i] = 255; result[i] = 255;
} }
for (i = th / 2; i < H - th / 2; i++) for (i = th / 2; i < H - th / 2; i++)
{ {
for (j = tw / 2; j < W - tw / 2; j++) for (j = tw / 2; j < W - tw / 2; j++)
{ {
// 应用拉普拉斯模板 // 应用拉普拉斯模板
int sum = 0; int sum = 0;
for (k = -th / 2; k <= th / 2; k++) 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)]; 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 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 else
{ {
cout << "创建文件失败" << endl; cout << "创建文件失败" << endl;
return 0; return 0;
} }
return 0;
} }
+15 -11
View File
@@ -24,18 +24,20 @@ int main()
return 0; return 0;
} }
// sobel模板
int tw = 3; int tw = 3;
int th = 3; int th = 3;
// xy方向的sobel模板
int sobel_x[9]={-1,0,1,-2,0,2,-1,0,1}; 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 sobel_y[9]={-1,-2,-1,0,0,0,1,2,1};
int i,j,k,l; int i,j,k,l; // 中间变量
unsigned char *result_grey; unsigned char *result;
result_grey = new unsigned char[H * W]; result = new unsigned char[H * W];
for (i = 0; i < H * W; i++) for (i = 0; i < H * W; i++)
{ {
result_grey[i] = 255; result[i] = 255;
} }
for (i = th / 2; i < H - th / 2; i++) for (i = th / 2; i < H - th / 2; i++)
{ {
@@ -56,20 +58,22 @@ int main()
int sum = (int)sqrt(gx * gx + gy * gy); int sum = (int)sqrt(gx * gx + gy * gy);
if (sum > 100) if (sum > 100)
{ {
result_grey[i * W + j] = 0; result[i * W + j] = 0;
} }
else 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); fwrite(result, sizeof(unsigned char), H * W, fresult);
fclose(fnew); fclose(fresult);
} }
else else
{ {
Binary file not shown.