使用C模糊图像

0tdrvxhp  于 2023-01-25  发布在  其他
关注(0)|答案(6)|浏览(148)

我正在尝试模糊一个图像的基础上,我的教程如下。这里的指示。
模糊有很多种方法可以创建模糊或柔化图像的效果。对于这个问题,我们将使用“框模糊”,它的工作原理是取每个像素,并为每个颜色值,通过平均相邻像素的颜色值来赋予它一个新的值。
考虑下面的像素网格,其中我们对每个像素进行了编号。
像素网格
每个像素的新值将是原始像素的1行和1列内的所有像素的值的平均值例如,像素6的每个颜色值将通过平均像素1、2、3、5、6、7、9、10的原始颜色值来获得,类似地,像素11的颜色值将通过对像素6、7、8、10、11、12、14、15和16的颜色值求平均来获得。

对于沿着边缘或角的像素,如像素15,我们仍将寻找1行和1列内的所有像素:在这种情况下,像素10、11、12、14、15和16。
为了解决这个问题,我想出了这个办法。

for each row
  for each column
     add the pixel if upper left exists
     add the pixel if directly above exists
     add the pixel if upper right exists
     add the pixel if right exists
     add the pixel if directly below exists
     add the pixel if left exists
divide by times added

我已经输入了代码-

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int red;
    int green;
    int blue;
    int counter = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (i + 1 && j - 1)
            {
                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)
            {
                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 && j + 1)
            {
                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)
            {
                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)
            {
                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)
            {
                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++;
            }
            image[i][j].rgbtRed = red/counter;
            image[i][j].rgbtGreen = green/counter;
            image[i][j].rgbtBlue = blue/counter;
        }
    }
    return;
}

好像我得到了错误消息

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

对我来说,这段代码似乎并没有真正解决这个问题。它在第一次迭代时就失败了。因为ij的值变成了负值,因此并没有真正达到检查附近像素存在的目的。
你能分享一两点关于这里应该做些什么来更好地发现程序应该如何检测周围的像素吗?

goqiplq2

goqiplq21#

在C中,非零视为真,j=0处的j-1为-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;
}
z6psavjg

z6psavjg2#

这是一个更好的方法,有可能增加模糊框到其他维度形成一个3x3框,或其他。

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    double average_red = 0, average_green = 0, average_blue = 0;
    int count_number = 0;
    int box = 1; // set 1 to have a 3x3 box

    //scanning the matriz
    for (int i = 0; i<height; ++i)
    {
        for(int j = 0; j<width;++j)
        {
            //cleaning the averages and the count number

            average_red = 0;
            average_blue = 0;
            average_red = 0;
            count_number = 0;

            //forming a 3x3 box
            for (int li = i-box; li<=i+box; ++li )
            {
                if( li < 0 )
                {
                    li = 0;
                }
                if( li == height)
                {
                    break;
                }

                for(int col = j-box; col<= j+box; col++)
                {

                    if (col == width ) break;
                    if (col < 0)
                    {
                        col = 0;
                    }

                    count_number++;

                    average_red += image[li][col].rgbtRed;
                    average_green += image[li][col].rgbtGreen;
                    average_blue += image[li][col].rgbtBlue;

                }

            }
            //set the new average to the pixel
            image[i][j].rgbtRed = (int)round(average_red/count_number) ;
            image[i][j].rgbtGreen = (int)round(average_green/count_number);
            image[i][j].rgbtBlue = (int)round( average_blue/count_number );
        }
    }
    return;
}
qxsslcnc

qxsslcnc3#

