0

So I have been struggling to correct my code to fix the blur function of pset4 and feel that my code is correct (even if it isn't optimally efficient).

Here's how I went about it:

  1. First I looped through the height and width of the stock image with 2 for loops.
  2. Set a pixelcounter to 0 to count the number of valid pixels around any particular pixel defined by the j'th pixel of the i'th row.
  3. I set up two for loops to iterate across a 3x3 grid like structure around the pixel in question. The variables in play here are k and l. K has to be 1 row above i (hence k = i-1), ending one row below i (hence k <= i+1) and l has to be 1 pixel behind j (hence l = j-1), ending 1 pixel ahead of j (hence l <= j+1)
  4. Inside the for loops stated in '3.' I used an 'if loop' to determine whether the pixel being iterated across in the 3x3 grid exists - by stating that k has to be greater than -1 and less than 'height' and ditto for l.
  5. pixelcounter++ to add to the total of valid pixels surrounding the [i][j]th pixel.
  6. Exiting the messy loop, to ensure all pixels are counted, array pixelcolour is declared with size of pixelcounter.
  7. I used the same loops as seen in step 3 to loop through the 3x3 pixel grid surrounding the [i][j]th pixel with the same if condition. Only this time I nested it within a for loop using z as my pixelcounter, such that it can iterate across the 1D array pixelcolour, storing in it at the zth position the properties of the image RGB colours at [k][l] if they are valid.
  8. I declared 3 variables - rawred, rawblue, rawgreen with the purpose of just totalling the values of red green and blue.
  9. For loop to carry out step 8.
  10. I then initialized average values of RGB components with avgred/avgblue/avggreen with rawred/rawblue/rawgreen floats divided by pixelcounter casted as a float. Rounded the result to give integral values.
  11. I then input those integral values into the pixel at the [i][j]th pixel.

I know there's probably a much more efficient way to do this, but I really want to see where exactly I'm going wrong with this code. It compiles and the end result picture is strangely shifted to the left-corner. Nothing is blurred, the entire picture is shifted by a pixel and there are no pixels that shouldn't have been there (stray pixels with random colours).

Here is my code:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // Looping through height of the image
    for (int i = 0; i < height; i++)
    {
        // Looping through the individual pixels in each row
        for (int j = 0; j < width; j++)
        {
            int pixelcounter = 0;
            // Looping through a 3x3 pixel grid surrounding of the individual pixel - Height
            for (int k = i - 1; k <= i + 1; k++)
            {
                // Looping through individual pixels within the kth row
                for (int l = j - 1; l <= j + 1; l++)
                {
                    // Counting the number of valid pixels in the 3x3 grid
                    if ((k > -1) && (k < height) && (l > -1) && (l < width))
                    {
                        pixelcounter++;
                    }
                }
            }
            
            RGBTRIPLE pixelcolour[pixelcounter];
            // Looping through array 3x3 pixel grid surrounding the individual pixel - height
            for (int z = 0; z < pixelcounter; z++)
            {
                for (int k = i - 1; k <= i + 1; k++)
                {
                    for (int l = j - 1; l <= j + 1; l++)
                    {
                        // Storing valid pixels in an array of valid pixels
                        if ((k > -1) && (k < height) && (l > -1) && (l < width))
                        {
                        
                            pixelcolour[z] = image[k][l];
                        
                        }
                    }
                }
            }
            
            // adding all RGB components
            float rawred = 0;
            float rawblue = 0;
            float rawgreen = 0;
            for (int a = 0; a < pixelcounter; a++)
            {
                rawred = rawred + pixelcolour[a].rgbtRed;
                rawblue = rawblue + pixelcolour[a].rgbtBlue;
                rawgreen = rawgreen + pixelcolour[a].rgbtGreen;
            }
            
            // Calculating average values of RGB component
            int avgred = round(rawred / (float) pixelcounter);
            int avgblue = round(rawblue / (float) pixelcounter);
            int avggreen = round(rawgreen / (float) pixelcounter);
            
            // Dereferencing original pixel colour to new colour
            image[i][j].rgbtRed = avgred;
            image[i][j].rgbtBlue = avgblue;
            image[i][j].rgbtGreen = avggreen;
        }
    }

    return;
}
1

I solved it! There was an unnecessary for loop incrementing z and the absence of a copy of the image to work on.

You must log in to answer this question.

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