Search code examples
cpixelcs50gaussianblur

CS50 pset4 filter blur function turns the whole image to one color


The blur function is the implementation of a box blur algorithm that works by taking each pixel and, for each color value, giving it a new value by averaging the color values of neighboring pixels. Trying to understand this problem took me a whole day and a lot of frustration. I am not sure why the image does not blur but changes the whole to one color.

// Blur image
    void blur(int height, int width, RGBTRIPLE image[height][width])
    {
    
      RGBTRIPLE temp[height][width];
    
    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 count = 0;
            float red = 0, green = 0, blue = 0;
    
           // for row-1,row,row+1
           //for col-1,col.col+1
    
            for(int r = -1; r <= 2; r++)
            {
                for (int c = -1; c < 2; c++)
                {
                    if(r >= 0 && r < height && c >= 0 && c < width)
                    {
                        red +=   temp[r][c].rgbtRed;
                        green += temp[r][c].rgbtGreen;
                        blue +=  temp[r][c].rgbtBlue;
                        count++;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
    
             image[i][j].rgbtRed = round(red/count);
             image[i][j].rgbtGreen = round(green/count);
             image[i][j].rgbtBlue = round(blue/count);
    
    
    
        }
    
    }
    
    
     return;
    
    }

Solution

  • I figured out the answer. I did not add i th and j th index to the row(r) and column(c) while calculating red, green and blue where I was not able to move to neighbouring rows and columns.

    // Blur image
    void blur(int height, int width, RGBTRIPLE image[height][width])
    {
    
      RGBTRIPLE temp[height][width];
    
    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 count = 0;
            float red = 0, green = 0, blue = 0;
    
           // for row-1,row,row+1
           //for col-1,col.col+1
    
            for(int r = -1; r <= 2; r++)
            {
                for (int c = -1; c < 2; c++)
                {
                    if(r >= 0 && r < height && c >= 0 && c < width)
                    {
                        red += temp[i+r][j+c].rgbtRed;
                        green += temp[i+r][j+c].rgbtGreen;
                        blue += temp[i+r][j+c].rgbtBlue;
                        count++;
                    }
                   
                }
            }
    
             image[i][j].rgbtRed = round(red/count);
             image[i][j].rgbtGreen = round(green/count);
             image[i][j].rgbtBlue = round(blue/count);
    
    
    
        }
    
    }
    
    
     return;
    
    }