cs50 pset4 filter edge detection - c

I made a new post for the major changes in my post, but the problem now is that my image is returned as the same image entered. I believe it's a problem with the newimage variable and swapping it with the old image variable but I don't know why.
void edges(int height, int width, RGBTRIPLE image[height][width])
{
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}};
RGBTRIPLE newimage[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int redx = 0;
int greenx = 0;
int bluex = 0;
int redy = 0;
int greeny = 0;
int bluey = 0;
for (int k = i - 1; k <= i + 1 && k < height; k++)
{
for (int m = j - 1; m <= j + 1 && m < width; m++)
{
if (k != -1 && m != -1)
{
redx += image[k][m].rgbtRed * gx[k-(i-1)][m-(j-1)];
greenx += image[k][m].rgbtGreen * gx[k-(i-1)][m-(j-1)];
bluex += image[k][m].rgbtBlue * gx[k-(i-1)][m-(j-1)];
redy += image[k][m].rgbtRed * gy[k-(i-1)][m-(j-1)];
greeny += image[k][m].rgbtGreen * gy[k-(i-1)][m-(j-1)];
bluey += image[k][m].rgbtBlue * gy[k-(i-1)][m-(j-1)];
}
}
}
int finalred = round(sqrt((redx * redx) + (redy * redy)));
int finalgreen = round(sqrt((greenx * greenx) + (greeny * greeny)));
int finalblue = round(sqrt((bluex * bluex) + (bluey * bluey)));
if (finalred > 255)
{
finalred = 255;
}
if (finalgreen > 255)
{
finalgreen = 255;
}
if (finalblue > 255)
{
finalblue = 255;
}
newimage[i][j].rgbtRed = finalred;
newimage[i][j].rgbtGreen = finalgreen;
newimage[i][j].rgbtBlue = finalblue;
}
}
image = newimage;
return;
}

Related

So, this function was supposed to use edge detection and apply sobel filter. But well It doesn't

I'm trying to figure out what I did wrong. My intent is in title :)
This is the code I tried, and it clearly isn't working. It just print's 1 solid colour, also edges are not included yet. What part is wrong I can't figure it out what part of this code is wrong. It's just a function and it's in C.
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE edit[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
edit[i][j] = image[i][j];
}
}
for (int q = 1; q < height - 1; q++)
{
for (int w = 1; w < width - 1; w++)
{
int redEdit[9];
int greenEdit[9];
int blueEdit[9];
for (int top = 0; top < 3; top++)
{
redEdit[top] += edit[q - 1][w - 1 + top].rgbtRed;
greenEdit[top] += edit[q - 1][w - 1 + top].rgbtGreen;
blueEdit[top] += edit[q - 1][w - 1 + top].rgbtBlue;
}
int midctr = 0;
for (int mid = 3; mid < 6; mid++)
{
redEdit[mid] += edit[q][w - 1 + midctr].rgbtRed;
greenEdit[mid] += edit[q][w - 1 + midctr].rgbtGreen;
blueEdit[mid] += edit[q][w - 1 + midctr].rgbtBlue;
midctr ++;
}
int topctr = 0;
for (int top = 6; top < 9; top++)
{
redEdit[top] += edit[q + 1][w - 1 + topctr].rgbtRed;
greenEdit[top] += edit[q + 1][w - 1 + topctr].rgbtGreen;
blueEdit[top] += edit[q + 1][w - 1 + topctr].rgbtBlue;
topctr ++;
}
int matrixgx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
int matrixgy[] = {- 1, -2, -1, 0, 0, 0, 1, 2, 1};
int redEditgx[9];
int greenEditgx[9];
int blueEditgx[9];
int redEditgy[9];
int greenEditgy[9];
int blueEditgy[9];
for (int mtrx = 0; mtrx < 9; mtrx++)
{
redEditgx[mtrx] = redEdit[mtrx] * matrixgx[mtrx];
greenEditgx[mtrx] = greenEdit[mtrx] * matrixgx[mtrx];
blueEditgx[mtrx] = blueEdit[mtrx] * matrixgx[mtrx];
redEditgy[mtrx] = redEdit[mtrx] * matrixgy[mtrx];
greenEditgy[mtrx] = greenEdit[mtrx] * matrixgy[mtrx];
blueEditgy[mtrx] = blueEdit[mtrx] * matrixgy[mtrx];
}
// now sum up the changes of gx and gt
int redSumgx = 0;
int greenSumgx = 0;
int blueSumgx = 0;
int redSumgy = 0;
int greenSumgy = 0;
int blueSumgy = 0;
for (int sum = 0; sum < 9; sum++)
{
redSumgx += redEditgx[sum];
greenSumgx += greenEditgx[sum];
blueSumgx += blueEditgx[sum];
redSumgy += redEditgy[sum];
greenSumgy += greenEditgy[sum];
blueSumgy += blueEditgy[sum];
}
int finalRed = round(sqrt(pow(redSumgx, 2) + pow(redSumgy, 2)));
int finalGreen = round(sqrt(pow(greenSumgx, 2) + pow(greenSumgy, 2)));
int finalBlue = round(sqrt(pow(blueSumgx, 2) + pow(blueSumgy, 2)));
if (finalRed > 255)
{
finalRed = 255;
}
if (finalGreen > 255)
{
finalGreen = 255;
}
if (finalBlue > 255)
{
finalBlue = 255;
}
image[q][w].rgbtRed = finalRed;
image[q][w].rgbtGreen = finalGreen;
image[q][w].rgbtBlue = finalBlue;
}
}
return;
}
I know this code is cancer please don't judge
These arrays
int redEdit[9];
int greenEdit[9];
int blueEdit[9];
have not been initialised and hold arbitrary values. So when you execute statements such as
redEdit[top] += edit[q - 1][w - 1 + top].rgbtRed;
the sum is meaningless. You need
int redEdit[9] = { 0 };
int greenEdit[9] = { 0 };
int blueEdit[9] = { 0 };
Only static variables are implicitly initialised to 0. Local (auto) variables are not.

