0

I've been struggling with the filter portion of problem set 4 for quite a while. In particular, I've had a particularly persistent bug.

As you can see from the Check50 screenshot below, some RGB values are just all 0 for some pixels.[trouble with Check50

I've made a mockup in MS Paint. I've color-coded the "problem-pixels" red. The "problem pixels" are in red.

Finally, my code is below. If you could help with this issue to help me figure out what I'm doing wrong, that would be much appreciated.

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


    float tot[height][width];
    int r[height][width];
    int g[height][width];
    int b[height][width];
    
    
    
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            r[i][j] = 0;
            g[i][j] = 0;
            b[i][j] = 0;
            tot[height][width] = 0;
        }
    }
    
    
    

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {


            //the pixel itself
            tot[i][j]++;
            r[i][j] = r[i][j] +  image[i][j].rgbtRed;
            g[i][j] = g[i][j] +  image[i][j].rgbtGreen;
            b[i][j] = b[i][j] +  image[i][j].rgbtBlue;

            //north
            if (i != 0)
            {
                tot[i][j]++;
                r[i][j] = r[i][j] +  image[i - 1][j].rgbtRed;
                g[i][j] = g[i][j] +  image[i - 1][j].rgbtGreen;
                b[i][j] = b[i][j] +  image[i - 1][j].rgbtBlue;

                //northwest
                if (j != 0)
                {
                    tot[i][j]++;
                    r[i][j] = r[i][j] +  image[i - 1][j - 1].rgbtRed;
                    g[i][j] = g[i][j] +  image[i - 1][j - 1].rgbtGreen;
                    b[i][j] = b[i][j] +  image[i - 1][j - 1].rgbtBlue;

                }

                if ((j + 1) != width)
                {
                    tot[i][j]++;
                    r[i][j] = r[i][j] +  image[i - 1][j + 1].rgbtRed;
                    g[i][j] = g[i][j] +  image[i - 1][j + 1].rgbtGreen;
                    b[i][j] = b[i][j] +  image[i - 1][j + 1].rgbtBlue;

                }
            }

            //south
            if ((i + 1) != height)
            {
                tot[i][j]++;
                r[i][j] = r[i][j] +  image[i + 1][j].rgbtRed;
                g[i][j] = g[i][j] +  image[i + 1][j].rgbtGreen;
                b[i][j] = b[i][j] +  image[i + 1][j].rgbtBlue;

                //southwest
                if (j != 0)
                {
                    tot[i][j]++;
                    r[i][j] = r[i][j] +  image[i + 1][j - 1].rgbtRed;
                    g[i][j] = g[i][j] +  image[i + 1][j - 1].rgbtGreen;
                    b[i][j] = b[i][j] +  image[i + 1][j - 1].rgbtBlue;
                }

                //southeast
                if ((j + 1) != width)
                {
                    tot[i][j]++;
                    r[i][j] = r[i][j] +  image[i + 1][j + 1].rgbtRed;
                    g[i][j] = g[i][j] +  image[i + 1][j + 1].rgbtGreen;
                    b[i][j] = b[i][j] +  image[i + 1][j + 1].rgbtBlue;
                }

            }

            //west
            if (j != 0)
            {
                tot[i][j]++;
                r[i][j] = r[i][j] +  image[i][j - 1].rgbtRed;
                g[i][j] = g[i][j] +  image[i][j - 1].rgbtGreen;
                b[i][j] = b[i][j] +  image[i][j - 1].rgbtBlue;
            }

            //east
            if ((j + 1) != width)
            {
                tot[i][j]++;
                r[i][j] = r[i][j] +  image[i][j + 1].rgbtRed;
                g[i][j] = g[i][j] +  image[i][j + 1].rgbtGreen;
                b[i][j] = b[i][j] +  image[i][j + 1].rgbtBlue;
            }

        }

    }

    //averages
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = round(r[i][j] / tot[i][j]);
            image[i][j].rgbtGreen = round(g[i][j] / tot[i][j]);
            image[i][j].rgbtBlue = round(b[i][j] / tot[i][j]);
        }
    }

    return;
}

You must log in to answer this question.

Browse other questions tagged .