1

Apologises for the long post: I wanted to be as explicit in my logic as possible

The code I've written uses the following logic to box blur:

  • Use nested for loop to access each pixel in image
  • Check where the pixel is - using predefined start and endpoints of rows and columns
  • Change appropriate variables e.g. count_from_i based on where the pixel is
  • once the pixel's location is identified use a nested for loop with the variables set in accordance with each pixel's location
  • This nested for loop goes through each appropriate surrounding pixel and adds it to the appropriate variable e.g redsum, if dealing with the red pixel
  • once the appropriate pixel is found then add all pixels of same hex colour value together and divide by the appropriate count of pixels. E.g. if it's a corner case then divide the summed green pixels by 4
  • Then while still in my nested first loop (which goes through each pixel) I get my answer and let it equal my individual pixel

Result: I get an image of static noise. I fail most of check50 for blur bar "blur correctly filters pixel in corner"

Any guidance would be greatly appreciated

Code:

int count;
int redsum = 0, greensum = 0, bluesum = 0;
int count_from_i, count_to_i;
int count_from_j, count_to_j;


int first_row = 0; // it's always gonna start from zero, no matter the size
int last_row = height - 1;

int first_column = 0; // it's always gonna start from zero, no matter the size
int last_column = width - 1;

for (int h = 0; h < height; h++)
{
    for (int w = 0; w < width; w++)
    {

        // LEFT AREA FIRST
        if (w == first_column) /* if pixel is first in column*/
        {
            if (h == first_row) /* If pixel is first in row*/
            {
            // TOP LEFT CORNER PIXEL
                count = 4;

                count_from_i = 0, count_to_i = 2;// Row
                count_from_j = 0, count_to_j = 2;// Column
            }


            else if (h == last_row) /* if pixel is last in the row*/
            {
                // BOTTOM LEFT CORNER PIXEL

                count = 4;

                count_from_i = -1, count_to_i = 1; // Row
                count_from_j = 0, count_to_j = 2; // Column

            }

            else
            {
                // LEFT SIDE PIXEL
                count = 6;

                count_from_i = -1, count_to_i = 2; // Row
                count_from_j = 0, count_to_j = 2; // Column
            }
        }

        else if (w == last_column) /*if pixel is last in column*/
        {
            if (h == first_row) /* If pixel is first in row*/
            {
                // TOP RIGHT CORNER PIXEL

                count = 4;

                count_from_i = 0, count_to_i = 2; //Row
                count_from_j = -1, count_to_j = 1; // Column
            }

            else if (h == last_row) /*if pixel is last in the row*/
            {
                // BOTTOM RIGHT CORNER PIXEL
                count = 4;

                count_from_i = -1, count_to_i = 1; //Row
                count_from_j = -1, count_to_j = 1; // Column
            }

            // RIGHT SIDE PIXEL
            else
            {
                count = 6;

                count_from_i = -1, count_to_i = 2; //Row
                count_from_j = -1, count_to_j = 1; // Column

            }
        }


        //=========BOTTOM (FLOOR) PIXEL============//
        else if (h == last_row) /*pixel is last in the row*/
        {
            count = 6;

            count_from_i = -1, count_to_i = 1; //Row
            count_from_j = -1, count_to_j = 2; // Column
        }

        //=========TOP (CEILING) PIXEL============//

        else if (h == first_row) /*pixel is first in the row*/
        {
            count = 6;

            count_from_i = 0, count_to_i = 2; //Row
            count_from_j = -1, count_to_j = 2; // Column
        }

        //============MIDDLE PIXEL==========================//
        else
        {
            // MIDDLE PIXEL

            count = 9;

            count_from_i = -1, count_to_i = 2; //Row
            count_from_j = -1, count_to_j = 2; //Column
        }
        // Where I'm putting the last part is what's screwing me!!!!!
        // All of this is still happening on one pixel

        for (int i = count_from_i; i < count_to_i; i++)
        {
            for (int j = count_from_j; j < count_to_j; j++)
            {
                redsum   += image[h + i][w + j].rgbtRed;
                greensum += image[h + i][w + j].rgbtGreen;
                bluesum  += image[h + i][w + j].rgbtBlue;
            }
        }

        // See how different the values are if nothing is added back to the pixel
        image[h][w].rgbtRed   = (redsum/count);
        image[h][w].rgbtGreen = (greensum/count);
        image[h][w].rgbtBlue  = (bluesum/count);
    }

}

return;
1

I see two issues with your code. First, the following code is nested inside of your other for loops:

for (int i - count_from_i; i < count_to_i; i++)
{
    for (int j = count_from_j; j < count_to_j; j++)
    {
        redsum   += image[h + i][w + j].rgbtRed;
        greensum += image[h + i][w + j].rgbtGreen;
        bluesum  += image[h + i][w + j].rgbtBlue;
    }
}

// See how different the values are if nothing is added back to the pixel
image[h][w].rgbtRed   = (redsum/count);
image[h][w].rgbtGreen = (greensum/count);
image[h][w].rgbtBlue  = (bluesum/count);

This means, as you pointed out in your logic explanation, that you are performing this function for each repetition of your outer-most loop. You do not want to do this as it means that the values that will be used for each subsequent repetition have been altered. The original image no longer exists after the first pass, so each subsequent pass will distort the image further. You will need to find a way to preserve your original information until it is no longer needed.

Second, you need to be careful with redsum/count and the like. This is integer division and will not always lead to the expected or desired result. It might work for you, but you might get check50 results where it is unhappy that your values are off by +-1. Just a heads-up. :)

Hope this helps. If it does, feel free to accept this answer with the check mark beside it. But, if not, please let me know and I'll try to provide some more assistance.

3
  • Interesting...So from what I gather from your comment the actual image[h][w].rgbtRed are being carried over to each pixel rather than using what's there? Mar 3 at 14:40
  • Yes. Keep in mind that, for each pixel's new value, it uses the value of the pixels that surround it. If those values are altered from a previous pass, then the program will use the altered values because they are now located within the image variable. Mar 3 at 14:48
  • Thank you, you've gotten me that tad bit closer. Mar 3 at 19:34

You must log in to answer this question.

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