用 C 模糊图像

Jas*_*ter 3 c image

我正在尝试根据我正在遵循的教程来模糊图像。这是说明。

\n
\n

模糊 有多种方法可以创建模糊或软化图像的效果。对于这个问题,我们\xe2\x80\x99将使用\xe2\x80\x9cbox模糊,\xe2\x80\x9d的工作原理是获取每个像素,并针对每个颜色值,通过平均给它一个新的值。相邻像素的颜色值。

\n

考虑以下像素网格,其中我们\xe2\x80\x99\n对每个像素进行了编号。

\n

像素网格

\n

每个像素的新值将是原始像素 1 行和列内的所有像素值的平均值(形成一个 3x3 框)。例如,像素 6\n的每个颜色值将通过对像素 1、\n2、3、5、6、7、9、10 和 11 的原始颜色值进行平均来获得(请注意,像素 6 本身也包括在内)平均)。同样,像素 11 的颜色值可以通过对像素 6、7、8、10、11、12、14、15 和 16 的颜色值进行平均来获得。

\n
\n
\n

在此输入图像描述

\n
\n
\n

对于沿着边缘或角落的像素,例如像素 15,我们仍然\n查找 1 行和列内的所有像素:在本例中,为像素 10、\n11、12、14、15 和 16。```

\n
\n

因此,为了解决这个问题,我想出了这个解决方案。

\n
for each row\n  for each column\n     add the pixel if upper left exists\n     add the pixel if directly above exists\n     add the pixel if upper right exists\n     add the pixel if right exists\n     add the pixel if directly below exists\n     add the pixel if left exists\ndivide by times added\n
Run Code Online (Sandbox Code Playgroud)\n

我已经输入了代码 -

\n
void blur(int height, int width, RGBTRIPLE image[height][width])\n{\n    int red;\n    int green;\n    int blue;\n    int counter = 0;\n\n    for (int i = 0; i < height; i++)\n    {\n        for (int j = 0; j < width; j++)\n        {\n            if (i + 1 && j - 1)\n            {\n                red = image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed;\n                green = image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen;\n                blue = image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue;\n                counter++;\n            }\n            if (j + 1)\n            {\n                red = image[i][j].rgbtRed + image[i][j + 1].rgbtRed;\n                green = image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen;\n                blue = image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue;\n                counter++;\n            }\n            if (i + 1 && j + 1)\n            {\n                red = image[i][j].rgbtRed + image[i + 1][j + 1].rgbtRed;\n                green = image[i][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;\n                blue = image[i][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;\n                counter++;\n            }\n            if (i + 1)\n            {\n                red = image[i][j].rgbtRed + image[i + 1][j].rgbtRed;\n                green = image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen;\n                blue = image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue;\n                counter++;\n            }\n            if (j - 1)\n            {\n                red = image[i][j].rgbtRed + image[i][j - 1].rgbtRed;\n                green = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen;\n                blue = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue;\n                counter++;\n            }\n            if (i - 1)\n            {\n                red = image[i][j].rgbtRed + image[i - 1][j].rgbtRed;\n                green = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen;\n                blue = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue;\n                counter++;\n            }\n            image[i][j].rgbtRed = red/counter;\n            image[i][j].rgbtGreen = green/counter;\n            image[i][j].rgbtBlue = blue/counter;\n        }\n    }\n    return;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

似乎我收到错误消息

\n
~/pset4/filter/ $ ./filter -b images/courtyard.bmp  test.bmp                                                                                                                                                        \nhelpers.c:84:45: runtime error: index -1 out of bounds for type \'RGBTRIPLE [width]\'\nhelpers.c:85:49: runtime error: index -1 out of bounds for type \'RGBTRIPLE [width]\'\nhelpers.c:86:47: runtime error: index -1 out of bounds for type \'RGBTRIPLE [width]\'\nhelpers.c:112:45: runtime error: index -1 out of bounds for type \'RGBTRIPLE [width]\'\nhelpers.c:113:49: runtime error: index -1 out of bounds for type \'RGBTRIPLE [width]\'\nhelpers.c:114:47: runtime error: index -1 out of bounds for type \'RGBTRIPLE [width]\'\nUndefinedBehaviorSanitizer:DEADLYSIGNAL\n==4657==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fd1249a490a (pc 0x00000042b7ea bp 0x7ffd589ca4f0 sp 0x7ffd589c90f0 T4657)\n==4657==The signal is caused by a READ memory access.\n    #0 0x42b7e9  (/home/ubuntu/pset4/filter/filter+0x42b7e9)\n    #1 0x422ed2  (/home/ubuntu/pset4/filter/filter+0x422ed2)\n    #2 0x7fd123897b96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)\n    #3 0x402d69  (/home/ubuntu/pset4/filter/filter+0x402d69)\n
Run Code Online (Sandbox Code Playgroud)\n

对我来说,代码似乎并没有真正解决问题。它在第一次迭代后就失败了。因为ij值变为负数,因此并不能真正达到检查附近像素是否存在的目的。

\n

您能否分享一两点关于这里应该做什么,以更好地了解程序应如何检测周围像素?

\n

Ven*_*ama 6

在C中,非零被认为是真,j-1在j=0时是-1真。

void blur(int height, int width, RGBTRIPLE image[height][width])

{

 int red;

int green;
int blue;
int counter = 0;
RGBTRIPLE **new_image = (RGBTRIPLE**) malloc(sizeof(RGBTRIPLE*)*height);

  for(int i=0;i<height;i++)
     new_image[i]=((RGBTRIPLE*) malloc(sizeof(RGBTRIPLE)*width);

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
          red=green=blue=0;
          counter=0;
        if (i + 1 <height && j - 1 >=0)
        {
            red += image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue;
            counter++;
        }
        if (j + 1<width)
        {
            red += image[i][j].rgbtRed + image[i][j + 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue;
            counter++;
        }
        if (i + 1 <height && j + 1 <width)
        {
            red += image[i][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
            counter++;
        }
        if (i + 1<height)
        {
            red += image[i][j].rgbtRed + image[i + 1][j].rgbtRed;
            green += image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue;
            counter++;
        }
        if (j - 1>=0)
        {
            red += image[i][j].rgbtRed + image[i][j - 1].rgbtRed;
            green += image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue;
            counter++;
        }
        if (i - 1>=0)
        {
            red += image[i][j].rgbtRed + image[i - 1][j].rgbtRed;
            green += image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen;
            blue += image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue;
            counter++;
        }
        new_image[i][j].rgbtRed = red/counter;
        new_image[i][j].rgbtGreen = green/counter;
        new_image[i][j].rgbtBlue = blue/counter;
    }
}
return;
}
Run Code Online (Sandbox Code Playgroud)