CS 50 Edges filter

Could someone tell me where in my code I made a mistake? The values are failing check50. The loops seem to be correct, I really can't tell where the problem is.
I've looked at https://medium.com/swlh/cs50-pset-4-filter-8cbf734b0dbc and the code seems pretty much the same...
Thank you so much!
https://pastebin.com/MkZPFbEK
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// for pixels at the border, treat value as B.G.R value as 0
// compute gx and gy for each value of B, G, R
// square root of gx and gy squared
RGBTRIPLE tempstore[height][width];
int Gx[3][3] = {
{-1, 0 , 1},
{-2, 0 , 2},
{-1, 0 , 1}
};
int Gy[3][3] = {
{-2, -1 , -1},
{0, 0 , 0},
{2, -1 , 1}
};
// for each row
for (int i = 0; i < height; i = i + 1)
{
// for each column
for (int j = 0; j < width; j = j + 1)
{
// float horvalueB=0;
// float horvalueG=0;
// float horvalueR=0;
// float vertvalueB=0;
// float vertvalueG=0;
// float vertvalueR=0;
float sumhorB=0;
float sumhorG=0;
float sumhorR=0;
float sumvertB=0;
float sumvertG=0;
float sumvertR=0;
// check if height of neighbor cell
for (int k = -1; k <= 1; k = k + 1)
{
for (int l = -1; l <= 1; l = l + 1)
{
int htcheck = i + k;
int wdcheck = j + l;
// check if height of neighbor cell is within bounds
if (((htcheck) >= 0) && ((htcheck) < height))
{
// check if width of neighbor cell is within bounds
if (((wdcheck) >= 0) && ((wdcheck) < width))
{
sumhorB += image[htcheck][wdcheck].rgbtBlue * Gx[k+1][l+1];
sumhorG += image[htcheck][wdcheck].rgbtGreen * Gx[k+1][l+1];
sumhorR += image[htcheck][wdcheck].rgbtRed * Gx[k+1][l+1];
sumvertB += image[htcheck][wdcheck].rgbtBlue * Gy[k+1][l+1];
sumvertG += image[htcheck][wdcheck].rgbtGreen * Gy[k+1][l+1];
sumvertR += image[htcheck][wdcheck].rgbtRed * Gy[k+1][l+1];
// sumhorB = sumhorB + horvalueB;
// sumhorG = sumhorG + horvalueG;
// sumhorR = sumhorR + horvalueR;
// sumvertB = sumvertB + vertvalueB;
// sumvertG = sumvertG + vertvalueG;
// sumvertR = sumvertR + vertvalueR;
}
}
}
}
int blue = round(sqrt( sumhorB*sumhorB + sumvertB*sumhorB ));
int green = round(sqrt( sumhorG*sumhorG + sumvertG*sumhorG ));
int red = round(sqrt( sumhorR*sumhorR + sumvertR*sumhorR ));
// Cap at 255
if (red > 255)
{
red = 255;
}
if (green > 255)
{
green = 255;
}
if (blue > 255)
{
blue = 255;
} // Assign new values to pixels
tempstore[i][j].rgbtRed = red;
tempstore[i][j].rgbtGreen = green;
tempstore[i][j].rgbtBlue = blue;
}
}
for (int i = 0; i < height; i = i + 1)
{
for (int j = 0; j < width; j = j + 1)
{
image[i][j].rgbtBlue = tempstore[i][j].rgbtBlue;
image[i][j].rgbtGreen = tempstore[i][j].rgbtGreen;
image[i][j].rgbtRed = tempstore[i][j].rgbtRed;
}
}
return;
}
Ahh.... The problem is I was multiplying the wrong things... Sorry about it. Was multiplying sumvert with sumhor...
int blue = round(sqrt( (sumhorB*sumhorB) + (sumvertB*sumhorB) ));
int green = round(sqrt( (sumhorG*sumhorG) + (sumvertG*sumhorG) ));
int red = round(sqrt( (sumhorR*sumhorR) + (sumvertR*sumhorR) ));
Should be
int blue = round(sqrt( (sumhorB*sumhorB) + (sumvertB*sumvertB) ));
int green = round(sqrt( (sumhorG*sumhorG) + (sumvertG*sumvertG) ));
int red = round(sqrt( (sumhorR*sumhorR) + (sumvertR*sumvertR) ));

