我有一个问题与边缘函数的帮助。c
生成"right"图像,但check50未能通过与它相关的所有检查
我尝试用黑色帧创建较大的图像,然后将较小的图像插入其中心,因此当程序计算GX和GY时,它只使用
check50链接参考:"https://submit.cs50.io/check50/87cfe472a6c49c1f212f91ac346ea1bb532806eb">
生成的图像我代码:
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Create temporary_image array and fill it with values
RGBTRIPLE temporary_image[height + 2][width + 2];
for (int i = 0; i < height + 2; i++)
{
for (int j = 0; j < width + 2; j++)
{
temporary_image[i][j].rgbtRed = 0;
temporary_image[i][j].rgbtGreen = 0;
temporary_image[i][j].rgbtBlue = 0;
}
}
// Copy image to temporary array
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temporary_image[i + 2][j + 2].rgbtRed = image[i][j].rgbtRed;
temporary_image[i + 2][j + 2].rgbtGreen = image[i][j].rgbtGreen;
temporary_image[i + 2][j + 2].rgbtBlue = image[i][j].rgbtBlue;
}
}
// Create gx and gy arrays
const int gx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
const int gy[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
// Iterate over image array
for (int i = 1; i < height + 2; i++)
{
for (int j = 1; j < width + 2; j++)
{
int rgx = 0, rgy = 0, ggx = 0, ggy = 0, bgx = 0, bgy = 0, t = 0;
// Set pixel values
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
rgx = rgx + (gx[t] * image[i + k][j + l].rgbtRed);
rgy = rgy + (gy[t] * image[i + k][j + l].rgbtRed);
ggx = ggx + (gx[t] * image[i + k][j + l].rgbtGreen);
ggy = ggy + (gy[t] * image[i + k][j + l].rgbtGreen);
bgx = bgx + (gx[t] * image[i + k][j + l].rgbtBlue);
bgy = bgy + (gy[t] * image[i + k][j + l].rgbtBlue);
t++;
}
}
image[i - 1][j - 1].rgbtRed = max_at_255(rgx, rgy);
image[i - 1][j - 1].rgbtGreen = max_at_255(ggx, ggy);
image[i - 1][j - 1].rgbtBlue = max_at_255(bgx, bgy);
}
}
return;
}
int max_at_255(int gx, int gy)
{
float g = sqrt(gx * gx + gy * gy);
int result = round(g);
if (result > 255)
{
return 255;
}
else
{
return result;
}
}
我尝试用黑色帧创建一个较大的图像,然后将较小的图像插入它的中心…
temporary_image[i + 2][j + 2].rgbtRed = image[i][j].rgbtRed; temporary_image[i + 2][j + 2].rgbtGreen = image[i][j].rgbtGreen; temporary_image[i + 2][j + 2].rgbtBlue = image[i][j].rgbtBlue;
如果将较小的图像插入到中间,则索引必须为temporary_image[i+1][j+1]
;对于+2
,image
被放置在大图的右下方,下面和右边没有帧。