0

I'm so close to solving this problem and I'm only one pixel short of getting it thorugh, but I cannot for the life of me figure out what I'm doing wrong. Really hope some (one) of you can spot the problem in my code. I'll happily provide you with more information for questions you might have.

UPDATE: I figured out that it is every pixel to the right that is calculated wrong. So in the 3x3 it is 1x3, 2x3, and 3x3 and in the 4x4 it is the 1x4, 2x4, etc. I still do not know why this is though, so any help is appreciated! :)

enter image description here

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE tempArray[height][width];
    //Main loop to get through rows and columns
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float red = 0.0;
            float green = 0.0;
            float blue = 0.0;
            //Gets color for current pixel.
            red = image[i][j].rgbtRed;
            green = image[i][j].rgbtGreen;
            blue = image[i][j].rgbtBlue;



            int culminator = 1;
            //Each of the below if statements get the pixel in realtion to the current
            //pixel as suggested by the variables name. Each you pass through it
            //you add to the average as well as add 1 to the counter so you know how
            //much you have to divide with to get the average.
            if (i > 0)
            {
                //printf("Top\n");
                red += image[i - 1][j].rgbtRed;
                green += image[i - 1][j].rgbtGreen;
                blue += image[i - 1][j].rgbtBlue;


                culminator++;
            }

            if (i > 0 && j < (width - 1))
            {
                //printf("Top Right\n");
                red += image[i - 1][j + 1].rgbtRed;
                green += image[i - 1][j + 1].rgbtGreen;
                blue += image[i - 1][j + 1].rgbtBlue;


                culminator++;

            }

            if (i > 0 && j > 0)
            {
                //printf("Top Left\n");
                red += image[i - 1][j - 1].rgbtRed;
                green += image[i - 1][j - 1].rgbtGreen;
                blue += image[i - 1][j - 1].rgbtBlue;

                culminator++;

            }

            if (j < (width - 1))
            {
                //printf("Right\n");
                red += image[i][j + 1].rgbtRed;
                green += image[i][j + 1].rgbtGreen;
                blue += image[i][j + 1].rgbtBlue;

                culminator++;

            }

            if (j > 0)
            {
                //printf("Left\n");
                red += image[i][j - 1].rgbtRed;
                green += image[i][j - 1].rgbtGreen;
                blue += image[i][j - 1].rgbtBlue;

                culminator++;

            }

            if (i < (height - 1))
            {
                //printf("Low\n");
                red += image[i + 1][j].rgbtRed;
                green += image[i + 1][j].rgbtGreen;
                blue += image[i + 1][j].rgbtBlue;


                culminator++;

            }

            if (i < (height - 1) && j < (width - 1))
            {
                //printf("Low Right\n");
                red += image[i + 1][j + 1].rgbtRed;
                green += image[i + 1][j + 1].rgbtGreen;
                blue += image[i + 1][j + 1].rgbtBlue;


                culminator++;

            }

            if (i < (height - 1) && (j < (width - 1) && j > 0))
            {
                //printf("Low Left\n");
                red += image[i + 1][j - 1].rgbtRed;
                green += image[i + 1][j - 1].rgbtGreen;
                blue += image[i + 1][j - 1].rgbtBlue;


                culminator++;

            }

            //Divides the total and assign new variable to the current pixel.
            red /= culminator;
            if (red > 255)
            {
                red = 255;
            }
            tempArray[i][j].rgbtRed = round(red);

            green /= culminator;
            if (green > 255)
            {
                green = 255;
            }
            tempArray[i][j].rgbtGreen = round(green);

            blue /= culminator;
            if (blue > 255)
            {
                blue = 255;
            }
            tempArray[i][j].rgbtBlue = round(blue);



        }
    }
    //copying over from tempArray to the original image array.
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = tempArray[i][j].rgbtRed;
            image[i][j].rgbtGreen = tempArray[i][j].rgbtGreen;
            image[i][j].rgbtBlue = tempArray[i][j].rgbtBlue;
        }
    }
    return;
}
0

I figured it out. After drawing the grid on a piece of paper I found out that it didn't take the pixel [i - 1][j - 1] into consideration when the current pixel was on the far right, except from when it was the last pixel.

The if condition ended out being if (i < (height - 1) && j > 0). Hope this might help someone.

| improve this answer | |
  • Hey can you explain what you did wrong a little more? Would really help. – Burhan Mustafa Mar 1 at 17:17
  • So what I essentially did wrong was that one of the ifstatements didn't have the correct logic. What this means, is that the pixel this if statement was supposed to get a value from to calculate the average for the current pixel (image[i][j].rgbt... ) was completely ignored which gave me the wrong values. Does that explain it? – yoyohoho Mar 2 at 11:40

You must log in to answer this question.

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