0

The code works fine, but it fails blur filters a 3x3 and 4x4 image. I would like a help on why it correctly blur a middle pixel, but fails those two. Apparently, the wrong pixels are either on edge (middle right, 130,150,160 for blue), and bottom (200 ,210, 220; 220,230,240; and 240, 250, 255 blue). But check50 passed ok both corner and edge pixel. Dont understand why fails 3x3 and 4x4'

// Blur image void blur(int height, int width, RGBTRIPLE image[height][width]) { RGBTRIPLE red_pixel[height][width]; RGBTRIPLE green_pixel[height][width]; RGBTRIPLE blue_pixel[height][width];

RGBTRIPLE new_red[height][width];
RGBTRIPLE new_green[height][width];
RGBTRIPLE new_blue[height][width];

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        //copying the image
        red_pixel[i][j].rgbtRed = image[i][j].rgbtRed;
        green_pixel[i][j].rgbtGreen = image[i][j].rgbtGreen;
        blue_pixel[i][j].rgbtBlue = image[i][j].rgbtBlue;
    }
}

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        //New Red
        new_red[i][j].rgbtRed = red_pixel[i-1][j-1].rgbtRed + red_pixel[i-1][j].rgbtRed
        + red_pixel[i-1][j+1].rgbtRed + red_pixel[i][j-1].rgbtRed + red_pixel[i][j].rgbtRed
        + red_pixel[i][j+1].rgbtRed + red_pixel[i+1][j-1].rgbtRed +
        red_pixel[i+1][j].rgbtRed + red_pixel[i+1][j+1].rgbtRed;

        //New Green
        new_green[i][j].rgbtGreen = green_pixel[i-1][j-1].rgbtGreen + green_pixel[i-1][j].rgbtGreen
        + green_pixel[i-1][j+1].rgbtGreen + green_pixel[i][j-1].rgbtGreen + green_pixel[i][j].rgbtGreen
        + green_pixel[i][j+1].rgbtGreen + green_pixel[i+1][j-1].rgbtGreen +
        green_pixel[i+1][j].rgbtGreen + green_pixel[i+1][j+1].rgbtGreen;

        //New Blue
        new_blue[i][j].rgbtBlue = blue_pixel[i-1][j-1].rgbtBlue + blue_pixel[i-1][j].rgbtBlue
        + blue_pixel[i-1][j+1].rgbtBlue + blue_pixel[i][j-1].rgbtBlue + blue_pixel[i][j].rgbtBlue
        + blue_pixel[i][j+1].rgbtBlue + blue_pixel[i+1][j-1].rgbtBlue +
        blue_pixel[i+1][j].rgbtBlue + blue_pixel[i+1][j+1].rgbtBlue;

        if(i == (height - 1) && (j = (width - 1))) // botton right corner
        {

           new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed + red_pixel[i - 1][j - 1].rgbtRed + red_pixel[i - 1][j].rgbtRed + red_pixel[i][j - 1].rgbtRed) / 4.0);

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i - 1][j - 1].rgbtGreen + green_pixel[i - 1][j].rgbtGreen + green_pixel[i][j - 1].rgbtGreen) / 4.0) ;

            new_blue[i][j].rgbtBlue = round((blue_pixel[i][j].rgbtBlue + blue_pixel[i-1][j - 1].rgbtBlue + blue_pixel[i - 1][j - 1].rgbtBlue + blue_pixel[i][j - 1].rgbtBlue) / 4.0);

        }


        if (i == (height - 1) && j == 0)  //  botton left corner
        {               

            //Corner case
            //int n = 4;
            new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed + red_pixel[i - 1][j].rgbtRed + red_pixel[i - 1][j + 1].rgbtRed +  red_pixel[i][j + 1].rgbtRed) / 4.0) ;

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i - 1][j].rgbtGreen + green_pixel[i - 1][j + 1].rgbtGreen +  green_pixel[i][j + 1].rgbtGreen)  / 4.0) ;

            new_blue[i][j].rgbtBlue = round((blue_pixel[i][j].rgbtBlue + blue_pixel[i - 1][j].rgbtBlue + blue_pixel[i - 1][j + 1].rgbtBlue +  blue_pixel[i][j + 1].rgbtBlue) / 4.0) ;

        }

        if (i == 0 && j == (width - 1))// top right corner
        {               

            //Corner case
            //int n = 4;
            new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed  + red_pixel[i][j - 1].rgbtRed + red_pixel[i + 1][j - 1].rgbtRed + green_pixel[i + 1][j].rgbtGreen) / 4.0) ;

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i][j - 1].rgbtGreen + green_pixel[i + 1][j - 1].rgbtGreen + green_pixel[i + 1][j].rgbtGreen)  / 4.0) ;

            new_blue[i][j].rgbtBlue = round((blue_pixel[i][j].rgbtBlue + blue_pixel[i][j - 1].rgbtBlue + blue_pixel[i + 1][j - 1].rgbtBlue + blue_pixel[i + 1][j].rgbtBlue) / 4.0) ;

        }

        if (i == 0 && j == 0)//top left corner
        {                

            //Corner case
            //int n = 4;
            new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed + red_pixel[i][j+1].rgbtRed + red_pixel[i+1][j].rgbtRed + red_pixel[i+1][j+1].rgbtRed) / 4.0) ;

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i][j + 1].rgbtGreen + green_pixel[i + 1][j].rgbtGreen + green_pixel[i + 1][j + 1].rgbtGreen) / 4.0) ;

            new_blue[i][j].rgbtBlue = round((blue_pixel[i][j].rgbtBlue + blue_pixel[i][j + 1].rgbtBlue + blue_pixel[i + 1][j].rgbtBlue + blue_pixel[i + 1][j + 1].rgbtBlue) / 4.0) ;

        }

        if (j == 0 && i != 0 && i != (height - 1))//left edge
        {             

            //Edges
            //int n = 6;
            new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed + red_pixel[i - 1][j].rgbtRed + red_pixel[i - 1][j + 1].rgbtRed + red_pixel[i][j + 1].rgbtRed +
            red_pixel[i + 1][j].rgbtRed + red_pixel[i + 1][j + 1].rgbtRed) / 6.0) ;

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i - 1][j].rgbtGreen + green_pixel[i - 1][j + 1].rgbtGreen + green_pixel[i][j + 1].rgbtGreen +
            green_pixel[i + 1][j].rgbtGreen + green_pixel[i + 1][j + 1].rgbtGreen ) / 6.0) ;

            new_blue[i][j].rgbtBlue = round((blue_pixel[i][j].rgbtBlue + blue_pixel[i - 1][j].rgbtBlue + blue_pixel[i - 1][j + 1].rgbtBlue + blue_pixel[i][j + 1].rgbtBlue +
            blue_pixel[i + 1][j].rgbtBlue + blue_pixel[i + 1][j + 1].rgbtBlue) / 6.0) ;

        }

        if (j == (width - 1) && i != 0 && i != (height - 1))//right edge
        {             

            //Edges
            //int n = 6;
            new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed + red_pixel[i - 1][j].rgbtRed + red_pixel[i - 1][j - 1].rgbtRed + red_pixel[i][j - 1].rgbtRed +
            red_pixel[i + 1][j - 1].rgbtRed + red_pixel[i + 1][j].rgbtRed) / 6.0) ;

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i - 1][j].rgbtGreen + green_pixel[i - 1][j - 1].rgbtGreen + green_pixel[i][j - 1].rgbtGreen
            + green_pixel[i + 1][j - 1].rgbtGreen + green_pixel[i + 1][j].rgbtGreen) / 6.0) ;

            new_blue[i][j].rgbtBlue = round((green_pixel[i][j].rgbtBlue + blue_pixel[i - 1][j].rgbtBlue + blue_pixel[i - 1][j - 1].rgbtBlue + blue_pixel[i][j - 1].rgbtBlue +
            blue_pixel[i + 1][j - 1].rgbtBlue + blue_pixel[i + 1][j].rgbtBlue) / 6.0) ;

        }

        if (i == (height - 1) && j != 0 && j != (width - 1)) //bottom edge
        {                
            //Edges
            //int n = 6;
            new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed + red_pixel[i][j - 1].rgbtRed + red_pixel[i][j + 1].rgbtRed + red_pixel[i - 1][j - 1].rgbtRed +
            red_pixel[i - 1][j].rgbtRed + red_pixel[i - 1][j + 1].rgbtRed) / 6.0) ;

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i][j - 1].rgbtGreen + green_pixel[i][j + 1].rgbtGreen + green_pixel[i - 1][j - 1].rgbtGreen +
            green_pixel[i - 1][j].rgbtGreen + green_pixel[i - 1][j + 1].rgbtGreen) / 6.0) ;

            new_blue[i][j].rgbtBlue = round((blue_pixel[i][j].rgbtBlue + blue_pixel[i][j - 1].rgbtBlue + blue_pixel[i][j + 1].rgbtBlue + blue_pixel[i - 1][j - 1].rgbtBlue +
            blue_pixel[i - 1][j].rgbtBlue + blue_pixel[i - 1][j + 1].rgbtBlue) / 6.0) ;


        }

        if (i == 0 && j != 0 && j != (width - 1))//top edge
        {               

            //Edges
            //int n = 6;
            new_red[i][j].rgbtRed = round((red_pixel[i][j].rgbtRed + red_pixel[i][j - 1].rgbtRed + red_pixel[i][j + 1].rgbtRed + red_pixel[i + 1][j - 1].rgbtRed +
            red_pixel[i + 1][j].rgbtRed + red_pixel[i + 1][j + 1].rgbtRed) / 6.0) ;

            new_green[i][j].rgbtGreen = round((green_pixel[i][j].rgbtGreen + green_pixel[i][j - 1].rgbtGreen + green_pixel[i][j + 1].rgbtGreen + green_pixel[i + 1][j - 1].rgbtGreen +
            green_pixel[i + 1][j].rgbtGreen + green_pixel[i + 1][j + 1].rgbtGreen ) / 6.0) ;

            new_blue[i][j].rgbtBlue = round((blue_pixel[i][j].rgbtBlue + blue_pixel[i][j - 1].rgbtBlue + blue_pixel[i][j + 1].rgbtBlue + blue_pixel[i + 1][j - 1].rgbtBlue +
            blue_pixel[i + 1][j].rgbtBlue + blue_pixel[i + 1][j + 1].rgbtBlue) / 6.0) ;

        }

        if (i > 0 && i < (height - 1) && j > 0 && j < (width - 1))
        {

            //pixels inside
            //int n = 9;
            new_red[i][j].rgbtRed = round((red_pixel[i-1][j-1].rgbtRed + red_pixel[i-1][j].rgbtRed
            + red_pixel[i-1][j+1].rgbtRed + red_pixel[i][j-1].rgbtRed + red_pixel[i][j].rgbtRed
            + red_pixel[i][j+1].rgbtRed + red_pixel[i+1][j-1].rgbtRed +
            red_pixel[i+1][j].rgbtRed + red_pixel[i+1][j+1].rgbtRed) / 9.0);

            new_green[i][j].rgbtGreen = round((green_pixel[i-1][j-1].rgbtGreen + green_pixel[i-1][j].rgbtGreen
            + green_pixel[i-1][j+1].rgbtGreen + green_pixel[i][j-1].rgbtGreen + green_pixel[i][j].rgbtGreen
            + green_pixel[i][j+1].rgbtGreen + green_pixel[i+1][j-1].rgbtGreen +
            green_pixel[i+1][j].rgbtGreen + green_pixel[i+1][j+1].rgbtGreen) / 9.0);

            new_blue[i][j].rgbtBlue = round((blue_pixel[i-1][j-1].rgbtBlue + blue_pixel[i-1][j].rgbtBlue
            + blue_pixel[i-1][j+1].rgbtBlue + blue_pixel[i][j-1].rgbtBlue + blue_pixel[i][j].rgbtBlue
            + blue_pixel[i][j+1].rgbtBlue + blue_pixel[i+1][j-1].rgbtBlue +
            blue_pixel[i+1][j].rgbtBlue + blue_pixel[i+1][j+1].rgbtBlue) / 9.0);

        }

        image[i][j].rgbtRed = (new_red[i][j].rgbtRed);
        image[i][j].rgbtGreen = (new_green[i][j].rgbtGreen);
        image[i][j].rgbtBlue = (new_blue[i][j].rgbtBlue);

    }
}
return;

}

  • Please edit your question and a fuller description of the problem, your concerns, the score you received including the detailed breakdown, etc. Posting code with little or no explanation (a quick few words in the title is insufficient) is called a naked code drop and is against forum rules. – Cliff B Aug 1 at 2:41

You must log in to answer this question.

Browse other questions tagged .