Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, if anyone has done CS50? this is from pset4 "filter-more comfortable" and I cannot get write the edge detection function. what am I doing wrong? any help will be appreciated!

What I have tried:

C
void edges(int height, int width, RGBTRIPLE image[height][width])
{
	RGBTRIPLE copy[height][width];

	for (int i=0; i<height; i++)
	{
		for (int j=0; j<width; j++)
		{
			copy[i][j]=image[i][j];
		}
	}

	int x[3][3]={ { -1,0,1 } ,{ -2,0,2 } ,{ -1,0,1 } };
	int y[3][3]={ { -1,-2,-1 } ,{ 0,0,0 } ,{ 1,2,1 } };

	for (int i=0; i<height; i++)
	{
		for (int j=0; j<width; j++)
		{
			int a=0; int b=0;
            int gx_b=0; int gx_g=0; int gx_r=0;
            int gy_b=0; int gy_g=0; int gy_r=0;

			for (int k=i-1; k<=i+1; k++)
			{
				if (k<0 || k >= height)
				{
					if (k >= height)
					{
						//a=0;
						// b++;
						break;
					}
					else
					{
						a++;
						k++;
					}
				}

				for (int m=j-1; m<=j+1; m++)
				{
					if (m<0 || m >= width)
					{
						if (m >= width)
						{
							b=0;
							a++;
							break;
						}
						else
						{
							b++;
							m++;
						}
					}
					// printf(" i,j [%d][%d] : k=%d m=%d a=%d b=%d \n",i,j,k,m,a,b);
					gx_b=gx_b+(copy[k][m].rgbtBlue * x[a][b]);
					gx_g=gx_g+(copy[k][m].rgbtGreen * x[a][b]);
					gx_r=gx_r+(copy[k][m].rgbtRed * x[a][b]);

					gy_b=gy_b+(copy[k][m].rgbtBlue * y[a][b]);
					gy_g=gx_g+(copy[k][m].rgbtGreen * y[a][b]);
					gy_r=gx_r+(copy[k][m].rgbtRed * y[a][b]);

					if (b == 2)
					{
						a++;
						b=0;
					}
					else
					{
						b++;
					}
				}
			}


			float gb=sqrt((gx_b*gx_b)+(gy_b*gy_b));
			int rounded_gb=round(gb);
			if (rounded_gb > 255)
			{
				rounded_gb=255;
			}
			image[i][j].rgbtBlue=rounded_gb;
			//printf("gx_b=%d\n",gx_b);
			// printf("gy_b=%d\n",gy_b);
			//printf(" \nrounded_gb[%d][%d]=%d\n " , i,j,rounded_gb);

			float gg=sqrt((gx_g*gx_g)+(gy_g*gy_g));
			int rounded_gg=round(gg);
			if (rounded_gg > 255)
			{
				rounded_gg=255;
			}
			image[i][j].rgbtGreen=rounded_gg;

			float gr=sqrt((gx_r*gx_r)+(gy_r*gy_r));
			int rounded_gr=round(gr);
			if (rounded_gr > 255)
			{
				rounded_gr=255;
			}
			image[i][j].rgbtRed=rounded_gr;
		}
	}
	return;
}
Posted
Updated 29-Jan-23 7:36am
v4

1 solution

I suggest to define the call as follows, because matrices with variable arguments are not supported in C and C++.
C
void edges(int height, int width, RGBTRIPLE **image)
//void edges(int height, int width, RGBTRIPLE image[height][width])
{
  // RGBTRIPLE **copy[height][width];
  RGBTRIPLE **copy = AllocRGBTripple(height, width);

  for (int i=0; i<height; i++) { ...

The call would then look like this:
C
int height = 20, width = 20;
RGBTRIPLE **image = AllocRGBTripple(height, width);
edges(height, width, image);

With the function AllocRGBTripple() you would then of course have to get enough memory e.g. with calloc() and initialize it. If the memory is no longer needed it should be freed with free().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900