【问题标题】:CS50 - filter (more comfortable) Edges - only blue value is wrongCS50 - 过滤器(更舒适)边缘 - 只有蓝色值是错误的
【发布时间】:2021-05-10 11:33:08
【问题描述】:

我在尝试解决 CS50 中的边缘检测问题时遇到了问题。

下面是我的代码:

        // Detect edges
    void edges(int height, int width, RGBTRIPLE image[height][width])
    {
        // Ask for some temparory memories for store blur pixels
         RGBTRIPLE temp[height][width];
         
         // Consider every condition you may encounter with pixels
        int GxR, GyR, GxG, GyG, GxB, GyB;
        
        // Initialize Gx and Gy metrix
        int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
        int Gy[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++)
            {
                GxR = GyR = GxG = GyG = GxB = GyB= 0;
    
                // Loop over 3x3 pixels
                for (int h = -1; h < 2; h++)
                {
                    for (int w = -1; w < 2; w++)
                    {
                        // Check if this pixel is outside the image
                        if (i + h < 0 || i + h > height - 1)
                        {
                            continue;
                        }
                        
                        if (j + w < 0 || j + w > width - 1)
                        {
                            continue;
                        }
                        
                        // sum each channel value
                        // X
                        GxR += image[i + h][j + w].rgbtRed * Gx[h + 1][w + 1];
                        GxG += image[i + h][j + w].rgbtGreen * Gx[h + 1][w + 1];
                        GxB += image[i + h][j + w].rgbtBlue * Gx[h + 1][w + 1];
                        
                        // Y
                        GyR += image[i + h][j + w].rgbtRed * Gy[h + 1][w + 1];
                        GyG += image[i + h][j + w].rgbtGreen * Gy[h + 1][w + 1];
                        GyB += image[i + h][j + w].rgbtBlue * Gy[h + 1][w + 1];
                    }
                }
                
                // Calculate every Gx and Gy value and store in temp
                temp[i][j].rgbtRed = round(sqrt((GxR * GxR  + GyR * GyR)));
                temp[i][j].rgbtGreen = round(sqrt((GxG * GxG + GyG * GyG)));
                temp[i][j].rgbtBlue = round(sqrt((GxB * GxB + GyB * GyB)));
                
                // Capped color value at 255
                if (temp[i][j].rgbtRed > 255)
                {
                    temp[i][j].rgbtRed = 255;
                }
                
                if (temp[i][j].rgbtGreen > 255)
                {
                    temp[i][j].rgbtGreen = 255;
                }
                
                if (temp[i][j].rgbtBlue > 255)
                {
                    temp[i][j].rgbtBlue = 255;
                }
            }
        }       
    
        // Ready to iterate whole image from temp to image[i][j]
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                image[i][j] = temp[i][j];
            }
        }
        
        return;
    }

当我运行 check50 时,结果显示像素的红色和绿色值是正确的,但只有蓝色是错误的。结果如下:

    :( edges correctly filters pixel on edge
        expected "213 228 255\n", not "213 228 140\n"
    :( edges correctly filters pixel in corner
        expected "76 117 255\n", not "76 117 66\n"
    :( edges correctly filters 3x3 image
        expected "76 117 255\n21...", not "76 117 66\n213..."
    :( edges correctly filters 4x4 image
        expected "76 117 255\n21...", not "76 117 66\n213..."

谁能告诉我我的代码有什么问题?
我已经尽力调试了,但还是找不到哪里出错了...

【问题讨论】:

  • 欢迎来到 SO。你检查过其他像素吗?可能只有第一个像素 R 和 G 是正确的,而其他像素有更多的错误。
  • 此外,测试结果通常包含有关测试的更多详细信息。您可以查看输入数据并将它们输入到您的函数中。然后在你的调试器中一步一步地执行它,并将值与你期望的值进行比较。

标签: c image-processing cs50 edge-detection


【解决方案1】:

您的edges 函数几乎是正确的。你只是错过了

                // Calculate every Gx and Gy value and store in temp
                temp[i][j].rgbtRed = round(sqrt((GxR * GxR  + GyR * GyR)));
                temp[i][j].rgbtGreen = round(sqrt((GxG * GxG + GyG * GyG)));
                temp[i][j].rgbtBlue = round(sqrt((GxB * GxB + GyB * GyB)));

rgbtRedrgbtGreenrgbtBlue 成员只有 8 位,从 256 开始分配浮点类型值的行为是未定义的;以下限制值的代码不起作用。所以在分配它们之前限制 RGB 值:

                temp[i][j].rgbtRed   = fmin(round(sqrt(GxR * GxR + GyR * GyR)), 255);
                temp[i][j].rgbtGreen = fmin(round(sqrt(GxG * GxG + GyG * GyG)), 255);
                temp[i][j].rgbtBlue  = fmin(round(sqrt(GxB * GxB + GyB * GyB)), 255);

【讨论】:

  • 我完全忘了检查值的类型。在我听取了您的建议后,程序运行正常。非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 2020-11-13
相关资源
最近更新 更多