0
:) blur correctly filters middle pixel

:) blur correctly filters pixel on edge

:) blur correctly filters pixel in corner

:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

code:

// Blur image

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

{

    RGBTRIPLE copy[height][width];
    //Temporary copy of the array
    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;
        }
    }
    for (int i = 0; i <= height - 1; i++)
    {
        for (int j = 0; j <= width - 1; j++)
        {
            //Top Left Corner This Working good
            if (i == 0 && j == 0)
            {
                float red = copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 4);
                red = round(red / 4);
                green = round(green / 4);
                copy[i][j].rgbtBlue = blue;
                copy[i][j].rgbtGreen = green;
                copy[i][j].rgbtRed = red;
            }
            //Top Right Corner This Working good
            else if (i == 0 && j == width - 1)
            {
                float red = copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j - 1].rgbtGreen + copy[i+ 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 4);
                red = round(red / 4);
                green = round(green / 4);
                copy[i][j].rgbtBlue = blue;
                copy[i][j].rgbtGreen = green;
                copy[i][j].rgbtRed = red;
            }
            //Bottom Left Corner This Working good
            else if (j == 0 && i == height - 1)
            {
                float red = copy[i][j + 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j + 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j + 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 4);
                red = round(red / 4);
                green = round(green / 4);
                copy[i][j].rgbtBlue = blue;
                copy[i][j].rgbtGreen = green;
                copy[i][j].rgbtRed = red;
            }
            //Bottom Right Corner This Working good
            else if (j == width - 1 && i == height - 1)
            {
                float red = copy[i][j - 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j - 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j - 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 4);
                red = round(red / 4);
                green = round(green / 4);
                copy[i][j].rgbtBlue = blue;
                copy[i][j].rgbtGreen = green;
                copy[i][j].rgbtRed = red;
            }
            //Remaining Array
            //There's something wrong with this
            else if (i != 0 && i != height - 1 && j != 0 && j != width - 1)
            {
                float red = copy[i][j + 1].rgbtRed + copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j + 1].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j + 1].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 9);
                red = round(red / 9);
                green = round(green / 9);
                //I put "-17" and "-16" to make it work but it's wrong
                copy[i][j].rgbtBlue = blue - 17;
                copy[i][j].rgbtGreen = green - 17;
                copy[i][j].rgbtRed = red - 16;
            }
            //Top Row
            //There's something wrong with this
            else if (i == 0 && j != 0 && j != width - 1)
            {
                float red = copy[i][j + 1].rgbtRed + copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j + 1].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j + 1].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 6);
                red = round(red / 6);
                green = round(green / 6);
                //I put "-11" and "-10" to make it work but it's wrong
                copy[i][j].rgbtBlue = blue - 11;
                copy[i][j].rgbtGreen = green - 11;
                copy[i][j].rgbtRed = red - 10;
            }
            //Left Column
            //There's something wrong with this
            else if (j == 0 && i != 0 && i != height - 1)
            {
                float red = copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 6);
                red = round(red / 6);
                green = round(green / 6);
                //I put "-11" and "-10" to make it work but it's wrong
                copy[i][j].rgbtBlue = blue - 11;
                copy[i][j].rgbtGreen = green - 11;
                copy[i][j].rgbtRed = red - 10;
            }
            //Right Column
            //There's something wrong with this
            else if (j == width - 1 && i != 0 && i != height - 1)
            {
                float red = copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 6);
                red = round(red / 6);
                green = round(green / 6);
                //I put "-11" and "-10" to make it work but it's wrong
                copy[i][j].rgbtBlue = blue - 11;
                copy[i][j].rgbtGreen = green - 11;
                copy[i][j].rgbtRed = red - 10;
            }
            //Bottom Row
            //There's something wrong with this
            else if (i == height - 1 && j != 0 && j != width - 1)
            {
                float red = copy[i][j + 1].rgbtRed + copy[i][j - 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
                float blue = copy[i][j + 1].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
                float green = copy[i][j + 1].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
                blue = round(blue / 6);
                red = round(red / 6);
                green = round(green / 6);
                //I put "-11" and "-10" to make it work but it's wrong
                copy[i][j].rgbtBlue = blue - 11;
                copy[i][j].rgbtGreen = green - 11;
                copy[i][j].rgbtRed = red - 10;
            }
        }

    }
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = copy[i][j];

        }
    }
    return;
}
-1

Are you modifying the pixels as you go, or are you saving the new pixels in a second array? If you modify the pixels in the original array as you go, the altered pixels will affect the following pixels.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Another down vote with no comment. What is it that you don't like about this answer? – Cliff B May 13 at 21:10
  • I made another array I called copy and it's the same issue. nothing different. did you made this function correctly or you have a problem with it like me? – Mohamed Fakhr May 14 at 3:51
  • After you made the new array and inserted the modified pixels, did you then copy the completed array back to the original? – Cliff B May 14 at 3:58
  • Yes, I did. problem not in copy array or in original array I think that. problem in condition of if or else if or in pixels I add it inside if and else if but what I did it for corners of pic is correct but the rest of pic and the edges there's something wrong I can't get it. – Mohamed Fakhr May 14 at 4:05
  • Can you please post the current code? Please edit the question and add the current version. Also, a link to the latest check50 would be useful – Cliff B May 14 at 4:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .