微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决CS50 - 滤镜更舒服 Edges - 只有蓝色值是错误的

我在尝试解决 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,1},{-2,2},{-1,1}};
        int Gy[3][3] = {{-1,-2,-1},{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..."

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

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。