0

For some reason, my CS50 pset4 blur function is not returning the proper pixel values for a 3x3 or a 4x4 image. It passes the other check50 tests. I looked at the check50 output, and my code gets the first 5 pixels of the 3x3 image correct, but gets the remaining 4 incorrect. The values are lower than what I would expect.

Can anyone tell me what my code is doing that is causing this?

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];
    //creates a copy of the original image
    for (int h = 0; h < height; h++)
        for (int w = 0; w < width; w++)
        {
            copy[h][w].rgbtRed = image[h][w].rgbtRed;
            copy[h][w].rgbtGreen = image[h][w].rgbtGreen;
            copy[h][w].rgbtBlue = image[h][w].rgbtBlue;
        }
    //blur the image based on the copied values
    //this keeps track of the number of terms
    //averaged together
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
        {
            int terms = 0;
            //This keeps track of the total sums of the
            //bordering pixel colors
            int sumR = 0;
            int sumG = 0;
            int sumB = 0;
            //Begin checking pixel of interest and its borders
            for (int k = -1; k <= 1; k++)
            {
                //if index i+k exists
                if (i + k >= 0)
                {
                    for (int m = -1; m <= 1; m++)
                    {
                        //if index j+m exists
                        if (j + m >= 0)
                        {
                            //add the color terms to the sum
                            sumR += copy[i + k][j + m].rgbtRed;
                            sumG += copy[i + k][j + m].rgbtGreen;
                            sumB += copy[i + k][j + m].rgbtBlue;
                            //increment the number of terms
                            terms++;
                        }
                    }
                }
            }
            //END OF CHECKING PIXEL OF INTEREST AND ITS BORDERS
            //This is where I update the color of the original
            //image based on the average of the colors from
            image[i][j].rgbtRed = round((float) sumR / (float) terms);
            image[i][j].rgbtGreen = round((float) sumG / (float) terms);
            image[i][j].rgbtBlue = round((float) sumB / (float) terms);
        }
    return;
}
0

Solved the problem. I was not checking for all indexes that do not exist in the array, only for the negative indexes.

You must log in to answer this question.

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