# CS50 blur filter failed 3x3 and 4x4

I created a for loop checking each pixel around the one to be blurred. If the pixel was in range, its RGB values would be added to a total variable to then be averaged. My program works for all requirements except the 3x3 and 4x4 images. Here are my results: https://submit.cs50.io/check50/b0fce8b4cfdbb28d3a22b6f7c93c885a8fc60b8a

I'm not sure why it successfully filters all the pixels in an image but 3x3 and 4x4. I tried playing with some of the if statements to make it `< width` and `< height` instead of `<= width` and `<= height` but that still didn't work. I don't know what else to try.

Here is my code:

``````// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE new_image[height][width];

// fully duplicate image to avoid blurring already blurred pixels
for (int i = 0; i < height; i++)
{
for (int x = 0; x < width; x++)
{
new_image[i][x].rgbtRed = image[i][x].rgbtRed;
new_image[i][x].rgbtGreen = image[i][x].rgbtGreen;
new_image[i][x].rgbtBlue = image[i][x].rgbtBlue;
}
}

for (int i = 0; i < height; i++)
{
for (int x = 0; x < width; x++)
{
/*           center        bottom middle   top middle      center right      center left       bottom right          top left              bottom left           top right
totalColor = image[i][x] + image[i+1][x] + image[i-1][x] + image[i][x + 1] + image[i][x - 1] + image[i + 1][x + 1] + image[i - 1][x - 1] + image[i + 1][x - 1] + image[i - 1][x + 1]
*/
int pixels = 1;
int totalRed = 0, totalGreen = 0, totalBlue = 0;

// top left
if (i - 1 >= 0 && x - 1 >= 0)
{
pixels++;
totalRed += new_image[i - 1][x - 1].rgbtRed;
totalGreen += new_image[i - 1][x - 1].rgbtGreen;
totalBlue += new_image[i - 1][x - 1].rgbtBlue;
}
// top middle
if (i - 1 >= 0)
{
pixels++;
totalRed += new_image[i - 1][x].rgbtRed;
totalGreen += new_image[i - 1][x].rgbtGreen;
totalBlue += new_image[i - 1][x].rgbtBlue;
}
// top right
if (i - 1 >= 0 && x + 1 <= width)
{
pixels++;
totalRed += new_image[i - 1][x + 1].rgbtRed;
totalGreen += new_image[i - 1][x + 1].rgbtGreen;
totalBlue += new_image[i - 1][x + 1].rgbtBlue;
}
// center left
if (x - 1 >= 0)
{
pixels++;
totalRed += new_image[i][x - 1].rgbtRed;
totalGreen += new_image[i][x - 1].rgbtGreen;
totalBlue += new_image[i][x - 1].rgbtBlue;
}
// center right
if (x + 1 <= width)
{
pixels++;
totalRed += new_image[i][x + 1].rgbtRed;
totalGreen += new_image[i][x + 1].rgbtGreen;
totalBlue += new_image[i][x + 1].rgbtBlue;
}
// bottom left
if (i + 1 <= height && x - 1 >= 0)
{
pixels++;
totalRed += new_image[i + 1][x - 1].rgbtRed;
totalGreen += new_image[i + 1][x - 1].rgbtGreen;
totalBlue += new_image[i + 1][x - 1].rgbtBlue;
}
// bottom middle
if (i + 1 <= height)
{
pixels++;
totalRed += new_image[i + 1][x].rgbtRed;
totalGreen += new_image[i + 1][x].rgbtGreen;
totalBlue += new_image[i + 1][x].rgbtBlue;
}
// bottom right
if (i + 1 <= height && x + 1 <= width)
{
pixels++;
totalRed += new_image[i + 1][x + 1].rgbtRed;
totalGreen += new_image[i + 1][x + 1].rgbtGreen;
totalBlue += new_image[i + 1][x + 1].rgbtBlue;
}

image[i][x].rgbtRed = round((float)(totalRed + new_image[i][x].rgbtRed) / pixels);
image[i][x].rgbtGreen = round((float)(totalGreen + new_image[i][x].rgbtGreen) / pixels);
image[i][x].rgbtBlue = round((float)(totalBlue + new_image[i][x].rgbtBlue) / pixels);
}
}
return;
}```
``````

Very interesting! Did you notice that in the two failing cases, all of the pixels on the right and bottom edges are incorrect?

So, that says there's something wrong with the code that handles those two edges.

Now, look at the relevant code for the lower right pixel. Remember that an array declared as `array` has valid elements from 0 to 9, not 10.

``````        // bottom right
if (i + 1 <= height && x + 1 <= width)
``````

Next, think about the indices for the lower right pixel. It would be `new_image[height - 1][width - 1]` So, i+1 can == height and x+1 can == width, are the indices still within the array, or is the code accessing memory that it shouldn't?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance, ;-)

• Hello, I'm not exactly sure what you mean. I changed my bottom right to be `if (i + 1 == height - 1 && x + 1 == width - 1)` as well as for the center right to be `if (x + 1 == width - 1)` but then check50 said only the middle pixels were blurred correctly. Can you clarify if I'm not understanding something?
– Amie
Jul 17, 2021 at 1:45
• if (i + 1 < height && x + 1 < width) Jul 17, 2021 at 2:02
• Hm still no luck. Thank you for identifying the bottom and right though. I'll keep looking at those.
– Amie
Jul 17, 2021 at 2:17
• You did remember to make the similar changes on all the right side and bottom pixel if statements, right? Jul 17, 2021 at 2:20
• Yes. Would the bottom left be `if (i + 1 < height && x - 1 >= 0)` or `if (i + 1 < height && x - 1 > 0)`.
– Amie
Jul 17, 2021 at 2:23
{"ft_lang_label":"__label__en","ft_lang_prob":0.57131433,"math_prob":0.9983303,"size":3225,"snap":"2023-14-2023-23","text_gpt3_token_len":1154,"char_repetition_ratio":0.26358274,"word_repetition_ratio":0.29313543,"special_character_ratio":0.39534885,"punctuation_ratio":0.16558442,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9935568,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-25T04:39:08Z\",\"WARC-Record-ID\":\"<urn:uuid:60f664a3-4487-4d02-a2c4-667e84a323dd>\",\"Content-Length\":\"111851\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:068d94ec-b150-462d-b53d-c52386ae3070>\",\"WARC-Concurrent-To\":\"<urn:uuid:73d85b7a-e8ca-4fa1-b35c-e3d3773adc70>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://cs50.stackexchange.com/questions/41678/cs50-blur-filter-failed-3x3-and-4x4\",\"WARC-Payload-Digest\":\"sha1:57K7TAFLMOVZMVTWSYPRZHV5N4E5URVY\",\"WARC-Block-Digest\":\"sha1:6NHYEVQPA3SM26YRKKYL4BT7GPJYV3SE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945315.31_warc_CC-MAIN-20230325033306-20230325063306-00129.warc.gz\"}"}