0

I have read many similar problems on here about the same issue. I believe my function is correct, although redundant and not very stylish. It should however get the job done. I get the same pixels wrong,(top right corner, bottom left corner, and bottom right corner) every time I run my code. The code passes the individual pixel checks as well. Any insight would be greatly appreciated. Code is below:

void blur(int height, int width, RGBTRIPLE image[height][width]) { // Create copy of image to avoid confusion with blur function RGBTRIPLE copy[height][width];

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

for (i = 0; i < height; i++)
{
    for (j = 0; j < width; j++)
    {
        // Top left
        if (i == 0 && j == 0)
        {
            float rdTL = round((copy[i][j].rgbtRed + copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 4.0);
            float grTL = round((copy[i][j].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 4.0);
            float blTL = round((copy[i][j].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 4.0);
            image[i][j].rgbtRed = rdTL;
            image[i][j].rgbtGreen = grTL;
            image[i][j].rgbtBlue = blTL;
        }
        // Top right
        else if (i == 0 && j == width - 1)
        {
            float rdTR = round((copy[i][j - 1].rgbtRed + copy[i][j - 2].rgbtRed + copy [i + 1][j - 1].rgbtRed + copy[i + 1][j - 2].rgbtRed) / 4.0);
            float grTR = round((copy[i][j - 1].rgbtGreen + copy[i][j - 2].rgbtGreen + copy [i  + 1][j - 1].rgbtGreen + copy[i + 1][j - 2].rgbtGreen) / 4.0);
            float blTR = round((copy[i][j - 1].rgbtBlue + copy[i][j - 2].rgbtBlue + copy [i + 1][j - 1].rgbtBlue + copy[i + 1][j - 2].rgbtBlue) / 4.0);
            image[i][j].rgbtRed = rdTR;
            image[i][j].rgbtGreen = grTR;
            image[i][j].rgbtBlue = blTR;
        }
        // Bottom left
        else if (i == height - 1 && j == 0)
        {
            float rdBL = round((copy[i - 1][j].rgbtRed + copy[i - 2][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i - 2][j + 1].rgbtRed) / 4.0);
            float grBL = round((copy[i - 1][j].rgbtGreen + copy[i - 2][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i - 2][j + 1].rgbtGreen) / 4.0);
            float blBL = round((copy[i - 1][j].rgbtBlue + copy[i - 2][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i - 2][j + 1].rgbtBlue) / 4.0);
            image[i][j].rgbtRed = rdBL;
            image[i][j].rgbtGreen = grBL;
            image[i][j].rgbtBlue = blBL;
        }
        // Bottom right
        else if (i == height - 1 && j == width - 1)
        {
            float rdBR = round((copy[i - 1][j - 1].rgbtRed + copy[i - 1][j - 2].rgbtRed + copy[i - 2][j - 1].rgbtRed + copy[i - 2][j - 2].rgbtRed) / 4.0);
            float grBR = round((copy[i - 1][j - 1].rgbtGreen + copy[i - 1][j - 2].rgbtGreen + copy[i - 2][j - 1].rgbtGreen + copy[i - 2][j - 2].rgbtGreen) / 4.0);
            float blBR = round((copy[i - 1][j - 1].rgbtBlue + copy[i - 1][j - 2].rgbtBlue + copy[i - 2][j - 1].rgbtBlue + copy[i - 2][j - 2].rgbtBlue) / 4.0);
            image[i][j].rgbtRed = rdBR;
            image[i][j].rgbtGreen = grBR;
            image[i][j].rgbtBlue = blBR;
        }
        // Top row
        else if (i == 0 && j != 0 && j != width - 1)
        {
            float rdT = round((copy[i][j - 1].rgbtRed + copy[i][j].rgbtRed + copy[i][j + 1].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 6.0);
            float grT = round((copy[i][j - 1].rgbtGreen + copy[i][j].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 6.0);
            float blT = round((copy[i][j - 1].rgbtBlue + copy[i][j].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 6.0);
            image[i][j].rgbtRed = rdT;
            image[i][j].rgbtGreen = grT;
            image[i][j].rgbtBlue = blT;
        }
        // Bottom row
        else if (i == height - 1 && j != 0 && j != width - 1)
        {
            float rdB = round((copy[i][j - 1].rgbtRed + copy[i][j].rgbtRed + copy[i][j + 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed) / 6.0);
            float grB = round((copy[i][j - 1].rgbtGreen + copy[i][j].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen) / 6.0);
            float blB = round((copy[i][j - 1].rgbtBlue + copy[i][j].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue) / 6.0);
            image[i][j].rgbtRed = rdB;
            image[i][j].rgbtGreen = grB;
            image[i][j].rgbtBlue = blB;
        }
        // Left wall
        else if (i != 0 && i != height - 1 && j == 0)
        {
            float rdL = round((copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j].rgbtRed + copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 6.0);
            float grL = round((copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 6.0);
            float blL = round((copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 6.0);
            image[i][j].rgbtRed = rdL;
            image[i][j].rgbtGreen = grL;
            image[i][j].rgbtBlue = blL;
        }
        // Right wall
        else if (i != 0 && i != height - 1 && j == width - 1)
        {
            float rdR = round((copy[i][j].rgbtRed + copy[i][j - 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j - 1].rgbtRed) / 6.0);
            float grR = round((copy[i][j].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen) / 6.0);
            float blR = round((copy[i][j].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue) / 6.0);
            image[i][j].rgbtRed = rdR;
            image[i][j].rgbtGreen = grR;
            image[i][j].rgbtBlue = blR;
        }
        // Remainder of the field pixels
        else
        {
            float rd = round((copy[i - 1][j - 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j - 1].rgbtRed + copy[i][j].rgbtRed + copy[i][j + 1].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 9.0);
            float gr = round((copy[i - 1][j - 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i][j].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 9.0);
            float bl = round((copy[i - 1][j - 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i][j].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 9.0);
            image[i][j].rgbtRed = rd;
            image[i][j].rgbtGreen = gr;
            image[i][j].rgbtBlue = bl;
        }
    }
}
return;

}

1 Answer 1

0

The big clue is that the problem lies with 3 of the 4 corners, literally corner cases. Upper left is fine, but the other 3 have the same problem.

Let's take the upper right corner. It calculates based on 4 pixels. The question is, which 4 pixels?

The upper right pixel is (i,j). The four pixels should be (i,j), (i+1,j), (i,j-1) and (i+1,j-1).

So why on earth would the code be looking at j-2 for anything????

Similar logic applies to the other corners.

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

You must log in to answer this question.

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