简体   繁体   English

CS50 pset4 中的模糊 function 无法正常工作

[英]Blur function in CS50 pset4 not fully working

I've spent two days trying to correct my function for blurring a given image, but despite extensive proofreading it now only works correctly for corner cases.我花了两天时间试图纠正我的 function 模糊给定图像,但尽管进行了广泛的校对,它现在只适用于极端情况。 For the rest, it creates a discrepacy of 2-20+ in RGB values.对于 rest,它会在 RGB 值中产生 2-20+ 的差异。

The task is part of Harvard's CS50 course (more info on pset4 https://cs50.harvard.edu/x/2020/psets/4/filter/less/ ).该任务是哈佛 CS50 课程的一部分(有关 pset4 https://cs50.harvard.edu/x/2020/psets/4/filter/less/的更多信息)。

I've read everything I could find online and tried using those hacks like dividing new RGB values with a float, copying results directly back into the original image, tweaking the if condition, but that didn't help and I still have no idea what's wrong.我已经阅读了我可以在网上找到的所有内容,并尝试使用这些技巧,例如将新的 RGB 值与浮点数相除、将结果直接复制回原始图像、调整 if 条件,但这并没有帮助,我仍然不知道是什么错误的。 Would greatly appreciate help, thank you!非常感谢帮助,谢谢!

 // Blur image
 void blur(int height, int width, RGBTRIPLE image[height][width])
 {
    float new_red, new_blue, new_green;
    new_red = new_blue = new_green = 0;

    int count = 0;

    // Copy the image
    RGBTRIPLE temp[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j];
        }
    }

    // Loop through height
    for (int i = 0; i < height; i++)
    {
        // Loop through width
        for (int j = 0; j < width; j++)
        {
            // Loop through rows around a pixel
            for (int k = -1; k <= 1; k++)
            {
                // Loop through columns around a pixel
                for (int m = -1; m <= 1; m++)
                {
                    if (i + k >= 0 && i + k < height && j + m >= 0 && j + m < width)
                    {
                        count++;
                        new_red += temp[i + k][j + m].rgbtRed;
                        new_blue += temp[i + k][j + m].rgbtBlue;
                        new_green += temp[i + k][j + m].rgbtGreen;
                    }
                }
            }

            temp[i][j].rgbtBlue = round(new_blue / count);
            temp[i][j].rgbtRed = round(new_red / count);
            temp[i][j].rgbtGreen = round(new_green / count);
        }

    }

    // Copy the blurred image to original file
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = temp[i][j];
        }
    }

    return;
}

I assume you'll want to reset count, new_blue, new_red, and new_green for each pixel.我假设您要为每个像素重置计数、new_blue、new_red 和 new_green。 In your code these values continue to grow as you process the the image.在您的代码中,这些值会随着您处理图像而继续增长。

// Loop through height
for (int i = 0; i < height; i++)
{
    // Loop through width
    for (int j = 0; j < width; j++)
    {
        count = new_blue = new_red = new_green = 0;

One way you could have caught this while debugging is by printing out the values of these variables for each pixel before doing the division and assignment.在调试时你可能会发现这一点的一种方法是在进行除法和赋值之前打印出每个像素的这些变量的值。 You might have noticed that the count value was too high.您可能已经注意到计数值太高了。

I believe another issue is that you are blurring pixels in place in your temporary image.我相信另一个问题是您正在模糊临时图像中的像素。 When you blur a pixel you'll be using the already-blurred values of the pixels above it.当您模糊一个像素时,您将使用其上方像素已经模糊的值。 Instead you probably want to use the original image in your inner-most loop:相反,您可能想在最里面的循环中使用原始图像:

                    new_red += image[i + k][j + m].rgbtRed;
                    new_blue += image[i + k][j + m].rgbtBlue;
                    new_green += image[i + k][j + m].rgbtGreen

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

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