# CS50 Blur Problems

``````:) blur correctly filters middle pixel

:) blur correctly filters pixel on edge

:) 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..."
``````

code:

``````// Blur image

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

{

RGBTRIPLE copy[height][width];
//Temporary copy of the array
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
copy[i][j].rgbtRed = image[i][j].rgbtRed;
copy[i][j].rgbtGreen = image[i][j].rgbtGreen;
copy[i][j].rgbtBlue = image[i][j].rgbtBlue;
}
}
for (int i = 0; i <= height - 1; i++)
{
for (int j = 0; j <= width - 1; j++)
{
//Top Left Corner This Working good
if (i == 0 && j == 0)
{
float red = copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 4);
red = round(red / 4);
green = round(green / 4);
copy[i][j].rgbtBlue = blue;
copy[i][j].rgbtGreen = green;
copy[i][j].rgbtRed = red;
}
//Top Right Corner This Working good
else if (i == 0 && j == width - 1)
{
float red = copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j - 1].rgbtGreen + copy[i+ 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 4);
red = round(red / 4);
green = round(green / 4);
copy[i][j].rgbtBlue = blue;
copy[i][j].rgbtGreen = green;
copy[i][j].rgbtRed = red;
}
//Bottom Left Corner This Working good
else if (j == 0 && i == height - 1)
{
float red = copy[i][j + 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j + 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j + 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 4);
red = round(red / 4);
green = round(green / 4);
copy[i][j].rgbtBlue = blue;
copy[i][j].rgbtGreen = green;
copy[i][j].rgbtRed = red;
}
//Bottom Right Corner This Working good
else if (j == width - 1 && i == height - 1)
{
float red = copy[i][j - 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j - 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j - 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 4);
red = round(red / 4);
green = round(green / 4);
copy[i][j].rgbtBlue = blue;
copy[i][j].rgbtGreen = green;
copy[i][j].rgbtRed = red;
}
//Remaining Array
//There's something wrong with this
else if (i != 0 && i != height - 1 && j != 0 && j != width - 1)
{
float red = copy[i][j + 1].rgbtRed + copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j + 1].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j + 1].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 9);
red = round(red / 9);
green = round(green / 9);
//I put "-17" and "-16" to make it work but it's wrong
copy[i][j].rgbtBlue = blue - 17;
copy[i][j].rgbtGreen = green - 17;
copy[i][j].rgbtRed = red - 16;
}
//Top Row
//There's something wrong with this
else if (i == 0 && j != 0 && j != width - 1)
{
float red = copy[i][j + 1].rgbtRed + copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j + 1].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j + 1].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 6);
red = round(red / 6);
green = round(green / 6);
//I put "-11" and "-10" to make it work but it's wrong
copy[i][j].rgbtBlue = blue - 11;
copy[i][j].rgbtGreen = green - 11;
copy[i][j].rgbtRed = red - 10;
}
//Left Column
//There's something wrong with this
else if (j == 0 && i != 0 && i != height - 1)
{
float red = copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 6);
red = round(red / 6);
green = round(green / 6);
//I put "-11" and "-10" to make it work but it's wrong
copy[i][j].rgbtBlue = blue - 11;
copy[i][j].rgbtGreen = green - 11;
copy[i][j].rgbtRed = red - 10;
}
//Right Column
//There's something wrong with this
else if (j == width - 1 && i != 0 && i != height - 1)
{
float red = copy[i][j - 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j - 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j - 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 6);
red = round(red / 6);
green = round(green / 6);
//I put "-11" and "-10" to make it work but it's wrong
copy[i][j].rgbtBlue = blue - 11;
copy[i][j].rgbtGreen = green - 11;
copy[i][j].rgbtRed = red - 10;
}
//Bottom Row
//There's something wrong with this
else if (i == height - 1 && j != 0 && j != width - 1)
{
float red = copy[i][j + 1].rgbtRed + copy[i][j - 1].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j].rgbtRed;
float blue = copy[i][j + 1].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j].rgbtBlue;
float green = copy[i][j + 1].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j].rgbtGreen;
blue = round(blue / 6);
red = round(red / 6);
green = round(green / 6);
//I put "-11" and "-10" to make it work but it's wrong
copy[i][j].rgbtBlue = blue - 11;
copy[i][j].rgbtGreen = green - 11;
copy[i][j].rgbtRed = red - 10;
}
}

}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = copy[i][j];

}
}
return;
}
``````