c语言实现图像锐化代码
时间: 2023-11-10 16:06:52 浏览: 109
18.如何实现图像的锐化效果?(Visual C++编程 源代码)
下面是一个简单的C语言图像锐化代码,使用的是Sobel算子:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define WIDTH 512
#define HEIGHT 512
void sobel(int image[][HEIGHT], int outimage[][HEIGHT])
{
int x, y, i, j;
int gx, gy, sum;
int maskx[3][3] = {{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}};
int masky[3][3] = {{-1, -2, -1},
{0, 0, 0},
{1, 2, 1}};
for (x = 1; x < WIDTH - 1; x++)
{
for (y = 1; y < HEIGHT - 1; y++)
{
gx = gy = 0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
gx += maskx[i + 1][j + 1] * image[x + i][y + j];
gy += masky[i + 1][j + 1] * image[x + i][y + j];
}
}
sum = abs(gx) + abs(gy);
if (sum > 255)
sum = 255;
outimage[x][y] = sum;
}
}
}
int main(int argc, char *argv[])
{
FILE *fp_in, *fp_out;
char *filename_in = "lena.raw";
char *filename_out = "lena_sharp.raw";
int image[WIDTH][HEIGHT], outimage[WIDTH][HEIGHT];
int x, y;
// 读取图像数据
fp_in = fopen(filename_in, "rb");
fread(image, sizeof(unsigned char), WIDTH * HEIGHT, fp_in);
fclose(fp_in);
// 图像锐化
sobel(image, outimage);
// 写入图像数据
fp_out = fopen(filename_out, "wb");
fwrite(outimage, sizeof(unsigned char), WIDTH * HEIGHT, fp_out);
fclose(fp_out);
return 0;
}
阅读全文