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:
- First I looped through the height and width of the stock image with 2 for loops.
- 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.
- 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)
- 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.
- pixelcounter++ to add to the total of valid pixels surrounding the [i][j]th pixel.
- Exiting the messy loop, to ensure all pixels are counted, array pixelcolour is declared with size of pixelcounter.
- 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.
- I declared 3 variables - rawred, rawblue, rawgreen with the purpose of just totalling the values of red green and blue.
- For loop to carry out step 8.
- 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.
- 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;
}