首页 > 解决方案 > 在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;
}

标签: cgaussianblur

解决方案


推荐阅读