You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50 Pset4 edges滤镜实现错误:蓝色通道计算异常求助

CS50 Filter 项目Edges函数问题排查

Check50检测结果

:) edges correctly filters middle pixel
:( 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

问题详情

问题出在helpers.c的edges函数中,其余文件(helpers.h、bmp.h、filter.c、Makefile)均为CS50提供的标准文件。

复现方法:使用任意.bmp格式图片,执行命令:

./filter -e INFILE.bmp OUTFILE.bmp

已排查过蓝色通道的拼写错误,且使用double类型存储中间计算值以避免溢出,但仍未找到问题根源,请求排查帮助。

Helpers.c中的Edges函数代码

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    int sobel[3][3] = 
    {
        {-1, 0, 1},
        {-2, 0, 2},
        {-1, 0, 1}
    };
    RGBTRIPLE edged[height][width];

    for (int i = 0; i<height; i++){
        for (int j = 0; j<width; j++){
            double GxRed = 0 , GxGreen = 0 , GxBlue = 0 , GyRed = 0 , GyGreen = 0 , GyBlue = 0;
            int boxRow[3][3] = {{i-1, i-1, i-1}, {i, i, i}, {i+1, i+1, i+1}}, boxCol[3][3] = {{j-1, j, j+1}, {j-1, j, j+1}, {j-1, j, j+1}};

            for (int x = 0; x<3; x++){
                for (int y = 0; y<3; y++){
                    if (boxRow[x][y] >= 0 && boxRow[x][y] < height && boxCol[x][y] >= 0 && boxCol[x][y] < width){
                        GxRed += image[boxRow[x][y]][boxCol[x][y]].rgbtRed * sobel[x][y];
                        GxGreen += image[boxRow[x][y]][boxCol[x][y]].rgbtGreen * sobel[x][y];
                        GxBlue += image[boxRow[x][y]][boxCol[x][y]].rgbtBlue * sobel[x][y];

                        GyRed += image[boxRow[x][y]][boxCol[x][y]].rgbtRed * sobel[y][x];
                        GyGreen += image[boxRow[x][y]][boxCol[x][y]].rgbtGreen * sobel[y][x];
                        GyBlue += image[boxRow[x][y]][boxCol[x][y]].rgbtBlue * sobel[y][x];
                    }
                }
            }
            edged[i][j].rgbtRed = round(sqrt(pow(GxRed, 2) + pow(GyRed, 2)));
            if (edged[i][j].rgbtRed > 255) {edged[i][j].rgbtRed = 255;}
            if (edged[i][j].rgbtRed < 0) {edged[i][j].rgbtRed = 0;}

            edged[i][j].rgbtGreen = round(sqrt(pow(GxGreen, 2) + pow(GyGreen, 2)));
            if (edged[i][j].rgbtGreen > 255) {edged[i][j].rgbtGreen = 255;}
            if (edged[i][j].rgbtGreen < 0) {edged[i][j].rgbtGreen = 0;}

            edged[i][j].rgbtBlue = round(sqrt(pow(GxBlue, 2) + pow(GyBlue, 2)));
            if (edged[i][j].rgbtBlue > 255) {edged[i][j].rgbtBlue = 255;}
            if (edged[i][j].rgbtBlue < 0) {edged[i][j].rgbtBlue = 0;}
        }
    }
    for (int i = 0; i<height; i++){
        for (int j = 0; j<width; j++){
            image[i][j] = edged[i][j];
        }
    }
    return;
}

内容的提问来源于stack exchange,提问作者Diegocodes22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 09:35:16