好的,我正在高顶线程。。不幸的是提供的解决方案没有工作。正确的解决方案应该是这样的。

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE newImage[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            newImage[i][j] = image[i][j];
        }
    }

    for (int i = 0, red, green, blue, counter; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            red = green = blue = counter = 0;
            // add the center pixel
            if (i >= 0 && j >= 0)
            {
                red += newImage[i][j].rgbtRed;
                green += newImage[i][j].rgbtGreen;
                blue += newImage[i][j].rgbtBlue;
                counter++;
            }
            // add below pixel
            if (i >= 0 && j - 1 >= 0)
            {
                red += newImage[i][j-1].rgbtRed;
                green += newImage[i][j-1].rgbtGreen;
                blue += newImage[i][j-1].rgbtBlue;
                counter++;
            }
            // add right pixel
            if ((i >= 0 && j + 1 >= 0) && (i >= 0 && j + 1 < width))
            {
                red += newImage[i][j+1].rgbtRed;
                green += newImage[i][j+1].rgbtGreen;
                blue += newImage[i][j+1].rgbtBlue;
                counter++;
            }
            // add left pixel
            if (i - 1 >= 0 && j >= 0)
            {
                red += newImage[i-1][j].rgbtRed;
                green += newImage[i-1][j].rgbtGreen;
                blue += newImage[i-1][j].rgbtBlue;
                counter++;
            }
            // add left below pixel
            if (i - 1 >= 0 && j - 1 >= 0)
            {
                red += newImage[i-1][j-1].rgbtRed;
                green += newImage[i-1][j-1].rgbtGreen;
                blue += newImage[i-1][j-1].rgbtBlue;
                counter++;
            }
            // add left upper pixel
            if ((i - 1 >= 0 && j + 1 >= 0) && (i - 1 >= 0 && j + 1 < width))
            {
                red += newImage[i-1][j+1].rgbtRed;
                green += newImage[i-1][j+1].rgbtGreen;
                blue += newImage[i-1][j+1].rgbtBlue;
                counter++;
            }
            // add upper pixel
            if ((i + 1 >= 0 && j >= 0) && (i + 1 < height && j >= 0))
            {
                red += newImage[i+1][j].rgbtRed;
                green += newImage[i+1][j].rgbtGreen;
                blue += newImage[i+1][j].rgbtBlue;
                counter++;
            }
            // add below right pixel
            if ((i + 1 >= 0 && j - 1 >= 0) && (i + 1 < height && j - 1 >= 0))
            {
                red += newImage[i+1][j-1].rgbtRed;
                green += newImage[i+1][j-1].rgbtGreen;
                blue += newImage[i+1][j-1].rgbtBlue;
                counter++;
            }
            // add upper right pixel
            if ((i + 1 >= 0 && j + 1 >= 0) && (i + 1 < height && j + 1 < width))
            {
                red += newImage[i+1][j+1].rgbtRed;
                green += newImage[i+1][j+1].rgbtGreen;
                blue += newImage[i+1][j+1].rgbtBlue;
                counter++;
            }

            image[i][j].rgbtRed = round(red / (counter * 1.0));
            image[i][j].rgbtGreen = round(green / (counter * 1.0));
            image[i][j].rgbtBlue = round(blue / (counter * 1.0));
        }
    }

    return;
}
aiazj4mn

aiazj4mn4#

我已经研究这个问题两天了(天啊!编程是坚韧和令人沮丧的)。我需要一个很好的逻辑,可以帮助我们避免重复同样的事情一遍又一遍。因此,我发现了这个循环。希望它是可以理解的。请让我知道,如果有什么需要澄清。注:包括<math.h>

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //let us a temporary one that will help us store pixels inorder to avoid overwriting any
    RGBTRIPLE temp[height][width];

    //initialize temporary image structure;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j].rgbtBlue = image[i][j].rgbtBlue;
            temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
            temp[i][j].rgbtRed = image[i][j].rgbtRed;
        }
    }

    int blue , red, green;
    float counter;

    //loop over our image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //initialize all variables to 0 for each new pixel
            counter = 0;
            blue = green = red = 0;

            //now for each pixel we will chech over down, up, left and right to see if it meets the condition
            //it means checking the previous, current, and next pixel
            for (int h = -1; h < 2; h++)
            {
                for (int w = -1; w < 2; w++)
                {
                    //chech if it is still inside the image to avoid memory leak or seg fault
                    if (i + h > height-1 || i + h < 0 || j + w > width-1 || j + w < 0)
                    {
                        continue;
                    }

                    //if not the we must loop and add pixels at all corners of our current pixel
                    blue += image[i+h][j+w].rgbtBlue;
                    green += image[i+h][j+w].rgbtGreen;
                    red += image[i+h][j+w].rgbtRed;
                    counter++;
                }
            }

            //at the end of iterations around a pixel, we must record a new pixel that we just found
            temp[i][j].rgbtBlue = round(blue / counter);
            temp[i][j].rgbtGreen = round(green / counter);
            temp[i][j].rgbtRed = round(red / counter);
        }
    }

    //after we finishing iterations over all pixels of our image, let modify the original image
    //recall that we were copying our findings in temp

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
            image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
            image[i][j].rgbtRed = temp[i][j].rgbtRed;
        }
    }

    return;
}
qrjkbowd

qrjkbowd5#

