# Filter less, Blur, why does my code not blur the edge and 3x3 and 4x4 image?

I am stuck at the blur function in filter less. Your task is to calculate the average RGB value of the surrounding pixel in one row and one column of a bmg image, including the middle pixel. It works with the middle and the corner pixel but not on the edge and not the 3x3 and 4x4 image. I know that the code is very long and I intend to rewrite it more efficently but I want to understand where and why my code does not work properly. Thank you!

``````{
// Temp array
RGBTRIPLE temp[height][width];

// Copy array
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}

for (int i = 0; i < height; i++)
{

for (int j = 0; j < width; j++)
{
float tempred = 0;
float tempgreen = 0;
float tempblue = 0;
// First corner case top left
if (i == 0 && j == 0)
{
// :)
tempred = image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
tempred = round(tempred / 4.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
tempgreen = round(tempgreen / 4.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
tempblue = round(tempblue / 4.0);
temp[i][j].rgbtBlue = tempblue;

}

// Second corner top right
else if (i == 0 && j == width -1)
{
// :(
tempred = image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed;
tempred = round(tempred / 4.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen;
tempgreen = round(tempgreen / 4.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue;
tempblue = round(tempblue / 4.0);
temp[i][j].rgbtBlue = tempblue;
}
// Bottom left

else if (i == height - 1 && j == 0)
{

tempred = image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
tempred = round(tempred / 4.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
tempgreen = round(tempgreen / 4.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
tempblue = round(tempblue / 4.0);
temp[i][j].rgbtBlue = tempblue;
}

// bottom right
else if (i == height - 1 && j == width - 1)
{
tempred = image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed;
tempred = round(tempred / 4.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen;
tempgreen = round(tempgreen / 4.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue;
tempblue = round(tempblue / 4.0);
temp[i][j].rgbtBlue = tempblue;

}
else if (i == 0 && j > 0 && j < width)
{
// top row
tempred = image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
tempred = round(tempred / 6.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
tempgreen = round(tempgreen / 6.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
tempblue = round(tempblue / 6.0);
temp[i][j].rgbtBlue = tempblue;
}
else if (i > 0 && i < height && j == 0)
{
// left edge :(

tempred = image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
tempred = round(tempred / 6.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
tempgreen = round(tempgreen / 6.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
tempblue = round(tempblue / 6.0);
temp[i][j].rgbtBlue = tempblue;
}
// right edge :(
else if (i > 0 && i < height && j == width)
{

tempred = image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed;
tempred = round(tempred / 6.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen;
tempgreen = round(tempgreen / 6.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue;
tempblue = round(tempblue / 6.0);
temp[i][j].rgbtBlue = tempblue;
}

else
// Middle pixel :)
{

tempred = image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed;
tempred = round(tempred / 9.0);
temp[i][j].rgbtRed = tempred;

tempgreen = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen;
tempgreen = round(tempgreen / 9.0);
temp[i][j].rgbtGreen = tempgreen;

tempblue = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue;
tempblue = round(tempblue / 9.0);
temp[i][j].rgbtBlue = tempblue;
}
}
}

for (int i = 0; i < height; i++)
{
// loop for width
for (int j = 0; j < width; j++)
{
image[i][j] = temp[i][j];
}
}
return;
}
``````
``````:) blur correctly filters middle pixel
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "80 95 130\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

check50 log on 3x3 image
Expected Output:
70 85 95
80 95 105
90 105 115
117 130 140
127 140 149
137 150 159
163 178 188
170 185 194
178 193 201

Actual Output:
70 85 95
80 95 130
90 105 115
117 130 140
127 140 149
126 138 146
105 110 115
113 123 129
178 193 201
``````

So it looks like your program is doing most of the pixels correctly.

1~2~3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4~5~6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7~8~9

It's messing up on pixels 6, 7 , and 8. so the issues are with your right edge, bottom edge, and left bottom corner cases. It looks like your problem is one I ran into myself.

If you have x amount of slots in an array. Your array will go up to array[x - 1] because of zero-indexing.

Sorry if this doesn't help; I'm pretty green.

• Hi Kirsten, I had to delete my comment earlier, because I thought I already knew what you had pointed out, but I did not. It was the array indexing, thank you very much!!
– Karl
Aug 22, 2022 at 18:45
• Absolutely no problem. Happy coding! Aug 24, 2022 at 1:50