首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CS50模糊函数

CS50模糊函数
EN

Stack Overflow用户
提问于 2022-08-13 18:28:47
回答 1查看 98关注 0票数 1

我试图创建一个模糊函数,但它返回错误的输出。这个函数失败了所有的检查,我不明白为什么:

代码语言:javascript
复制
:( blur correctly filters middle pixel
    expected "127 140 149\n", not "143 158 168\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "96 114 126\n"
:( blur correctly filters pixel in corner
    expected "70 85 95\n", not "93 113 127\n"
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "93 113 127\n96..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "93 113 127\n96..."

如果有人能检查我的代码并帮助我识别错误,我将非常感激:

代码语言:javascript
复制
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];

    int offsetx[] = {0, 1, 1, 1, 0, -1, -1, -1};
    int offsety[] = {-1, -1, 0, 1, 1, 1, 0, -1};

    for (int row = 0; row < height; row++)
    {
        for (int col = 0; col < width; col++)
        {
            int sum_Red = 0;
            int sum_Green = 0;
            int sum_Blue = 0;
            int counter = 0;

            for(int i = 0; i < 9; i++)
            {
                int r = row + offsetx[i];
                int c = col + offsety[i];


                 if (r >= 0 && r < height && c >= 0 && c < width)
                 {
                    sum_Red += image[r][c].rgbtRed;
                    sum_Green += image[r][c].rgbtGreen;
                    sum_Blue += image[r][c].rgbtBlue;
                    counter++;

                 }
            }

            copy[row][col].rgbtRed = round(sum_Red / (double)counter);
            copy[row][col].rgbtGreen = round(sum_Green / (double)counter);
            copy[row][col].rgbtBlue = round(sum_Blue / (double)counter);
         }
    }

    for (int row = 0; row < height; row++)
    {
        for (int col = 0; col < width; col++)
        {
            image[row][col] = copy[row][col];
        }
    }
    return;
}

谢谢你的时间和帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-14 08:28:31

我的问题已经在评论中得到了回答,但我将留在这里,以防有人错过答案。在应用了所有建议之后,仍然需要为偏移数组添加一个条目,如下所示:

代码语言:javascript
复制
  int offsetx[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
  int offsety[] = {-1, -1, 0, 1, 1, 1, 0, -1, 0};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73346747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档