写一个C语言,拉普拉斯算子锐化图像的代码
时间: 2023-05-11 18:06:15 浏览: 125
基于OpenCV的图像锐化 C代码
以下是拉普拉斯算子锐化图像的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define WIDTH 512
#define HEIGHT 512
int main()
{
FILE *fp_in, *fp_out;
unsigned char image[HEIGHT][WIDTH];
unsigned char new_image[HEIGHT][WIDTH];
int i, j, k, l;
int mask[3][3] = {{-1, -1, -1}, {-1, 8, -1}, {-1, -1, -1}};
fp_in = fopen("lena512.raw", "rb");
if (!fp_in) {
printf("Cannot open file.\n");
exit(1);
}
fread(image, sizeof(unsigned char), WIDTH * HEIGHT, fp_in);
fclose(fp_in);
for (i = 1; i < HEIGHT - 1; i++) {
for (j = 1; j < WIDTH - 1; j++) {
int sum = 0;
for (k = -1; k <= 1; k++) {
for (l = -1; l <= 1; l++) {
sum += image[i + k][j + l] * mask[k + 1][l + 1];
}
}
if (sum < 0) {
sum = 0;
} else if (sum > 255) {
sum = 255;
}
new_image[i][j] = (unsigned char)sum;
}
}
fp_out = fopen("lena512_laplacian.raw", "wb");
if (!fp_out) {
printf("Cannot open file.\n");
exit(1);
}
fwrite(new_image, sizeof(unsigned char), WIDTH * HEIGHT, fp_out);
fclose(fp_out);
return 0;
}
```
这个程序读取名为"lena512.raw"的灰度图像文件,然后对其进行拉普拉斯算子锐化处理,并将结果保存为"lena512_laplacian.raw"文件。
阅读全文