【问题标题】:edges produces right image, but check50 fails边缘产生正确的图像,但 check50 失败
【发布时间】:2021-10-22 17:01:43
【问题描述】:

我遇到了 helpers.c 的边缘功能问题

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

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

check50 参考链接:“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;
    }
}

【问题讨论】:

    标签: cs50


    【解决方案1】:

    我尝试用黑框创建一个较大的图像,然后将较小的图像插入到它的中心......

                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 被放置在大图的右下角,下方和右侧没有框架

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 2017-01-20
      • 2020-05-10
      • 2014-07-18
      相关资源
      最近更新 更多