0

I have written the following code for blur .

    // Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    for ( int h=0 ; h<height ; h++) 
    {
        for ( int w=0 ; w<width ; w++)
        {

            if(h==0 )
            {
                if(w==0)
                {
                    image[h][w].rgbtBlue=round((image[h][w].rgbtBlue+ image[h][w+1].rgbtBlue+image[h+1][w].rgbtBlue+image[h+1][w+1].rgbtBlue)/4.000);
                    image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w+1].rgbtGreen+image[h+1][w].rgbtGreen+image[h+1][w+1].rgbtGreen)/4.000);
                    image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w+1].rgbtRed+image[h+1][w].rgbtRed+image[h+1][w+1].rgbtRed)/4.000);
                }
                else if(w==width-1)
                {
                    image[h][w].rgbtBlue=round((image[h][w].rgbtBlue+ image[h][w-1].rgbtBlue+image[h+1][w].rgbtBlue+image[h+1][w-1].rgbtBlue)/4.000);
                    image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w-1].rgbtGreen+image[h+1][w].rgbtGreen+image[h+1][w-1].rgbtGreen)/4.000);
                    image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w-1].rgbtRed+image[h+1][w].rgbtRed+image[h+1][w-1].rgbtRed)/4.000);

                }
                else if (0<w && w<width-1)
                       {
                        image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w+1].rgbtRed+image[h+1][w].rgbtRed+image[h+1][w+1].rgbtRed+image[h][w-1].rgbtRed+image[h+1][w-1].rgbtRed)/6.000);
                        image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w+1].rgbtGreen+image[h+1][w].rgbtGreen+image[h+1][w+1].rgbtGreen+image[h][w-1].rgbtGreen+image[h+1][w-1].rgbtGreen)/6.000);
                        image[h][w].rgbtBlue = round((image[h][w].rgbtBlue+ image[h][w+1].rgbtBlue+image[h+1][w].rgbtBlue+image[h+1][w+1].rgbtBlue+image[h][w-1].rgbtBlue+image[h+1][w-1].rgbtBlue)/6.000);
                       }

            }

                else if (0<h && h<height-1)
            {
                if (w==0)
                {
                    image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w+1].rgbtRed+image[h+1][w].rgbtRed+image[h+1][w+1].rgbtRed+image[h-1][w].rgbtRed+image[h-1][w+1].rgbtRed)/6.000);
                    image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w+1].rgbtGreen+image[h+1][w].rgbtGreen+image[h+1][w+1].rgbtGreen+image[h-1][w].rgbtGreen+image[h-1][w+1].rgbtGreen)/6.000);
                    image[h][w].rgbtBlue = round((image[h][w].rgbtBlue+ image[h][w+1].rgbtBlue+image[h+1][w].rgbtBlue+image[h+1][w+1].rgbtBlue+image[h-1][w].rgbtBlue+image[h-1][w+1].rgbtBlue)/6.000);
                }
                if (w==width-1)
                {
                    image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w-1].rgbtRed+image[h+1][w].rgbtRed+image[h+1][w-1].rgbtRed+image[h-1][w].rgbtRed+image[h-1][w-1].rgbtRed)/6.000);
                    image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w-1].rgbtGreen+image[h+1][w].rgbtGreen+image[h+1][w-1].rgbtGreen+image[h-1][w].rgbtGreen+image[h-1][w-1].rgbtGreen)/6.000);
                    image[h][w].rgbtBlue = round((image[h][w].rgbtBlue+ image[h][w-1].rgbtBlue+image[h+1][w].rgbtBlue+image[h+1][w-1].rgbtBlue+image[h-1][w].rgbtBlue+image[h-1][w-1].rgbtBlue)/6.000);

                }

                else if (0<w && w<width-1)
                { 
                        image[h][w].rgbtRed = round((image[h-1][w].rgbtRed+ image[h-1][w-1].rgbtRed+ image[h-1][w+1].rgbtRed+ image[h][w-1].rgbtRed+ image[h][w].rgbtRed+ image[h][w+1].rgbtRed+ image[h+1][w].rgbtRed+image[h+1][w-1].rgbtRed+image[h+1][w+1].rgbtRed)/9.000);
                        image[h][w].rgbtGreen = round((image[h-1][w].rgbtGreen+ image[h-1][w-1].rgbtGreen+ image[h-1][w+1].rgbtGreen+ image[h][w-1].rgbtGreen+ image[h][w].rgbtGreen+ image[h][w+1].rgbtGreen+ image[h+1][w].rgbtGreen+image[h+1][w-1].rgbtGreen+image[h+1][w+1].rgbtGreen)/9.000);
                        image[h][w].rgbtBlue = round((image[h-1][w].rgbtBlue+ image[h-1][w-1].rgbtBlue+ image[h-1][w+1].rgbtBlue+ image[h][w-1].rgbtBlue+ image[h][w].rgbtBlue+ image[h][w+1].rgbtBlue+ image[h+1][w].rgbtBlue+image[h+1][w-1].rgbtBlue+image[h+1][w+1].rgbtBlue)/9.000);


                }
            }

            else if (h==height-1 )
            {
                if (w==0)
                {
                    image[h][w].rgbtBlue=round((image[h][w].rgbtBlue+ image[h][w+1].rgbtBlue+image[h-1][w].rgbtBlue+image[h-1][w+1].rgbtBlue)/4.000);
                    image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w+1].rgbtGreen+image[h-1][w].rgbtGreen+image[h-1][w+1].rgbtGreen)/4.000);
                    image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w+1].rgbtRed+image[h-1][w].rgbtRed+image[h-1][w+1].rgbtRed)/4.000);

                }
                else if (w==width-1)
                {
                    image[h][w].rgbtBlue=round((image[h][w].rgbtBlue+ image[h][w-1].rgbtBlue+image[h-1][w].rgbtBlue+image[h-1][w-1].rgbtBlue)/4.000);
                    image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w-1].rgbtGreen+image[h-1][w].rgbtGreen+image[h-1][w-1].rgbtGreen)/4.000);
                    image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w-1].rgbtRed+image[h-1][w].rgbtRed+image[h-1][w-1].rgbtRed)/4.000);

                }
                else if (0<w && w<width-1)
                        {
                        image[h][w].rgbtRed = round((image[h][w].rgbtRed+ image[h][w+1].rgbtRed+image[h-1][w].rgbtRed+image[h-1][w+1].rgbtRed+image[h][w-1].rgbtRed+image[h-1][w-1].rgbtRed)/6.000);
                        image[h][w].rgbtGreen = round((image[h][w].rgbtGreen+ image[h][w+1].rgbtGreen+image[h-1][w].rgbtGreen+image[h-1][w+1].rgbtGreen+image[h][w-1].rgbtGreen+image[h-1][w-1].rgbtGreen)/6.000);
                        image[h][w].rgbtBlue = round((image[h][w].rgbtBlue+ image[h][w+1].rgbtBlue+image[h-1][w].rgbtBlue+image[h-1][w+1].rgbtBlue+image[h][w-1].rgbtBlue+image[h-1][w-1].rgbtBlue)/6.000);
                        }
            }


        }
    }
    return;
}

It compiles fine and gives a bluirred image but when I checked it by check50 . It shows following error - These are the errors .error

I want to know what is wrong with that code . If anyone can help me . Any help would be very appreciated .

0

I'm no expert but I think what you're missing here is that every pixel's "blurred" RGB values have to be based on the original colour values for the pixels that surround it. Which is to say that you can't directly change the RGB values for every pixel as you traverse it, or you are altering the "original" value, which still needs to be used by other pixels, and therefore you're shifting the average for every subsequent pixel. (For context, Brian stresses the word 'original' a few times in the walkthrough, which is what made me realise this.)

This is why you're getting the right result for only your first pixel (the top left corner) in check50, because your code works, but by changing the RGB values directly this pixel skews the result for the next pixel (and so on and so on).

Instead, I think you have to store the "blurred" RGB values (in a new array, for example) and then write them to the original image file at the end, only after all the new "blurred" RGB codes have been calculated and stored.

| improve this answer | |
  • I tried creating RGBTRIPLE temp[height][width]; . but it didn`t work . – MWD Jun 6 at 14:40

You must log in to answer this question.

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