简体   繁体   English

cs50 - pset4 - 模糊

[英]cs50 - pset4 - blur

I've been working on pset4 - filter, the blur part for many hours now.我一直在研究 pset4 - 过滤器,模糊部分已经好几个小时了。 I'm trying to run this code, but I don't know why, it creates black images.我正在尝试运行此代码,但我不知道为什么,它会创建黑色图像。 I tried going over stack and some other websites but couldn't find a solution for my problem.我尝试查看堆栈和其他一些网站,但找不到解决我的问题的方法。 Does anyone see the problem with the code below:有没有人看到下面代码的问题:


// 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;
}

Thanks谢谢

You are working with an uninitialised array.您正在使用未初始化的数组。 You are supposed to copy the image data to RGBTRIPLE temp[height][width];您应该将图像数据复制到RGBTRIPLE temp[height][width]; as the first step.作为第一步。

memcpy(temp, image, sizeof temp);

A smaller fault is with round(sum_red / counter) which does integer division and truncation before being passed to round() , which then does nothing.一个较小的错误是round(sum_red / counter)在传递给round()之前进行整数除法和截断,然后什么都不做。 I suggest using我建议使用

float sum_red;
float sum_green;
float sum_blue;

Try to declare counter as a float.尝试将counter声明为浮点数。 sum_red , sum_green and sum_blue could be integers sum_redsum_greensum_blue可以是整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM