c - 在c中使用高斯模糊模糊图像
问题描述
所以我看到很多人使用其他编程语言做高斯模糊,所以我试图用 C 做,但似乎我的代码不起作用。所以起初我要求用户输入图像文件,然后是它的宽度和高度。输入文件名的宽度和高度后,输出文件似乎是二进制文件,因此它是不受支持的文件。
#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, 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));
//Store pixel values from image in a width x height unsigned char matrix
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
read = fread(&px, sizeof(unsigned char), 1, fin);
rev[i][j] = px;
}
}
//Blur image using average of neighboring pixels
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 = (rev[i][j] + rev[i][j + 1] +
rev[i + 1][j] + rev[i + 1][j + 1]) / 4;
else if(j == width - 1)
sum = (rev[i][j] + rev[i][j - 1] +
rev[i + 1][j] + rev[i + 1][j - 1]) / 4;
else
sum = (rev[i][j] + rev[i][j - 1] + rev[i][j + 1] +
rev[i + 1][j] + rev[i + 1][j - 1] + rev[i + 1][j + 1]) / 6;
}
//Bottom row of image
else if(i == height - 1)
{
if(j == 0)
sum = (rev[i][j] + rev[i][j + 1] +
rev[i - 1][j] + rev[i - 1][j + 1]) / 4;
else if(j == width - 1)
sum = (rev[i][j] + rev[i][j - 1] +
rev[i - 1][j] + rev[i - 1][j - 1]) / 4;
else
sum = (rev[i][j] + rev[i][j - 1] + rev[i][j + 1] +
rev[i - 1][j] + rev[i - 1][j - 1] + rev[i - 1][j + 1]) / 6;
}
//Left side of image (excluding top or bottom row)
else if(j == 0)
sum = (rev[i][j] + rev[i - 1][j] + rev[i + 1][j] +
rev[i][j + 1] + rev[i - 1][j + 1] + rev[i + 1][j + 1]) / 6;
//Right side of image (excluding top or bottom row)
else if(j == width - 1)
sum = (rev[i][j] + rev[i - 1][j] + rev[i + 1][j] +
rev[i][j - 1] + rev[i - 1][j - 1] + rev[i + 1][j - 1]) / 6;
//Pixels not on a border of the image
else
sum = (rev[i][j] + rev[i][j - 1] + rev[i][j + 1] + rev[i - 1][j] + rev[i + 1][j] +
rev[i - 1][j - 1] + rev[i - 1][j + 1] + rev[i + 1][j - 1] + rev[i + 1][j + 1]) / 9;
rev[i][j] = (unsigned char)sum;
}
}
//Write each pixel in rev to output file
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
if(j < width && i < height)
fwrite(&rev[i][j], sizeof(unsigned char), 1, fout);
}
}
//Close input and output file
fclose(fout);
fclose(fin);
return 0;
}
解决方案
推荐阅读
- python - 按列计算两个 DataFrame 的相关性
- nginx - CentOS 7 - NGINX - DNS 负载均衡
- google-cloud-storage - 在 Google Cloud Storage 的子文件夹中托管静态 Sapper 站点
- r - 在 drake 中使用 code_to_plan 和 target(..., format = "fst")
- excel - 将活动行从一个表剪切并粘贴到不同工作表上的另一个表
- python - 需要有关用户输入的蛇程序帮助
- amazon-web-services - 有没有办法使用 Lambda 函数在 Amazon EFS 上存储文件?
- angular - Angular2 jwt auth 通过子模块中的拦截器
- sql - 网站分析数据的存储 - 关系还是时间序列?
- mysql - .on("ready") 用mysql定义公会ID