PSET4用于CS50X,模糊功能

发布于 2025-01-21 12:36:59 字数 2807 浏览 2 评论 0原文

我是编码的入门者,我开始使用CS50学习,但是我有问题,我写了模糊功能,而且看起来很好,图像实际上是模糊的,但是当我使用Test50(CS50上的代码测试工具)时,它说我没有正确处理RGB信息。请帮助我查看代码。 这是代码

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])//average rgb value of pixels arounds (9 if not on the edge)
{
    int a_blue = 0, a_green = 0, a_red =0;
    for(int i = 0; i < width; i++)
    {
        for(int j = 0; j < height; j++)
        {
            int left = i - 1, right = i + 1, upper = j - 1, down = j + 1, time = 0; //
            int blue = 0, red = 0, green = 0;
            _Bool cnd = (left >= 0 && right <= width && upper >= 0 && down <= height);
            for(int t1 = i-1; t1 <= i + 1; t1++)
            {
                if(!cnd)
                {
                    continue;
                }
                for(int t2 = j - 1; t2 <= j + 1; t2++)
                {
                    if(!cnd)
                    {
                        continue;
                    }
                    //sum of all blue around
                    blue = blue + image[t2][t1].rgbtBlue;;
                    //sum of all red around
                    red = red + image[t2][t1].rgbtRed;
                    //sum of all green around
                    green = green + image[t2][t1].rgbtGreen;
                    time++;
                }
            }
            a_blue = rounding((float)blue/time);
            a_green = rounding((float)green/time);
            a_red = rounding((float)red/time);

            for(int t1 = i-1; t1 <= i + 1; t1++)
            {
                if(!cnd)
                {
                    continue;
                }
                for(int t2 = j - 1; t2 <= j + 1; t2++)
                {
                    if(!cnd)
                    {
                        continue;
                    }
                    //sum of all blue around
                    image[t2][t1].rgbtBlue = a_blue;
                    //sum of all red around
                    image[t2][t1].rgbtRed = a_red;
                    //sum of all green around
                    image[t2][t1].rgbtGreen = a_green;

                }
            }


        }
    }
    return;
}

,这是我查看答案时test50和btw的结果

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "87 95 124\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "100 109 110\n"
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "127 140 149\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "127 140 149\n1..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "123 136 145\n1..."

,我发现有些人更喜欢键入每一行而不是使用循环,如果我想成为一名教授编码工程师,您能告诉我哪个是更好的编码习惯?

I'm a starter on coding, I started learning with cs50, but I have a problem, I wrote the blur function, and it seems worked fine, the image actually blurred, but when I use test50(code testing tool on cs50), it says I didn't process the RGB information correctly. Please help me view the code.
Here is the code

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])//average rgb value of pixels arounds (9 if not on the edge)
{
    int a_blue = 0, a_green = 0, a_red =0;
    for(int i = 0; i < width; i++)
    {
        for(int j = 0; j < height; j++)
        {
            int left = i - 1, right = i + 1, upper = j - 1, down = j + 1, time = 0; //
            int blue = 0, red = 0, green = 0;
            _Bool cnd = (left >= 0 && right <= width && upper >= 0 && down <= height);
            for(int t1 = i-1; t1 <= i + 1; t1++)
            {
                if(!cnd)
                {
                    continue;
                }
                for(int t2 = j - 1; t2 <= j + 1; t2++)
                {
                    if(!cnd)
                    {
                        continue;
                    }
                    //sum of all blue around
                    blue = blue + image[t2][t1].rgbtBlue;;
                    //sum of all red around
                    red = red + image[t2][t1].rgbtRed;
                    //sum of all green around
                    green = green + image[t2][t1].rgbtGreen;
                    time++;
                }
            }
            a_blue = rounding((float)blue/time);
            a_green = rounding((float)green/time);
            a_red = rounding((float)red/time);

            for(int t1 = i-1; t1 <= i + 1; t1++)
            {
                if(!cnd)
                {
                    continue;
                }
                for(int t2 = j - 1; t2 <= j + 1; t2++)
                {
                    if(!cnd)
                    {
                        continue;
                    }
                    //sum of all blue around
                    image[t2][t1].rgbtBlue = a_blue;
                    //sum of all red around
                    image[t2][t1].rgbtRed = a_red;
                    //sum of all green around
                    image[t2][t1].rgbtGreen = a_green;

                }
            }


        }
    }
    return;
}

And here's the result from test50

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "87 95 124\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "100 109 110\n"
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "127 140 149\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "127 140 149\n1..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "123 136 145\n1..."

And btw when I was viewing answers, I found that some prefer to type every line instead of using for loop, if I want to be a professonial coding engineer, could you tell me which is the better habit for coding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文