0

I have written code for the blur filter in problem set 4. The code compiles and runs fine and seems to blur the image but when I run check50 it says that the RGB values aren't correct. I have looked through my code several times but I still don't see where it is going wrong. Could someone please tell me where I am going wrong?

I have created a function called blurred which takes in the original image and the indices for the top left pixel of the box of pixels that are being averaged over and the number of rows and columns the box should be (which depends if it is in the corner, edge or middle). With 2 nested for loops it cycles through each RGB value and adds it to a sum. The sums are then divided by the number of pixels in the box.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE new_image[height][width];
    for (int i = 0; i < height; i++)
    {
        //Blur Corners
        for (int j = 0; j < width; j++)
        {
            if (i == 0 && j == 0)
            {
                new_image[i][j] = blurred(height, width, image, 0, 0, 2, 2);
            }
            else if (i == 0 && j == width - 1)
            {
                new_image[i][j] = blurred(height, width, image, 0, width - 2, 2, 2);
            }
            else if (i == height - 1 && j == 0)
            {
                new_image[i][j] = blurred(height, width, image, height - 2, 0, 2 , 2);
            }
            else if (i == height - 1 && j == width - 1)
            {
                new_image[i][j] = blurred(height, width, image, height - 2, width - 2, 2, 2);
            }
            
            //Blur edges
            else if (i == 0 && ((j != 0) || (j != width - 1)))
            {
                new_image[i][j] = blurred(height, width, image, 0, j - 1, 2, 3);
            }
            else if (i == height - 1 && ((j != 0) || (j != width - 1)))
            {
                new_image[i][j]  = blurred(height, width, image, height - 2, j - 1, 2, 3);
            }
            else if (j == 0 && ((i != 0) || (i != height - 1)))
            {
                 new_image[i][j] = blurred(height, width, image, i - 1, 0, 3, 2);
            }
            else if (j == width - 1 && ((i != 0) || (i != height - 1)))
            {
                new_image[i][j] = blurred(height, width, image, i - 1, width - 2, 3, 2);
            }
            
            //Blur middle
            else
            {
                new_image[i][j] = blurred(height, width, image, i - 1, j - 1, 3, 3);
            }
        }
    }
    image = new_image;
    return;
}

//Calculates blur values INPUT: (original image, (start index of top left pixel in box) k, m, no. rows, no. columns)
RGBTRIPLE blurred(int h, int w, RGBTRIPLE original[h][w], int k, int m, int rows, int columns)
{
    long sum_red = 0;
    long sum_green = 0;
    long sum_blue = 0;
    float sum_counter = 0;
    
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            sum_red += original[k + i][m + j].rgbtRed;
            sum_green += original[k + i][m + j].rgbtGreen;
            sum_blue += original[k + i][m + j].rgbtBlue;
            sum_counter++;
        }
    }
    
    
    RGBTRIPLE image_blurred;
    image_blurred.rgbtRed = round(sum_red / sum_counter);
    image_blurred.rgbtGreen = round(sum_green / sum_counter);
    image_blurred.rgbtBlue = round(sum_blue / sum_counter);
    
    return image_blurred;
}
1

If you look carefully at the extended results for check50 (the link at the end), you'd see that the result array is the same as the original input array.

The problem lies here:

image = new_image;

This doesn't do what you think. Instead, each element needs to be copied, using two nested for loops and this:

image[i][j]=new_image[i][j];

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

1
  • This worked, thanks! – charlotte Aug 17 '20 at 12:40

You must log in to answer this question.

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