CS50 - 过滤(更舒服) Edges - 只有蓝色值是错误的

CS50 - filter (more comfortable) Edges - only blue value is wrong

我在尝试解决 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;
    }

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

    :( 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..."

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

你的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);