我写这篇文章时完全是一个野蛮人。

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // making a new copy of image
    RGBTRIPLE copy[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j].rgbtRed = image[i][j].rgbtRed;
            copy[i][j].rgbtGreen = image[i][j].rgbtGreen;
            copy[i][j].rgbtBlue = image[i][j].rgbtBlue;
        }
    }
    // looping through each pixel
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            float avgRed = 0;
            float avgGreen = 0;
            float avgBlue = 0;
            // if 3x3 matrix of neighbours is possible (9 elements)
            if (h != 0 && w != 0 && (h + 1) != height && (w + 1) != width)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h - 1][w - 1].rgbtRed + copy[h - 1][w].rgbtRed + copy[h - 1][w + 1].rgbtRed + copy[h][w - 1].rgbtRed + copy[h][w + 1].rgbtRed + copy[h + 1][w - 1].rgbtRed + copy[h + 1][w].rgbtRed + copy[h + 1][w + 1].rgbtRed) / 9.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h - 1][w - 1].rgbtGreen + copy[h - 1][w].rgbtGreen + copy[h - 1][w + 1].rgbtGreen + copy[h][w - 1].rgbtGreen + copy[h][w + 1].rgbtGreen + copy[h + 1][w - 1].rgbtGreen + copy[h + 1][w].rgbtGreen + copy[h + 1][w + 1].rgbtGreen) / 9.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h - 1][w - 1].rgbtBlue + copy[h - 1][w].rgbtBlue + copy[h - 1][w + 1].rgbtBlue + copy[h][w - 1].rgbtBlue + copy[h][w + 1].rgbtBlue + copy[h + 1][w - 1].rgbtBlue + copy[h + 1][w].rgbtBlue + copy[h + 1][w + 1].rgbtBlue) / 9.00;
            }
            // top edge and not corner (6 elements)
            else if (h == 0 && w != 0 && (w + 1) != width)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h][w - 1].rgbtRed + copy[h][w + 1].rgbtRed + copy[h + 1][w - 1].rgbtRed + copy[h + 1][w].rgbtRed + copy[h + 1][w + 1].rgbtRed) / 6.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h][w - 1].rgbtGreen + copy[h][w + 1].rgbtGreen + copy[h + 1][w - 1].rgbtGreen + copy[h + 1][w].rgbtGreen + copy[h + 1][w + 1].rgbtGreen) / 6.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h][w - 1].rgbtBlue + copy[h][w + 1].rgbtBlue + copy[h + 1][w - 1].rgbtBlue + copy[h + 1][w].rgbtBlue + copy[h + 1][w + 1].rgbtBlue) / 6.00;
            }

            // bottom edge and not corner ( 6 elements)
            else if (h == height - 1 && w != 0 && (w + 1) != width)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h][w - 1].rgbtRed + copy[h][w + 1].rgbtRed + copy[h - 1][w - 1].rgbtRed + copy[h - 1][w].rgbtRed + copy[h - 1][w + 1].rgbtRed) / 6.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h][w - 1].rgbtGreen + copy[h][w + 1].rgbtGreen + copy[h - 1][w - 1].rgbtGreen + copy[h - 1][w].rgbtGreen + copy[h - 1][w + 1].rgbtGreen) / 6.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h][w - 1].rgbtBlue + copy[h][w + 1].rgbtBlue + copy[h - 1][w - 1].rgbtBlue + copy[h - 1][w].rgbtBlue + copy[h - 1][w + 1].rgbtBlue) / 6.00;
            }

            // left edge and not corner ( 6 elements)
            else if (w == 0 && h != 0 && (h + 1) != height)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h - 1][w].rgbtRed + copy[h + 1][w].rgbtRed + copy[h - 1][w + 1].rgbtRed + copy[h][w + 1].rgbtRed + copy[h + 1][w + 1].rgbtRed) / 6.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h - 1][w].rgbtGreen + copy[h + 1][w].rgbtGreen + copy[h - 1][w + 1].rgbtGreen + copy[h][w + 1].rgbtGreen + copy[h + 1][w + 1].rgbtGreen) / 6.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h - 1][w].rgbtBlue + copy[h + 1][w].rgbtBlue + copy[h - 1][w + 1].rgbtBlue + copy[h][w + 1].rgbtBlue + copy[h + 1][w + 1].rgbtBlue) / 6.00;
            }

            // right edge and not corner (6 elements)
            else if (w == width - 1 && h != 0 && (h + 1 != height))
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h - 1][w].rgbtRed + copy[h + 1][w].rgbtRed + copy[h - 1][w - 1].rgbtRed + copy[h][w - 1].rgbtRed + copy[h + 1][w - 1].rgbtRed) / 6.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h - 1][w].rgbtGreen + copy[h + 1][w].rgbtGreen + copy[h - 1][w - 1].rgbtGreen + copy[h][w - 1].rgbtGreen + copy[h + 1][w - 1].rgbtGreen) / 6.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h - 1][w].rgbtBlue + copy[h + 1][w].rgbtBlue + copy[h - 1][w - 1].rgbtBlue + copy[h][w - 1].rgbtBlue + copy[h + 1][w - 1].rgbtBlue) / 6.00;
            }

            // top left corner ( 4 elements)
            else if (h == 0 && w == 0)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h][w + 1].rgbtRed + copy[h + 1][w].rgbtRed + copy[h + 1][w + 1].rgbtRed) / 4.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h][w + 1].rgbtGreen + copy[h + 1][w].rgbtGreen + copy[h + 1][w + 1].rgbtGreen) / 4.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h][w + 1].rgbtBlue + copy[h + 1][w].rgbtBlue + copy[h + 1][w + 1].rgbtBlue) / 4.00;
            }

            // top right corner ( 4 elements)
            else if (h == 0 && w + 1 == width)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h][w - 1].rgbtRed + copy[h + 1][w].rgbtRed + copy[h + 1][w - 1].rgbtRed) / 4.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h][w - 1].rgbtGreen + copy[h + 1][w].rgbtGreen + copy[h + 1][w - 1].rgbtGreen) / 4.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h][w - 1].rgbtBlue + copy[h + 1][w].rgbtBlue + copy[h + 1][w - 1].rgbtBlue) / 4.00;
            }

            // bottom left corner ( 4 elements)
            else if (h + 1 == height && w == 0)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h - 1][w].rgbtRed + copy[h - 1][w + 1].rgbtRed + copy[h][w + 1].rgbtRed) / 4.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h - 1][w].rgbtGreen + copy[h - 1][w + 1].rgbtGreen + copy[h][w + 1].rgbtGreen) / 4.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h - 1][w].rgbtBlue + copy[h - 1][w + 1].rgbtBlue + copy[h][w + 1].rgbtBlue) / 4.00;
            }
            // bottom right corner ( 4 elements)
            else if (h + 1 == height && w + 1 == width)
            {
                avgRed = (double)(copy[h][w].rgbtRed + copy[h - 1][w].rgbtRed + copy[h - 1][w - 1].rgbtRed + copy[h][w - 1].rgbtRed) / 4.00;
                avgGreen = (double)(copy[h][w].rgbtGreen + copy[h - 1][w].rgbtGreen + copy[h - 1][w - 1].rgbtGreen + copy[h][w - 1].rgbtGreen) / 4.00;
                avgBlue = (double)(copy[h][w].rgbtBlue + copy[h - 1][w].rgbtBlue + copy[h - 1][w - 1].rgbtBlue + copy[h][w - 1].rgbtBlue) / 4.00;
            }
            // assigning averge values to the original image
            image[h][w].rgbtRed = round(avgRed);
            image[h][w].rgbtGreen = round(avgGreen);
            image[h][w].rgbtBlue = round(avgBlue);
        }
    }
    return;
}
t3irkdon

t3irkdon6#

这段代码在你考虑模糊一个图像或者使用方块模糊的n*n matxix时效果很好。它考虑顶部,底部和中间行,并且分别计算像素RGB。

// Blur image

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int sumBlue;
    int sumGreen;
    int sumRed;
    //create a temporary table
    RGBTRIPLE temp[height][width];

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            sumBlue = 0;
            sumGreen = 0;
            sumRed = 0;
            //Top row
            if (i == 0)
            {
                if (j == 0) //top row left corner
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          +  image[i + 1][j].rgbtGreen
                                          +  image[i + 1][j + 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j + 1].rgbtRed) / 4);

                }

                else if (j == width - 1) //top row right corner
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i + 1][j].rgbtGreen
                                          + image[i + 1][j - 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j - 1].rgbtRed) / 4);
                }

                else //top row middle pixel
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 6);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i + 1][j].rgbtGreen
                                          + image[i + 1][j - 1].rgbtGreen
                                          + image[i + 1][j + 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j - 1].rgbtRed
                                        + image[i + 1][j + 1].rgbtRed) / 6);
                }

            }
            //Bottom row of image
            else if (i == height - 1)
            {
                if (j == 0) //left side pixel of bottom row
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j + 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j + 1].rgbtRed) / 4);
                }

                else if (j == width - 1) //right side pixel of last row
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j - 1].rgbtBlue) / 4);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j - 1].rgbtGreen) / 4);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j - 1].rgbtRed) / 4);
                }

                else //middle pixels of last row
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j - 1].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue) / 6);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j - 1].rgbtGreen
                                          + image[i - 1][j + 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j - 1].rgbtRed
                                        + image[i - 1][j + 1].rgbtRed) / 6);
                }

            }

            else
            {
                if (j == 0) //left side of image
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 6); //6 pixels surrounding the left side of the image
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          +  image[i - 1][j].rgbtGreen
                                          +  image[i + 1][j].rgbtGreen
                                          +  image[i][j + 1].rgbtGreen
                                          +  image[i - 1][j + 1].rgbtGreen
                                          +  image[i + 1][j + 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        +  image[i - 1][j].rgbtRed
                                        +  image[i + 1][j].rgbtRed
                                        +  image[i][j + 1].rgbtRed
                                        +  image[i - 1][j + 1].rgbtRed
                                        +  image[i + 1][j + 1].rgbtRed) / 6);
                }

                //Right side of image
                else if (j == width - 1)
                {
                    sumBlue = round(ceil(image[i][j].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i - 1][j - 1].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue) / 6);
                    sumGreen = round(ceil(image[i][j].rgbtGreen
                                          +  image[i - 1][j].rgbtGreen
                                          +  image[i + 1][j].rgbtGreen
                                          +  image[i][j - 1].rgbtGreen
                                          +  image[i - 1][j - 1].rgbtGreen
                                          +  image[i + 1][j - 1].rgbtGreen) / 6);
                    sumRed = round(ceil(image[i][j].rgbtRed
                                        +  image[i - 1][j].rgbtRed
                                        +  image[i + 1][j].rgbtRed
                                        +  image[i][j - 1].rgbtRed
                                        +  image[i - 1][j - 1].rgbtRed
                                        +  image[i + 1][j - 1].rgbtRed) / 6);
                }

                else //middle pixels of middle rows
                {
                    //calculate for blue pixels
                    sumBlue = round(ceil(image[i - 1][j - 1].rgbtBlue
                                         + image[i - 1][j].rgbtBlue
                                         + image[i - 1][j + 1].rgbtBlue
                                         + image[i][j - 1].rgbtBlue
                                         + image[i][j].rgbtBlue
                                         + image[i][j + 1].rgbtBlue
                                         + image[i + 1][j - 1].rgbtBlue
                                         + image[i + 1][j].rgbtBlue
                                         + image[i + 1][j + 1].rgbtBlue) / 9);
                    //calculate for green pixels
                    sumGreen = round(ceil(image[i - 1][j - 1].rgbtGreen
                                          + image[i - 1][j].rgbtGreen
                                          + image[i - 1][j + 1].rgbtGreen
                                          + image[i][j - 1].rgbtGreen
                                          + image[i][j].rgbtGreen
                                          + image[i][j + 1].rgbtGreen
                                          + image[i + 1][j - 1].rgbtGreen
                                          + image[i + 1][j].rgbtGreen
                                          + image[i + 1][j + 1].rgbtGreen) / 9); // 9 pixels surrounding the middle one
                    //calculate for red pixels
                    sumRed = round(ceil(image[i - 1][j - 1].rgbtRed
                                        + image[i - 1][j].rgbtRed
                                        + image[i - 1][j + 1].rgbtRed
                                        + image[i][j - 1].rgbtRed
                                        + image[i][j].rgbtRed
                                        + image[i][j + 1].rgbtRed
                                        + image[i + 1][j - 1].rgbtRed
                                        + image[i + 1][j].rgbtRed
                                        + image[i + 1][j + 1].rgbtRed) / 9);
                }
            }

            //assign temp values with the calculated values
            temp[i][j].rgbtBlue = round((sumBlue));
            temp[i][j].rgbtGreen = round((sumGreen));
            temp[i][j].rgbtRed = round((sumRed));

        }
    }

    //copies values from temporary table and assigns to original image
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            //assiging temp value to original image
            image[j][i].rgbtBlue = temp[j][i].rgbtBlue;
            image[j][i].rgbtGreen = temp[j][i].rgbtGreen;
            image[j][i].rgbtRed = temp[j][i].rgbtRed;
        }
    }
}

相关问题