# Blur only working with middle pixel

I've been stuck in this problem for a while, and the code only works when its inputted a middle pixel. Does anyone have a clue? I've searched everywhere for this particular issue, but I have only found different approaches, and I'd like to know what's missing from my code.

``````//Copy of image
typedef struct
{
int rgbtRed;
int rgbtGreen;
int rgbtBlue;
}
rgb;
rgb imagecpy[height][width];

//Iterate through all pixels
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int p = 0.0;
//Check for surrounding pixels
for (int y = i - 1; y <= i + 1; y++)
{
for (int x = j - 1; x <= j + 1; x++)
{
//Ignore if pixel out of bounds
if (y < 0 || y > height - 1)
{
continue;
}
else if (x < 0 || x > width - 1)
{
continue;
}
else if (x == j && y == i)
{
continue;
}
else
{
//Add all values if surrounding pixel valid
imagecpy[i][j].rgbtRed += image[y][x].rgbtRed;
imagecpy[i][j].rgbtGreen += image[y][x].rgbtGreen;
imagecpy[i][j].rgbtBlue += image[y][x].rgbtBlue;
++p;
}
}
}
//Average pixel values after going through all surrounding values
imagecpy[i][j].rgbtRed = round(imagecpy[i][j].rgbtRed / (float) p);
imagecpy[i][j].rgbtGreen = round(imagecpy[i][j].rgbtGreen / (float) p);
imagecpy[i][j].rgbtBlue = round(imagecpy[i][j].rgbtBlue / (float) p);
}
}
//Copy values into original image
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
image[i][j].rgbtRed = imagecpy[i][j].rgbtRed;
image[i][j].rgbtGreen = imagecpy[i][j].rgbtGreen;
image[i][j].rgbtBlue = imagecpy[i][j].rgbtBlue;
}
}
return;
``````

Also with corners I get errors like "expected "70 85 95\n", not "208 20 240\n"", where all the values are completely off. Any help is appreciated!

I believe your code literally just calculates the averages for the middle pixel ...like what happens when i = 0 and j = 0 (the top corner pixel image)?

• A part of the code checks for the surrounding pixels. For example, in the top left corner, 'y' and 'x' start as -1, and because those values are lower than 0, that pixel would be ignored, something similar is what happens with the other corners and edge cases. I hope I made myself clear Sep 23 '20 at 18:57

Finally got it to work! Instead of adding all the values to the imagecpy array, I added them into an independent set of floats for each value. Posting the edited correct code below!

``````    //Iterate through all pixels
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float avgred = 0.0;
float avggreen = 0.0;
float avgblue = 0.0;
float p = 0.0;
//Check for surrounding pixels
for (int y = i - 1; y <= i + 1; y++)
{
for (int x = j - 1; x <= j + 1; x++)
{
//Ignore if pixel out of bounds
if (y < 0 || y > height - 1)
{
break;
}
else if (x < 0 || x > width - 1)
{
continue;
}
else
{
//Add all values if surrounding pixel valid
avgred += image[y][x].rgbtRed;
avggreen += image[y][x].rgbtGreen;
avgblue += image[y][x].rgbtBlue;
++p;
}
}
}
//Average pixel values after going through all surrounding values
imagecpy[i][j].rgbtRed = round(avgred / p);
imagecpy[i][j].rgbtGreen = round(avggreen / p);
imagecpy[i][j].rgbtBlue = round(avgblue / p);
}
}
``````