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

CS50 Filter作业Blur函数check50验证失败,求问题排查

CS50 Filter作业Blur函数调试问题

我在完成CS50的Filter作业时,Blur函数无法通过check50测试,RGB平均值计算错误。以下是check50的错误信息:

blur correctly filters middle pixel

expected "127 140 149\n", not "120 140 150\n":
testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 0...
checking for output "127 140 149\n"...
blur correctly filters pixel on edge

expected "127 140 149\n", not "120 140 150\n":
testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 0...
checking for output "127 140 149\n"...
blur correctly filters 3x3 image

expected "70 85 95\n80 9...", not "70 85 95\n76 9...":
testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 3...
checking for output "70 85 95\n80 95 105\n90 105 115\n117 130 140\n127 140 149\n137 150 159\n163 178 188\n170 185 194\n178 193 201\n"...
blur correctly filters 4x4 image

expected "70 85 95\n80 9...", not "70 85 95\n76 9...":
testing with sample 4x4 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90), (100, 110, 120)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160), (140, 160, 170)
third row: (195, 204, 213), (205, 214, 223), (225, 234, 243), (245, 254, 253)
fourth row: (50, 28, 90), (0, 0, 0), (255, 255, 255), (85, 85, 85)
running ./testing 3 4...
checking for output "70 85 95\n80 95 105\n100 115 125\n110 125 135\n113 126 136\n123 136 145\n142 155 163\n152 165 173\n113 119 136\n143 151 164\n156 166 171\n180 190 194\n113 112 132\n155 156 171\n169 174 177\n203 207 209\n"...

我编写的Blur函数代码如下:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];
    float sumRed = 0;
    float sumGreen = 0;
    float sumBlue = 0;
    float counter = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = image[i][j];

            for (int k = -1; k < 2; k++)
            {
                for (int l = -1; l < 2; l++)
                {
                    if (i + k < 0 || i + k > height - 1)
                    {
                        continue;
                    }
                    else if (j + l < 0 || j + l > width - 1)
                    {
                        continue;
                    }
                    sumRed += image[i + k][j + l].rgbtRed;
                    sumGreen += image[i + k][j + l].rgbtGreen;
                    sumBlue += image[i + k][j + l].rgbtBlue;
                    counter++;
                }
            }
            copy[i][j].rgbtRed = round(sumRed / counter);
            copy[i][j].rgbtGreen = round(sumGreen / counter);
            copy[i][j].rgbtBlue = round(sumBlue / counter);
        }
    }
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = copy[i][j].rgbtRed;
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
            image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
        }
    return;
    }
}

我尝试过用cs50.ai排查,但它认为只有Red值计算错误,这和check50的结果不符。我不确定RGB值的计算逻辑是否正确,也不知道如何继续修正,希望得到帮助。


问题分析与修正方案

你的代码存在两个核心问题:

  1. 累加变量未重置:sumRed、sumGreen、sumBlue和counter定义在最外层循环外,导致每个像素计算时会累加之前所有像素的求和结果,而非仅当前像素周围的像素值。比如计算第二个像素时,sumRed已经包含了第一个像素周围所有红色值的总和,这直接导致平均值完全错误。

  2. 提前返回导致复制不完整:在最后将copy数组内容复制回image的循环中,return语句被放在内层循环(j循环)里,这意味着只复制完第一行的所有列后,函数就直接返回了,后续行的像素完全没有被更新。

修正后的代码

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // 每个像素计算前重置求和变量
            float sumRed = 0;
            float sumGreen = 0;
            float sumBlue = 0;
            float counter = 0;

            for (int k = -1; k < 2; k++)
            {
                for (int l = -1; l < 2; l++)
                {
                    if (i + k < 0 || i + k > height - 1)
                    {
                        continue;
                    }
                    if (j + l < 0 || j + l > width - 1)
                    {
                        continue;
                    }
                    sumRed += image[i + k][j + l].rgbtRed;
                    sumGreen += image[i + k][j + l].rgbtGreen;
                    sumBlue += image[i + k][j + l].rgbtBlue;
                    counter++;
                }
            }
            // 计算当前像素的模糊值
            copy[i][j].rgbtRed = round(sumRed / counter);
            copy[i][j].rgbtGreen = round(sumGreen / counter);
            copy[i][j].rgbtBlue = round(sumBlue / counter);
        }
    }
    // 完整复制所有像素到原图像,return移到循环外
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = copy[i][j].rgbtRed;
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
            image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
        }
    }
    return;
}

内容的提问来源于stack exchange,提问作者Jötunn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 15:44:53