0

Got stuck in Blur for more than a week and can´t find out what is wrong. I´ve tried many differents ways, but only corner values matches. Can anyone please give me a help about this.

// Blur image

void blur(int height, int width, RGBTRIPLE image[height][width]) { RGBTRIPLE new_image[height][width]; float red = 0; float blue = 0; float green = 0;

//copying image into a new file
for (int h = 0; h < height; h++)
{
    for (int w = 0; w < width; w++)
    {
        new_image[h][w] = image[h][w];
    }
}

//h and w loops to reach all pixels in height and width
for (int h = 0; h < height; h++)
{
    for (int w = 0; w < width; w++)
    {
        int count = 0;
        //loops i & j for reach the neighbour pixels
        for (int i = - 1; i < 2; i++)
        {
            for (int j = - 1; j < 2; j++)
            {
                //Condition to exclude out of edge pixels
                if((h + i >= 0) && (h + i < height) && (w + j >= 0) && (w + j < width))
                {
                    //count of valid pixels
                    count++;
                    //adding colors values to variable
                    red += image[h + i][w + j].rgbtRed;
                    blue += image[h + i][w + j].rgbtBlue;
                    green += image[h + i][w + j].rgbtGreen;


                    //coping colors values into a new image
                    new_image[h][w].rgbtRed = round(red / (float)count);
                    new_image[h][w].rgbtBlue = round(blue / (float)count);
                    new_image[h][w].rgbtGreen = round(green / (float)count);

                }
            }
        }
    }
}

//loop to copy the blured pixel into original image
for (int h = 0; h < height; h++)
{
    for (int w = 0; w < width; w++)
    {
        image[h][w].rgbtRed = new_image[h][w].rgbtRed;
        image[h][w].rgbtBlue = new_image[h][w].rgbtBlue;
        image[h][w].rgbtGreen = new_image[h][w].rgbtGreen;
    }
}

}

:( blur correctly filters middle pixel
expected "127 140 149\n", not "73 118 150\n"

:( blur correctly filters pixel on edge expected "80 95 105\n", not "127 152 168\n" :) blur correctly filters pixel in corner :( blur correctly filters 3x3 image expected "70 85 95\n80 9...", not "70 85 95\n127 ..." :( blur correctly filters 4x4 image expected "70 85 95\n80 9...", not "70 85 95\n127 ..."

0

Are you sure you want to do the average operation there? Inside those loops, it's dividing by 'count' every time it gets a value from the adjacent pixels.

| improve this answer | |
  • I put the division outside the loop (only in primary nested loops). It compiles, but the image doesn´t come at all. Returns an error (out of range) as shown below. Now I have some round or edge issues. Will work on that. Thanks for your help. helpers.c:154:41: runtime error: 287 is outside the range of representable values of type 'unsigned char' helpers.c:153:39: runtime error: 275 is outside the range of representable values of type 'unsigned char' helpers.c:155:40: runtime error: 269 is outside the range of representable values of type 'unsigned char' – Marcelo Jun 4 at 18:12
  • red, blue ad green variables are acumulating their values every loop. – Tritum Jun 4 at 18:45

You must log in to answer this question.

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