我已经多次发布这个程序,但是它有不同的麻烦。我想写一个算法来模糊图像,到目前为止,我已经将灰度图像的每个像素值都变成**char
数组。从那里,我的计划是遍历每个像素,查看其直接相邻像素,将它们的值与我当前正在查看的像素相加,并获得所有这些像素的平均值。
完成后,我想将我正在查看的像素设置为该平均值。我为此编写了部分代码,我相信我正确地添加了它,但仍然没有得到结果图像。在将其分配给像素之前,我必须为我的总和提供演员表吗?
代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fin, *fout;
char path_in[64], path_out[64];
unsigned char **rev, px;
int sum, width, height, read, row, i, j;
printf("Input file name: ");
scanf("%s", path_in);
printf("Output file name: ");
scanf("%s", path_out);
printf("Width of image (in pixels): ");
scanf("%d", &width);
printf("Height of image (in pixels): ");
scanf("%d", &height);
fin = fopen(path_in, "rb");
fout = fopen(path_out, "wb");
rev = (unsigned char **)malloc(height * sizeof(unsigned char *));
for(i = 0; i < height; i++)
rev[i] = (unsigned char *)malloc(width * sizeof(unsigned char));
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
read = fread(&px, sizeof(char), 1, fin);
rev[i][j] = px;
}
}
sum = 0;
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
//Top row of image
if(i == 0)
{
if(j == 0)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j + 1] +
(unsigned)rev[i + 1][j] + (unsigned)rev[i + 1][j + 1]) / 4;
else if(j == width - 1)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] +
(unsigned)rev[i + 1][j] + (unsigned)rev[i + 1][j - 1]) / 4;
else
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] + (unsigned)rev[i][j + 1] +
(unsigned)rev [i + 1][j] + (unsigned)rev[i + 1][j - 1] + (unsigned)rev[i + 1][j + 1]) / 6;
}
//Bottom row of image
else if(i == height - 1)
{
if(j == 0)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j + 1] +
(unsigned)rev[i - 1][j] + (unsigned)rev[i - 1][j + 1]) / 4;
else if(j == width - 1)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] +
(unsigned)rev[i - 1][j] + (unsigned)rev[i - 1][j - 1]) / 4;
else
sum = ((unsigned)rev[i][j] + (unsigned)rev[i][j - 1] + (unsigned)rev[i][j + 1] +
(unsigned)rev[i - 1][j] + (unsigned)rev[i - 1][j - 1] + (unsigned)rev[i - 1][j + 1]) / 6;
}
//Left side of image (excluding top or bottom row)
else if(j == 0)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i - 1][j] + (unsigned)rev[i + 1][j] +
(unsigned)rev[i][j + 1] + (unsigned)rev[i - 1][j + 1] + (unsigned)rev[i + 1][j + 1]) / 6;
//Right side of image (excluding top or bottom row)
else if(j == width - 1)
sum = ((unsigned)rev[i][j] + (unsigned)rev[i - 1][j] + (unsigned)rev[i + 1][j] +
(unsigned)rev[i][j - 1] + (unsigned)rev[i - 1][j - 1] + (unsigned)rev[i + 1][j - 1]) / 6;
rev[i][j] = (unsigned char)sum;
}
}
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
if(j < width && i < height)
fwrite(&rev[i][j], sizeof(char), 1, fout);
}
}
fclose(fout);
fclose(fin);
return 0;
}
注意:此代码仅适用于.raw
灰度图像。
编辑:我已经对图像的其余部分进行了模糊处理,但仍然没有获得可读的输出文件。我不确定我的算法是否正确,因为我不太熟悉将字节加在一起。
答案 0 :(得分:1)
这部分
rev = (char **)malloc(height * sizeof(char *));
for(i = 0; i < width; i++)
rev[i] = (char *)malloc(width * sizeof(char));
我认为你的意思是
for(i = 0; i < height; i++)
其他错误:
unsigned char
作为数据类型sum +=
,请使用sum =
unsigned
,否则值将溢出尝试:
for(i = 0; i < (height - 1); i++)
{
for(j = 0; j < (width - 1); j++)
{
sum = ((unsigned) rev[i][j] + (unsigned) rev[i][j + 1] +
(unsigned) rev[i + 1][j] + (unsigned) rev[i + 1][j + 1]) / 4;
rev[i][j] = (unsigned char) sum;
}
}