Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cs50-pset4-模糊_C_For Loop_Filter_Cs50 - Fatal编程技术网

cs50-pset4-模糊

cs50-pset4-模糊,c,for-loop,filter,cs50,C,For Loop,Filter,Cs50,我已经在pset4-filter上工作了好几个小时了,模糊部分。我试图运行这段代码,但我不知道为什么,它会创建黑色图像。我试着浏览stack和其他一些网站,但找不到解决问题的方法。是否有人发现以下代码存在问题: // Blur image void blur(int height, int width, RGBTRIPLE image[height][width]) { RGBTRIPLE temp[height][width]; int counter; int su

我已经在pset4-filter上工作了好几个小时了,模糊部分。我试图运行这段代码,但我不知道为什么,它会创建黑色图像。我试着浏览stack和其他一些网站,但找不到解决问题的方法。是否有人发现以下代码存在问题:


// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    int counter;
    int sum_red;
    int sum_green;
    int sum_blue;
    for (int i = 0; i < height; i++)
    {
        counter = 0;
        sum_red = 0;
        sum_green = 0;
        sum_blue = 0;
        for (int j = 0; j < width; j++)
        {
            for (int r = -1; r < 2; r++)
            {
                if (r + i < 0 || r + i > height - 1)
                {
                    continue;
                }
                for (int c = -1; c < 2; c++)
                {
                    if (c + j < 0 || c + j > width - 1)
                    {
                        continue;
                    }
                    sum_red += temp[i + r][c + j].rgbtRed;
                    sum_green += temp[i + r][c + j].rgbtGreen;
                    sum_blue += temp[i + r][c + j].rgbtBlue;
                    counter++;

                }

            }
            image[i][j].rgbtRed = round(sum_red / counter);
            image[i][j].rgbtGreen = round(sum_green / counter);
            image[i][j].rgbtBlue = round(sum_blue / counter);
        }

    }
    return;
}

//模糊图像
无效模糊(整数高度、整数宽度、RGB三重图像[高度][宽度])
{
RGB三脚架温度[高度][宽度];
整数计数器;
int sum_red;
int sum_green;
int sum_蓝;
对于(int i=0;i高度-1)
{
继续;
}
for(int c=-1;c<2;c++)
{
如果(c+j<0 | | c+j>宽度-1)
{
继续;
}
和=温度[i+r][c+j].rgbtRed;
和绿色+=温度[i+r][c+j].rgbtGreen;
和蓝+=温度[i+r][c+j].rgbtBlue;
计数器++;
}
}
图像[i][j].rgbtRed=圆形(和红色/计数器);
图像[i][j].rgbtGreen=圆形(和绿色/计数器);
图像[i][j].rgbtBlue=圆形(和蓝色/计数器);
}
}
返回;
}

谢谢

您正在使用未初始化的数组。您应该将图像数据复制到
RGBTRIPLE temp[height][width]作为第一步

memcpy(temp, image, sizeof temp);
一个较小的错误是
round(sum\u red/counter)
,它在传递到
round()
之前进行整数除法和截断,然后什么也不做。我建议使用

float sum_red;
float sum_green;
float sum_blue;

尝试将
计数器
声明为浮点数
sum_red
sum_green
sum_blue
可以是整数

从一个小“图像”开始,在监视变量及其值的同时,使用调试器逐语句遍历代码语句。