CS50(2021) Filter - Blur - c

I couldn't figure out why the blur doesn't work when I check with check50. The image becomes blurry, but the RGB values are wrong. I'm not sure what I did wrong. If anyone knows, please do tell.
This is the picture of my
results in check50.
This is my code:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
// Copy image
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
copy[h][w] = image[h][w];
}
}
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
int row[] = { h - 1, h, h + 1 };
int col[] = { w - 1, w, w + 1 };
int R = 0;
int G = 0;
int B = 0;
int num = 0;
for (int x = 0; x < 3; x++)
{
// Check whether they are in frame
if (row[x] < 0 || row[x] >= height)
{
continue;
}
if (col[x] < 0 || col[x] >= width)
{
continue;
}
// Add up the value
R += copy[row[x]][col[x]].rgbtRed;
G += copy[row[x]][col[x]].rgbtGreen;
B += copy[row[x]][col[x]].rgbtBlue;
num++;
}
// Average
int blurRed = round(R / num);
int blurGreen = round(G / num);
int blurBlue = round(B / num);
// Update pixel
image[h][w].rgbtRed = blurRed;
image[h][w].rgbtGreen = blurGreen;
image[h][w].rgbtBlue = blurBlue;
}
}
return;
}

Related

CS50x Blur Function

I am currently working on the blur function in PSET4 for filter-more, and I am struggling to find the bug in my code. The blur function seems to correctly filter the middle pixel and I would assume other pixels that are not on the edges, but is incorrectly filtering those on the corners and edges. Any nudges in the right direction would be greatly appreciated.
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// Copy to store filtered image into
RGBTRIPLE temp[height][width];
int red_total = 0, blue_total = 0, green_total = 0;
int red_average = 0, blue_average = 0, green_average = 0;
// Goes through each pixel [i][j]
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// Loops through surrounding 3x3 grid and records the average of all of them
for (int h = -1; h < 2; h++)
{
// Checks if the current row is within the image array
if (i + h >= 0 && i + h < height)
{
for (int k = -1; k < 2; k++)
{
// Checks if current column is within image array
if (j + k >= 0 && j + k < width)
{
red_total = red_total + image[i + h][j + k].rgbtRed;
blue_total = blue_total + image[i + h][j + k].rgbtBlue;
green_total = green_total + image[i + h][j + k].rgbtGreen;
}
else continue;
}
}
else continue;
}
// Averages and reassigns pixel value
red_average = round(red_total / 9.0);
blue_average = round(blue_total / 9.0);
green_average = round(green_total / 9.0);
temp[i][j].rgbtBlue = blue_average;
temp[i][j].rgbtRed = red_average;
temp[i][j].rgbtGreen = green_average;
// Resets counter variables
red_total = green_total = blue_total = 0;
red_average = blue_average = green_average = 0;
}
}
// Makes image (original) array equal to the filtered image
for (int x = 0; x < height; x++)
{
for(int y = 0; y < width; y++)
{
image[x][y] = temp[x][y];
}
}
return;
}

Getting a segmentation fault on cs50's filter

I'm currently stuck on cs50's pset4 filter (less comfortable). The program compiles perfectly. I have tried out the first three parts of the code (grayscale, sepia & reflection) and their output has been as desired; however, whenever I try out blur, I keep getting hit with a seg fault. Could someone please help me identify where the error(s) lies?
void blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i <height; i++)
{
for (int j = 0; j < width; j++)
{
int c = i + 1;
int d = j + 1;
int e = i + 2;
int f = j + 2;
float blurred_blue = 0;
float blurred_green = 0;
float blurred_red = 0;
int z = 0;
if (height - i == 1 && width - j >= 2)
{
for (int a = i - 1; a < c; a++)
{
for (int b = j - 1; b < f; b++)
{
blurred_blue = blurred_blue + image[a][b].rgbtBlue;
blurred_green = blurred_green + image[a][b].rgbtGreen;
blurred_red = blurred_red + image[a][b].rgbtRed;
z++;
}
}
}
else if (width - j == 1 && height - i >= 2)
{
for(int a = i - 1; a < e; a++)
{
for(int b = j - 1; b < d; b++)
{
blurred_blue = blurred_blue + image[a][b].rgbtBlue;
blurred_green = blurred_green + image[a][b].rgbtGreen;
blurred_red = blurred_red + image[a][b].rgbtRed;
z++;
}
}
}
else
{
for(int a = i - 1; a < e; a++)
{
for(int b = j - 1; b < f; b++)
{
blurred_blue = blurred_blue + image[a][b].rgbtBlue;
blurred_green = blurred_green + image[a][b].rgbtGreen;
blurred_red = blurred_red + image[a][b].rgbtRed;
z++;
}
}
}
image[i][j].rgbtBlue = round (blurred_blue / z);
image[i][j].rgbtGreen = round (blurred_green / z);
image[i][j].rgbtRed = round (blurred_red / z);
}
}
return;
}

CS50 "BLUR" PSET 4

