CS50 PSet4-模糊滤镜,我正在获得黑色图像作为输出 [英] CS50 PSet4 - Blur Filter, I am getting black image as an output

查看:51
本文介绍了CS50 PSet4-模糊滤镜,我正在获得黑色图像作为输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为pset4,过滤器,cs50的模糊编写代码.但是我的输出图像是黑色的,或者根本无法给出正确的输出.我一直遇到这个问题.

I'm writing the code for pset4, filter, blur of cs50. But my output image is black, or it simply doesn't give the correct output. I keep running into this problem.

我的模糊功能出了什么问题?

What am I doing wrong in my blur function?

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    int sumRed;
    int sumGreen;
    int sumBlue;
    int counter;

    for (int i = 0; i < width; i++)
    {
        for (int n = 0; n < height; n++)
        {
            //Resetting sums to 0
            sumRed = 0;
            sumGreen = 0;
            sumBlue = 0;
            counter = 0;

            for (int x = -1; x < 2; x++)
            {

                if (x + i < 0 || x + i > width - 1)
                {
                    continue;
                }

                for (int y = -1; y < 2; y++)
                {
                    if (y + n < 0 || y + n > height - 1)
                    {
                        continue;
                    }

                    sumRed += temp[y + n][x + i].rgbtRed;
                    sumGreen += temp[y + n][x + i].rgbtGreen;
                    sumBlue += temp[y + n][x + i].rgbtBlue;
                    counter++;

                }

            }

            temp[n][i].rgbtRed = round(sumRed / counter);
            temp[n][i].rgbtGreen = round(sumGreen / counter);
            temp[n][i].rgbtBlue = round(sumBlue / counter);
        }
    }

    for (int k = 0; k < width; k++)
    {
        for (int l = 0; l < height; l++)
        {
            image[l][k].rgbtRed = temp[k][l].rgbtRed;
            image[l][k].rgbtGreen = temp[k][l].rgbtGreen;
            image[l][k].rgbtBlue = temp[k][l].rgbtBlue;
        }
    }

    return;
}

推荐答案

黑色图像的真正原因是 temp 未初始化.要初始化它,我们可以简单地将 image 复制到 temp 中.感谢@Gerhardh指出这一点.

The real cause of black image is that temp is not initialized. To initialize it, we can simply copy the image into temp. Thanks @Gerhardh for pointing this out.

RGBTRIPLE temp[height][width]; // create a temporary array to store a duplicate of image.

// save a new copy of image as temp per color.
for (int i = 0; i < height; i++) //Loop for height of image.
{
    for (int j = 0; j < width; j++) //Loop for width of image and save color values in temp.
    {
        temp[i][j] = image[i][j];
    }
}

您的算法中还有另一个问题.您对 temp 进行计算,并同时更改 temp ,这是错误的.因为随着 temp 的变化,将对修改后的像素进行新的像素模糊计算.计算应始终在原始图像上进行,然后应将图像分配给临时值.更详细的解释是

There is another issue in your algorithm. You do calculation on temp and change temp at the same time which is wrong. Because as temp changes, new pixel blur calculations will be done on modified pixels. Calculations should be always done on raw image, then image should be assigned to temp values. A more detailed explanation is here.

基本上,这段代码

temp[n][i].rgbtRed = round(sumRed / counter);
temp[n][i].rgbtGreen = round(sumGreen / counter);
temp[n][i].rgbtBlue = round(sumBlue / counter);

应该是这个,

image[n][i].rgbtRed = round(sumRed / counter);
image[n][i].rgbtGreen = round(sumGreen / counter);
image[n][i].rgbtBlue = round(sumBlue / counter);

并删除此内容,因为我们已经在计算后立即进行赋值了.

And remove this, since we already do assignment right after calculation.

for (int k = 0; k < width; k++)
{
    for (int l = 0; l < height; l++)
    {
        image[l][k].rgbtRed = temp[k][l].rgbtRed;
        image[l][k].rgbtGreen = temp[k][l].rgbtGreen;
        image[l][k].rgbtBlue = temp[k][l].rgbtBlue;
    }
}

这篇关于CS50 PSet4-模糊滤镜,我正在获得黑色图像作为输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