0

So basically what I did was, first I started off with the row above and then the relevant row and then the row below. I created a for loop which would run across the row 3 times.

My code looks like this, After editing once again,

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    float temp[height][width][3];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            long sumRed = 0, sumBlue = 0, sumGreen = 0;
            int pixCount = 0;
            //top row
            for (int k = j - 1; k <= j + 1; k++)
            {
                if (i - 1 >= 0 && (k >= 0 && k < width))
                {
                    sumRed += (long)image[i-1][k].rgbtRed;
                    sumBlue += (long)image[i-1][k].rgbtBlue;
                    sumGreen += (long)image[i-1][k].rgbtGreen;
                    pixCount++;
                }
            }
           //relevant row
            for (int k = j - 1; k <= j + 1; k++)
            {
                 if (i >= 0 && (k >=0 && k < width))
                {
                    sumRed += (long)image[i][k].rgbtRed;
                    sumBlue += (long)image[i][k].rgbtBlue;
                    sumGreen += (long)image[i][k].rgbtGreen;
                    pixCount++;
                }
            }
            //bottom row
            for (int k = j - 1; k <= j + 1; k++)
            {
                 if (i + 1 < height && (k >= 0 && k < width))
                {
                    sumRed += (long)image[i+1][k].rgbtRed;
                    sumBlue += (long)image[i+1][k].rgbtBlue;
                    sumGreen += (long)image[i+1][k].rgbtGreen;
                    pixCount++;
                }
            }
            temp[i][j][0] = round(sumRed / pixCount);
            temp[i][j][1] = round(sumBlue / pixCount);
            temp[i][j][2] = round(sumGreen / pixCount);
        }
    }
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = temp[i][j][0];
            image[i][j].rgbtBlue = temp[i][j][1];
            image[i][j].rgbtGreen = temp[i][j][2];
        }
    }
    return;
}

And then when I run check 50 I get some errors.

enter image description here

So basically What I think happens there is the values doesn't get rounded off correctly. It misses by a slight error. Can someone help me. Thank you so much.

1 Answer 1

1

When you divided here: image[i][j].rgbtRed = (sumRed / pixCount); you'll problably get a decimal point result. The problem is that working with int variables will result in a truncated result, the decimal point will be erased, no mather if it's ,001 or ,999. you have to think a way to get around it.

Also you have to think that when taking the values from the surroundings pixels, some of them will be already changed so the result will be diferent than with the original ones. Think in how you could solve that.

Lastly, in your width loops your condition to stop is when x < with-1. Why is that?

2
  • Yes, I really don't know why I went with width -1. lol. I fixed that. So then now I have to create a copy of all the pixels. Alright thank you. I'll keep you updated Commented Apr 28, 2020 at 12:05
  • I changed it again. Can you take a look again Commented Apr 28, 2020 at 12:49

You must log in to answer this question.

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