0

my code for my first attempt for blur is a tad long (I know there are definitely better ways to solve it), but I'm not sure what's wrong with my current one as when I try to visualize my code with a 3x3 image it should work in theory. In check50 I fail everything except for filtering the middle pixel!

Here's the entire code for blur (really sorry for the length):

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 1; i < height - 1; i++) // for pixels with full 3x3 grid boxes
    {
        for (int j = 1; j < width - 1; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i - 1; k <= i + 1; k++)
            {
                for (int l = j - 1; l <= j + 1; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/9);
            image[i][j].rgbtGreen = round(tracker_green/9);
            image[i][j].rgbtBlue = round(tracker_blue/9);
        }
    }

    for (int i = 0; i < 1; i++) // for first row middle pixels
    {
        for (int j = 1; j < width - 1; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i; k <= i + 1; k++)
            {
                for (int l = j - 1; l <= j + 1; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/6);
            image[i][j].rgbtGreen = round(tracker_green/6);
            image[i][j].rgbtBlue = round(tracker_blue/6);
        }
    }

    for (int i = height - 1; i < height; i++) // for last row middle pixels
    {
        for (int j = 1; j < width - 1; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i - 1; k <= i; k++)
            {
                for (int l = j - 1; l <= j + 1; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/6);
            image[i][j].rgbtGreen = round(tracker_green/6);
            image[i][j].rgbtBlue = round(tracker_blue/6);
        }
    }

    for (int i = 1; i < height - 1; i++) // for first column middle pixels
    {
        for (int j = 0; j < 1; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i - 1; k <= i + 1; k++)
            {
                for (int l = j; l <= j + 1; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/6);
            image[i][j].rgbtGreen = round(tracker_green/6);
            image[i][j].rgbtBlue = round(tracker_blue/6);
        }
    }

    for (int i = 1; i < height - 1; i++) // for last column middle pixels
    {
        for (int j = width - 1; j < width; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i - 1; k <= i + 1; k++)
            {
                for (int l = j - 1; l <= j; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/6);
            image[i][j].rgbtGreen = round(tracker_green/6);
            image[i][j].rgbtBlue = round(tracker_blue/6);
        }
    }

    for (int i = 0; i < 1; i++) // left upper corner pixel
    {
        for (int j = 0; j < 1; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i; k <= i + 1; k++)
            {
                for (int l = j; l <= j + 1; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/4);
            image[i][j].rgbtGreen = round(tracker_green/4);
            image[i][j].rgbtBlue = round(tracker_blue/4);
        }
    }

    for (int i = 0; i < 1; i++) // right upper corner pixel
    {
        for (int j = width - 1; j < width; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i; k <= i + 1; k++)
            {
                for (int l = j - 1; l <= j; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/4);
            image[i][j].rgbtGreen = round(tracker_green/4);
            image[i][j].rgbtBlue = round(tracker_blue/4);
        }
    }

    for (int i = height - 1; i < height; i++) // left lower corner pixel
    {
        for (int j = 0; j < 1; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i - 1; k <= i; k++)
            {
                for (int l = j; l <= j + 1; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/4);
            image[i][j].rgbtGreen = round(tracker_green/4);
            image[i][j].rgbtBlue = round(tracker_blue/4);
        }
    }

    for (int i = height - 1; i < height; i++) // right lower corner pixel
    {
        for (int j = width - 1; j < width; j++)
        {
            float tracker_red = 0; // track the values for red
            float tracker_green = 0; // track the values for green
            float tracker_blue = 0; // track the values for blue

            for (int k = i - 1; k <= i; k++)
            {
                for (int l = j - 1; l <= j; l++)
                {
                    tracker_red += (float) image[k][l].rgbtRed;
                    tracker_green += (float) image[k][l].rgbtGreen;
                    tracker_blue += (float) image[k][l].rgbtBlue;
                }
            }

            image[i][j].rgbtRed = round(tracker_red/4);
            image[i][j].rgbtGreen = round(tracker_green/4);
            image[i][j].rgbtBlue = round(tracker_blue/4);
        }
    }
    return;
}
2

You read from and write to the same array, but you need the original values for computing other averages.

A solution would be to use two arrays, one for reading, one for writing, and copy between them (either before or after processing).

| improve this answer | |
  • Oh right.... alright got it thanks so much! – Jereld Lim Feb 11 at 15:18

You must log in to answer this question.

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