0

I'm not sure why my code doesn't work for the blur section of pset4 (filter; less)? (I am aware there's a more efficient way to do this, but am interested in learning what's wrong with this specific code!)

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

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j];
        }
    }

    // declaring counter and sums
    float counter = 0;
    float sumR = 0;
    float sumG = 0;
    float sumB = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // if top left corner pixel exists
            if ((i - 1 > -1) && (j - 1 > -1))
            {
                sumR = sumR + temp[i - 1][j - 1].rgbtRed;
                sumB = sumB + temp[i - 1][j - 1].rgbtBlue;
                sumG = sumG + temp[i - 1][j - 1].rgbtGreen;

                counter++;
            }

            // if top pixel exists
            if (i - 1 > -1)
            {
                sumR = sumR + temp[i - 1][j].rgbtRed;
                sumB = sumB + temp[i - 1][j].rgbtBlue;
                sumG = sumG + temp[i - 1][j].rgbtGreen;

                counter++;
            }

            // if top right corner pixel exists
            if (i - 1 > -1 && j + 1 < width)
            {
                sumR = sumR + temp[i - 1][j + 1].rgbtRed;
                sumB = sumB + temp[i - 1][j + 1].rgbtBlue;
                sumG = sumG + temp[i - 1][j + 1].rgbtGreen;

                counter++;
            }

            // if left pixel exists
            if (j - 1 > -1)
            {
                sumR = sumR + temp[i][j - 1].rgbtRed;
                sumB = sumB + temp[i][j - 1].rgbtBlue;
                sumG = sumG + temp[i][j - 1].rgbtGreen;

                counter++;
            }

            // pixel [i][j]
            sumR = sumR + temp[i][j].rgbtRed;
            sumB = sumB + temp[i][j].rgbtBlue;
            sumG = sumG + temp[i][j].rgbtGreen;

            counter++;

            // if right pixel exists
            if (j + 1 < width)
            {
               sumR = sumR + temp[i][j + 1].rgbtRed;
               sumB = sumB + temp[i][j + 1].rgbtBlue;
               sumG = sumG + temp[i][j + 1].rgbtGreen;

               counter++;
            }

            // if bottom left pixel exists
            if (i + 1 < height && j - 1 > -1)
            {
               sumR = sumR + temp[i + 1][j - 1].rgbtRed;
               sumB = sumB + temp[i + 1][j - 1].rgbtBlue;
               sumG = sumG + temp[i + 1][j - 1].rgbtGreen;

               counter++;
            }

            // if bottom pixel exists
            if (i + 1 < height)
            {
                sumR = sumR + temp[i + 1][j].rgbtRed;
                sumB = sumB + temp[i + 1][j].rgbtBlue;
                sumG = sumG + temp[i + 1][j].rgbtGreen;

                counter++;
            }

            // if bottom right pixel exists
            if (i + 1 < height && j + 1 < width)
            {
                sumR = sumR + temp[i + 1][j + 1].rgbtRed;
                sumB = sumB + temp[i + 1][j + 1].rgbtBlue;
                sumG = sumG + temp[i + 1][j + 1].rgbtGreen;

                counter++;
            }

            // find averages
            float avgR = round(sumR / counter);
            float avgB = round(sumB / counter);
            float avgG = round(sumG / counter);

            if (avgR > 255)
            {
                avgR = 255;
            }

            if (avgB > 255)
            {
                avgB = 255;
            }

            if (avgG > 255)
            {
                avgG = 255;
            }
            
            // print image 
            image[i][j].rgbtRed = avgR;
            image[i][j].rgbtBlue = avgB;
            image[i][j].rgbtGreen = avgG;

        }
    }

    return;
}
0

Question: After a pixel is processed, where are counter, sumR, sumB and sumG reset before the next pixel is processed?

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

1
  • I put my //declaration of counter and sums... within the for-loop and now the code works. Thank you!
    – user33981
    Aug 3 at 5:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .