简体   繁体   English

模糊图像滤镜-C

[英]Blur Image Filter - C

I've posted this program several other times, but am different troubles with it. 我已经几次发布了该程序,但是麻烦不多。 I want to write an algorithm to blur an image, and so far have gotten every pixel value of a grayscale image into a **char array. 我想编写一种算法来模糊图像,到目前为止,已经将灰度图像的每个像素值放入**char数组中。 From there, my plan is to go through each pixel, look to its immediate neighboring pixels, sum the values of them AND the pixel I am currently looking at, and get the average of all of those pixels. 从那里开始,我的计划是遍历每个像素,查看其紧邻的像素,将它们的值与我当前正在查看的像素相加,并获得所有这些像素的平均值。

After that is done, I want to set the pixel I'm currently looking at to that average value. 完成之后,我要将当前正在查看的像素设置为该平均值。 I've written partial code for this, and I believe I'm adding it up correctly, but still do not get a resulting image. 我已经为此编写了部分代码,并且我相信我将其正确添加了,但仍然没有得到结果图像。 Must I provide a cast to my sum before assigning it back to the pixel? 在将总和分配回像素之前,是否必须对总和提供转换?

Code: 码:

#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;
}

Note : this code only works with .raw grayscale images. 注意 :此代码仅适用于.raw灰度图像。

EDIT : I've implemented blurring to the rest of the image, but am still not getting a readable output file. 编辑 :我已经实现了对图像其余部分的模糊处理,但是仍然无法获得可读的输出文件。 I'm not sure if my arithmetic is correct, as I'm not too familiar with adding bytes together. 我不确定我的算术方法是否正确,因为我不太喜欢将字节加在一起。

This part 这部分

rev = (char **)malloc(height * sizeof(char *));
for(i = 0; i < width; i++)
    rev[i] = (char *)malloc(width * sizeof(char));

I think you meant 我想你是说

for(i = 0; i < height; i++)

Other bugs: 其他错误:

  • you probably want to use unsigned char as the data type 您可能要使用unsigned char作为数据类型
  • don't use sum += , use sum = 不要使用sum += ,请使用sum =
  • you're only doing the blur calculation for the first row of the image 您只对图像的第一行进行模糊计算
  • you might want to convert each input used in the calculation to unsigned , otherwise the values will overflow 您可能需要将计算中使用的每个输入转换为unsigned ,否则值将溢出

try: 尝试:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM