Reputation: 21
I am stuck on cs50's box-blur solution, which is part of the filter (less) task of pset4. When I run my program, nothing is being executed...
The instructions are as follows: Blur
There are a number of ways to create the effect of blurring or softening an image. For this problem, we’ll use the “box blur,” which works by taking each pixel and, for each color value, giving it a new value by averaging the color values of neighboring pixels.
(example) The new value of each pixel would be the average of the values of all of the pixels that are within 1 row and column of the original pixel (forming a 3x3 box). For example, each of the color values for pixel 6 would be obtained by averaging the original color values of pixels 1, 2, 3, 5, 6, 7, 9, 10, and 11 (note that pixel 6 itself is included in the average). Likewise, the color values for pixel 11 would be be obtained by averaging the color values of pixels 6, 7, 8, 10, 11, 12, 14, 15 and 16.
For a pixel along the edge or corner, like pixel 15, we would still look for all pixels within 1 row and column: in this case, pixels 10, 11, 12, 14, 15, and 16.
My code:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
int red = 0;
int blue = 0;
int green = 0;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int count = 0;
int rowCoords[] = { row-1, row, row+1 };
int colCoords[] = { col-1, col, col+1 };
//3x3 square around pixel
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
int curR = rowCoords[r];
int curC = colCoords[c];
//check adjacents inside image
if (curR >= 0 && curR < height)
{
if (curC >= 0 && curC < width)
{
//total of all surrounding pixels
RGBTRIPLE pixel = image[curR][curC];
red = red + pixel.rgbtRed;
green = green + pixel.rgbtGreen;
blue = blue + pixel.rgbtBlue;
count++;
}
}
}
//average
temp[row][col].rgbtRed = round(red / count);
temp[row][col].rgbtGreen = round(green / count);
temp[row][col].rgbtBlue = round(blue / count);
}
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//copy to original
image[i][j] = temp[i][j];
}
}
return;
}
Upvotes: 1
Views: 2955
Reputation: 9
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
float avgcountr;
float Blue;
float Red;
float Green;
RGBTRIPLE image2[height][width]; // copy of original image
// copying pixels from original image to our copy
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image2[i][j] = image[i][j];
}
}
// Iterating through image pixels
for (int i = 0; i < height; i++) // iterating over rows
{
for (int j= 0; j < width; j++) // iterating over columns
{
Blue = Red = Green = 0; // setting colour zero before moving to a pixel
avgcountr = 0; // resetting colour before moving to a pixel
//------Iterating over 3*3 box to calculate average
for (int k = -1; k < 2; k++) // for box rows
{
for (int l = -1; l < 2; l++) // box columns
{
// Checking if pixel from 3*3 box lies inside image
// if it does, skip that pixel from average
if (i + k < 0 || i + k > height - 1 || j + l < 0 || j + l > width - 1)
{
continue;
}
Blue += image2[i+k][j+l].rgbtBlue;
Green += image2[i+k][j+l].rgbtGreen;
Red += image2[i+k][j+l].rgbtRed;
avgcountr++;
}
}
// updating values in the original image.
image[i][j].rgbtBlue = round(Blue / avgcountr);
image[i][j].rgbtGreen = round(Green / avgcountr);
image[i][j].rgbtRed = round(Red / avgcountr);
}
}
}
Upvotes: 0
Reputation: 1264
The code is very good. I would express some things differently (for example I think if (X) if (Y)
would be clearer as if (X && Y)
, and the extra level of indirection via a lookup table around the box indices is unnecessary obfuscation), but functionally it is almost right. The only issue I see is that the values of red
, green
, and blue
are only initialized to 0
once. That needs to happen before processing each pixel. So for example you could put those lines here:
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
int count = 0;
int red = 0;
int blue = 0;
int green = 0;
...
That said, that error should be producing wacky color values in the image. It should not have an effect that would be described as "nothing is being executed." So, it is likely that your function blur()
function is never getting called at all. Can you put a breakpoint at the top of this function to make sure it is actually getting called? Or do you want to post your main()
function?
Upvotes: 0