-2

I'm brand new to programming and I'm going through the CS50 course. Got stuck on Blur problem for weeks. The code compiles but the image doesn't change to blur. These are the errors

I don't really understand those numbers shown in the error message and what they mean, looks like the value of the pixel is not what it's supposed to be. I found a similar question on the platform but the code was written different than mine and the answer to it did not help me :( Thank you for your time!

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
// make copy of file
{
    RGBTRIPLE temp[height][width];

for (int row = 0; row < height; row++)
{
    for (int column = 0; column < width; column++)
    {
        temp[row][column] = image[row][column];
    }
}
    for (int row = 0; row < height; row++)
    {
        for (int column = 0; column < width; column++)
        {
           int totalRed, totalGreen, totalBlue;
            totalRed = totalGreen = totalBlue = 0;
            float counter = 0.00;


            //find neighbour pixels

            for ( int i = -1; i < 2; i++)
            {
                for (int j = -1; j < 2; j++)
                {
                  int currentRow = height + row;
                  int currentColumn = width + column;

                    //exclude invalid pixels

                      if (currentRow < 0 || currentRow > (height - 1) || currentColumn < 0 || currentColumn > (width - 1))
                       {
                        continue;
                       }

                       // adding the total value of the valid neighbouring pixels
                       totalRed += image[currentRow][currentColumn].rgbtRed;
                       totalGreen += image[currentRow][currentColumn].rgbtGreen;
                       totalBlue += image[currentRow][currentColumn].rgbtBlue;

                       counter++;

                }
                //calculate average of sorrounding pixels
          temp[height][width].rgbtRed = round(totalRed / counter);
          temp[height][width].rgbtGreen= round(totalGreen / counter);
          temp[height][width].rgbtBlue= round(totalBlue / counter);

            }
        }
    }
 // copy temp values in original file
    for (int row = 0; row < height; row++)
    {
        for (int column = 0; column < width; column++)
        {
            image[row][column].rgbtRed = temp[row][column].rgbtRed;
            image[row][column].rgbtGreen = temp[row][column].rgbtGreen;
            image[row][column].rgbtBlue = temp[row][column].rgbtBlue;
        }
    }

    return;
}
  • 2
    Please [don't post images of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). Copy-paste any and all text *as text* into your questions. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then learn how to [edit] your questions to improve them. – Some programmer dude Dec 08 '22 at 07:29
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 08 '22 at 11:57
  • @Someprogrammerdude my apologies, I edited the post and added the code as text. Thank you! – wakulovszki Dec 09 '22 at 18:58

1 Answers1

0

I'm guessing the problem is with these lines:

for ( int i = -1; i < 2; i++)
{
    for (int j = -1; j < 2; j++)
    {
        int currentRow = height + row;
        int currentColumn = width + column;

Here you set currentRow to the full height of the image, and then add the current row index. It will always be outside of the image. Same with the column.

What I'm guessing you're supposed to do is:

int currentRow = row + i;
int currentColumn = column + j;

This will get the pixels around the current row and column and use for the blurring.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you I tried this but it gives me the same error :( – wakulovszki Dec 13 '22 at 22:13
  • @wakulovszki Seems it's time to go into your [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code line by line while monitoring variables and their values to see what's really happening. – Some programmer dude Dec 13 '22 at 22:54