0

I'm not sure exactly what is wrong with my code. I don't get any runtime errors and my code compiles, but the image doesn't change at all compared with the original, and check 50 tells me I am out - putting incorrect values.


void blur(int height, int width, RGBTRIPLE image[height][width])
{

    RGBTRIPLE blur[height][width];

    for (int i = 0; i < height; i++)
    {

        int a;
        int b;

        if (i == 0)
        {
            a = 0;
            b = 1;
        }
        else if (i == (height - 1))
        {
            a = 1;
            b = 0;
        }
        else
        {
            a = 1;
            b = 1;
        }

        for (int j = 0; j < width; j++)
        {

            int c;
            int d;

            if (j == 0)
            {
                c = 0;
                d = 1;
            }
            else if (j == (width - 1))
            {
                c = 1;
                d = 0;
            }
            else
            {
                c = 1;
                d = 1;
            }

            float blue = 0;
            float green = 0;
            float red = 0;

            float counter = 0;

            for(int x = i - a; x < i + b; x++)
            {
                for (int y = j - c; y < j + d; y++)
                {
                    blue += image[x][y].rgbtBlue;
                    green += image[x][y].rgbtGreen;
                    red += image[x][y].rgbtRed;
                    counter ++;
                }
            }

            int blurblue = round(blue / counter);
            int blurgreen = round(green / counter);
            int blurred = round(red / counter);


            blur[i][j].rgbtBlue = blurblue;
            blur[i][j].rgbtGreen = blurgreen;
            blur[i][j].rgbtRed = blurred;
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < height; j++)
        {
            image[i][j] = blur[i][j];
        }
    }

    return;
}

1 Answer 1

0

You have four 'for loops' in cascade. The first and the second I understand but the others I can´t get a grip at what you're trying to do. You named conditions 'a b c d' and you didn't add any comments in your code so it's very dificult for the people and even for yoursef to follow the logic.I recomend you to write your logic downn with pen and paper and when you have rethink it, writing your code should be much more easy. Also in your last 'for loops' the conditions for them to stop are the same. I don't think you meant to do that.

2
  • I definitely see how more comments could be useful, but unfortunately I did not add them at the beginning. The first two for loops are designed to iterate over each pixel in the image, while the latter 2 evaluate the pixels around the pixel in question, and average their values. This is saved to an array "blur", which is then copied into the initial array
    – michael
    Apr 20, 2020 at 16:44
  • I also changed my code so that the last two for loops are <= instead of less than. My code now blurs the image, but only 60% of it
    – michael
    Apr 20, 2020 at 16:47

You must log in to answer this question.

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