0

I can't find any mistakes in my code:

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

{

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        float a = image[i][j].rgbtRed;
        float b = image[i][j].rgbtBlue;
        float c = image[i][j].rgbtGreen;
        float t = 1.0;
        //Red
        float t1[8];
        //Blue
        float t2[8];
        //Green
        float t3[8];
        if (i - 1 >= 0 && j - 1 >= 0)
        {
            t1[0] = image[i - 1][j - 1].rgbtRed;
            t2[0] = image[i - 1][j - 1].rgbtBlue;
            t3[0] = image[i - 1][j - 1].rgbtGreen;
            t++;
        }
        else
        {
            t1[0] = 0;
            t2[0] = 0;
            t3[0] = 0;
        }
        if (i - 1 >= 0)
        {
            t1[1] = image[i - 1][j].rgbtRed;
            t2[1] = image[i - 1][j].rgbtBlue;
            t3[1] = image[i - 1][j].rgbtGreen;
            t++;
        }
        else
        {
            t1[1] = 0;
            t2[1] = 0;
            t3[1] = 0;
        }
        if (i - 1 >= 0 && j + 1 < width)
        {
            t1[2] = image[i - 1][j + 1].rgbtRed;
            t2[2] = image[i - 1][j + 1].rgbtBlue;
            t3[2] = image[i - 1][j + 1].rgbtGreen;
            t++;
        }
        else
        {
            t1[2] = 0;
            t2[2] = 0;
            t3[2] = 0;
        }
        if (j - 1 >= 0)
        {
            t1[3] = image[i][j - 1].rgbtRed;
            t2[3] = image[i][j - 1].rgbtBlue;
            t3[3] = image[i][j - 1].rgbtGreen;
            t++;
        }
        else
        {
            t1[3] = 0;
            t2[3] = 0;
            t3[3] = 0;
        }
        if (j + 1 < width)
        {
            t1[4] = image[i][j + 1].rgbtRed;
            t2[4] = image[i][j + 1].rgbtBlue;
            t3[4] = image[i][j + 1].rgbtGreen;
            t++;
        }
        else
        {
            t1[4] = 0;
            t2[4] = 0;
            t3[4] = 0;
        }
        if (i + 1 < height && j - 1 >= 0)
        {
            t1[5] = image[i + 1][j - 1].rgbtRed;
            t2[5] = image[i + 1][j - 1].rgbtBlue;
            t3[5] = image[i + 1][j - 1].rgbtGreen;
            t++;
        }
        else
        {
            t1[5] = 0;
            t2[5] = 0;
            t3[5] = 0;
        }
        if (i + 1 < height)
        {
            t1[6] = image[i + 1][j].rgbtRed;
            t2[6] = image[i + 1][j].rgbtBlue;
            t3[6] = image[i + 1][j].rgbtGreen;
            t++;
        }
        else
        {
            t1[6] = 0;
            t2[6] = 0;
            t3[6] = 0;
        }
        if (i + 1 < height && j + 1 < width)
        {
            t1[7] = image[i + 1][j + 1].rgbtRed;
            t2[7] = image[i + 1][j + 1].rgbtBlue;
            t3[7] = image[i + 1][j + 1].rgbtGreen;
            t++;
        }
        else
        {
            t1[7] = 0;
            t2[7] = 0;
            t3[7] = 0;
        }
        image[i][j].rgbtRed = round((a + t1[0] + t1[1] + t1[2] + t1[3] + t1[4] + t1[5] + t1[6] + t1[7]) / t);
        image[i][j].rgbtBlue = round((b + t2[0] + t2[1] + t2[2] + t2[3] + t2[4] + t2[5] + t2[6] + t2[7]) / t);
        image[i][j].rgbtGreen = round((c + t3[0] + t3[1] + t3[2] + t3[3] + t3[4] + t3[5] + t3[6] + t3[7]) / t);
    }
}
return;

On using check50, only corner pixel is blurred correctly! Can someone please point out if i' ve done something wrong?

0

One obvious mistake: You overwrite values which you still need for computing other pixels' averages.

Simplest way would be to use separate arrays for input and output.

3
  • But i am assigning the values to the arrays I've declared. – Musical_Ant Mar 24 '20 at 5:00
  • 1
    But then you write to image and read from there for the next pixel, using old and new values. – Blauelf Mar 24 '20 at 6:46
  • I got what you were trying to say, i just declared another set of arrays, and set the value of output array through another loop, thanks..... – Musical_Ant Mar 24 '20 at 7:06

You must log in to answer this question.

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