edges 生成正确的图像,但 check50 失败

edges produces right image, but check50 fails

我对 helpers.c

的边缘函数有疑问

它生成了“正确”的图像,但 check50 未能通过与其相关的所有检查

我试着用黑框创建一个较大的图像,然后将较小的图像插入到它的中心,所以当程序对 GX 和 GY 进行计算时,它只使用那个

check50 link 供参考:“https://submit.cs50.io/check50/87cfe472a6c49c1f212f91ac346ea1bb532806eb”

generated image

我的代码:

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

I tried creating a larger images with a black frame and then inserting the smaller image in the center of it…

            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];对于 +2image 位于较大图像的右下角,下方和右侧没有 frame