2

Can't figure out what the problem with this code is. It seems to be working correctly for corners, but for nothing else. Please help!

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            int startu = (i == 0)?0:i-1;
            int startv = (j == 0)?0:j-1;
            
            int endu = (i == height - 1)?height - 1:i+1;
            int endv = (j == width - 1)?width - 1:j+1;
            
            float R = 0;
            float G = 0;
            float B = 0;
            
            int u = 0;
            int v = 0;
            for(u = startu; u <= endu; u++)
            {
                for(v = startv; v <= endv; v++)
                {
                    R = R + image[u][v].rgbtRed;
                    G = G + image[u][v].rgbtGreen;
                    B = B + image[u][v].rgbtBlue;
                }
            }
            
            int u1 = u - startu;
            int v1 = v - startv;
            
            image[i][j].rgbtRed = round(R / (u1 * v1));
            image[i][j].rgbtGreen = round(G / (u1 * v1));
            image[i][j].rgbtBlue = round(B / (u1 * v1));
            
        }
    }
    
    return;
}

This is what I get:

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "145 160 169\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "90 106 116\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
2

This is happening because your loop is summing values from neighbors to the left and up of the current pixel that have already been blurred by previous iterations of the same loop. You need to modify a copy of image[][], not image[][] itself. memcpy can help you copy the data from image[][] into another two dimensional array. Otherwise, your function seems to work correctly, and it's easy to read!

If this answers your question, please click on the check mark to accept. Don't be shy to ask another question the next time you have an issue!

| improve this answer | |
  • 1
    thank you so much, you're a real life saver!!! I spent over 5 hours figuring out what I was doing wrong! – Lakshya Dugar Aug 23 at 15:40

You must log in to answer this question.

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