when I run make filter it compiles but when I'm running it it does this... helpers.c:104:39: runtime error: -nan is outside the range of representable values of type 'int'
void blur(int height, int width, RGBTRIPLE image[height][width])
{
float red = 0, blue = 0, green = 0;
int average_red[height][width], average_blue[height][width], average_green[height][width];
float count = 0;
// LOOP HEIGHT
for (int i = 0; height > i; i++)
{
// LOOP WIDTH
for (int j = 0; width > j; j++)
{
// LOOP 3x3 PIXELS
for (int x = i - 1; i > x; x++)
{
for (int y = j - 1; j > y; y++)
{
// ONLY ACCEPT PIXELS WITHIN THE IMAGE
if ((x >= 0 && x < height) || (y >= 0 && y < width))
{
red += image[x][y].rgbtRed;
blue += image[x][y].rgbtBlue;
green += image[x][y].rgbtGreen;
count++;
}
}
}
// GET THE AVERAGE
average_red[i][j] = limit(round(red / count));
average_blue[i][j] = limit(round(blue / count));
average_green[i][j] = limit(round(green / count));
// RESET ALL VALUE
red = 0;
blue = 0;
green = 0;
count = 0;
}
}
// LOOP HEIGHT
for (int i = 0; height > i; i++)
{
// LOOP WIDTH
for (int j = 0; width > j; j++)
{
// RETURN THE VALUE BY COLOR TO RESPECTIVE PIXELS
image[i][j].rgbtRed = average_red[i][j];
image[i][j].rgbtBlue = average_blue[i][j];
image[i][j].rgbtGreen = average_green[i][j];
}
}
return;
}
for the inner loops I used
for (int col = i - 1; i + 2 > col; col++)
{
for (int row = j - 1; j + 2 > row; row++)
{
if (col >= 0 && col < height && row >= 0 && row < width)
{
red += image[col][row].rgbtRed;
blue += image[col][row].rgbtBlue;
green += image[col][row].rgbtGreen;
count++;
}
}
}

CS50x - Filter More

I doing filter of images bmp in c. The pset required the Sobel Operator. I don't know where I being mistake.
Help me please.
I'm basically making a copy of my image (because the original will be changed.)
Then I take the 3x3 values to put in the formula
So we add and multiply
Finally I take the result and put it in the formula: square root (Gx ^ 2 + Gy ^ 2)
If it exceeds 255 it must be 255, because RGB goes up to 255 which is white
And if there is a broken number, round to the nearest
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Variáveis
RGBTRIPLE temp[height][width];
int GR[3][3];
int GG[3][3];
int GB[3][3];
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
float resultR, resultG, resultB;
// Cópia temporária do original
for (int tempi = 0; tempi < height; tempi++)
{
for (int tempj = 0; tempj < width; tempj++)
{
temp[tempi][tempj].rgbtRed = image[tempi][tempj].rgbtRed;
temp[tempi][tempj].rgbtGreen = image[tempi][tempj].rgbtGreen;
temp[tempi][tempj].rgbtBlue = image[tempi][tempj].rgbtBlue;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int countx = 0;
// Pegar 3X3
for (int x = i - 1; x < i + 2; x++)
{
int county = 0;
for (int y = j - 1; y < j + 2; y++)
{
if ((x < 0 || y < 0) || (x >= height || y >= width))
{
GR[countx][county] = 0;
GG[countx][county] = 0;
GB[countx][county] = 0;
}
else
{
GR[countx][county] = temp[x][y].rgbtRed;
GG[countx][county] = temp[x][y].rgbtGreen;
GB[countx][county] = temp[x][y].rgbtBlue;
}
county++;
}
countx++;
}
float sumxR = 0, sumyR = 0, sumxG = 0, sumyG = 0, sumxB = 0, sumyB = 0;
for (int ix = 0; ix <= 2; ix++)
{
for (int iy = 0; iy <= 2; iy++)
{
sumxR = sumxR + (GR[ix][iy] * Gx[ix][iy]);
sumxG = sumxG + (GG[ix][iy] * Gx[ix][iy]);
sumxB = sumxB + (GB[ix][iy] * Gx[ix][iy]);
sumyR = sumyR + (GR[ix][iy] * Gy[ix][iy]);
sumyG = sumyG + (GG[ix][iy] * Gy[ix][iy]);
sumyB = sumyB + (GB[ix][iy] * Gy[ix][iy]);
}
}
resultR = sqrt(sumxR * sumxR) + sqrt(sumyR * sumyR);
resultG = sqrt(sumxG * sumxG) + sqrt(sumyG * sumyG);
resultB = sqrt(sumxB * sumxB) + sqrt(sumyB * sumyB);
if (resultR > 255)
{
resultR = 255;
}
if (resultG > 255)
{
resultG = 255;
}
if (resultB > 255)
{
resultB = 255;
}
image[i][j].rgbtRed = round(resultR);
image[i][j].rgbtGreen = round(resultG);
image[i][j].rgbtBlue = round(resultB);
}
}
}
You need to changesqrt(sumxR * sumxR) + sqrt(sumyR * sumyR) to sqrt((sumxR * sumxR) + (sumyR * sumyR)) and they are not the same

Attempting to apply a 'blur' filter to a bmp for CS50 pset4, and the output image is much darker

This is the output and input image that my code generates: https://imgur.com/a/mT7YqCD
The following code is for the blur function, all the supporting code is untouched from pset4.
void blur(int height, int width, RGBTRIPLE image[height] [width])
{
RGBTRIPLE newImage[height][width];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
BYTE redAvg = 0;
BYTE greenAvg = 0;
BYTE blueAvg = 0;
BYTE count = 0;
for (int y = -1; y <= 1; y++)
{
for (int x = -1; x <= 1; x++)
{
int h_1 = h + y;
int w_1 = w + x;
if (h_1 >= 0 && h_1 < height && w_1 >= 0 && w_1 < width)
{
count++;
redAvg += image[h_1][w_1].rgbtRed;
greenAvg += image[h_1][w_1].rgbtGreen;
blueAvg += image[h_1][w_1].rgbtBlue;
}
}
}
newImage[h][w].rgbtRed = redAvg/count;
newImage[h][w].rgbtGreen = greenAvg/count;
newImage[h][w].rgbtBlue = blueAvg/count;
}
}
for (int x = 0; x < height; x++)
{
for (int y = 0; y <width; y++)
{
image[x][y] = newImage[x][y];
}
}
return;
}

Resources