【解決方法】CS50 pset-4、フィルターのエッジ検出機能により快適に


こんにちは、CS50 をやったことがある人はいますか? これは pset4 の「filter-more comfort」によるもので、エッジ検出機能を書き込めません。 私は何を間違っていますか? どんな助けでも大歓迎です!

私が試したこと:

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

解決策 1

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++) { ...

呼び出しは次のようになります。

int height = 20, width = 20;
RGBTRIPLE **image = AllocRGBTripple(height, width);
edges(height, width, image);

関数 AllocRGBTripple() を使用すると、もちろん calloc() などで十分なメモリを取得して初期化する必要があります。 メモリが不要になった場合は、free() で解放する必要があります。

コメント

タイトルとURLをコピーしました