CS50(2021) Filter - Blur

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;
}

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

Converting 2D array into a greyscale image in C

I imported the 2d array of Lena using a header file (LenaArray.h); int lena [511][511] = {162,162,162,etc...},
but now I want to convert it into a greyscale image and I don't know how please help? Image of Lena I'm trying to print
#include <stdio.h>
#include "LenaArray.h"
int main () {
int i,j;
int width = 511;
int height = 511;
for (i = 0; i < height; i ++ )
{
for(j = 0; j < width; j ++)
{
printf("%d",&lena[i][j]);
}
}
}
#include <stdio.h>
typedef unsigned char U8;
typedef struct { U8 p[4]; } color;
U8 lena[511][511];
void save(char* file_name,int width,int height)
{
FILE* f = fopen(file_name, "wb");
color tablo_color[256];
for (int i = 0; i < 256; i++)
tablo_color[i] = { (U8)i,(U8)i,(U8)i,(U8)255 };//BGRA 32 bit
U8 pp[54] = { 'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0 ,
40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 32 };
*(int*)(pp + 2) = 54 + 4 * width * height; //file size
*(int*)(pp + 18) = width;
*(int*)(pp + 22) = height;
*(int*)(pp + 42) = height * width * 4; //bitmap size
fwrite(pp, 1, 54, f);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
U8 indis = lena[i][j];
fwrite(tablo_color+indis,4,1,f);
}
}
fclose(f);
}
int main()
{
for (int i = 0; i < 511; i++)
{
for (int j = 0; j < 511; j++)
{
lena[i][j]=i+j;
}
}
save("test.bmp", 511, 511);
}

Resources