简体   繁体   中英

CS50 Blur compiles but filter doesn't get applied on the picture

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM