【问题标题】:Cs50's Problem Set 4 - Filter Less - Blur FunctionCs50 的问题集 4 - Filter Less - Blur Function
【发布时间】:2020-08-19 08:34:46
【问题描述】:

所以我一直在努力纠正我的代码以修复 pset4 的模糊功能,并觉得我的代码是正确的(即使它不是最佳效率)。

我是这样处理的:

  1. 首先,我遍历了库存图片的高度和宽度 2 个 for 循环。

  2. 将像素计数器设置为 0 以计算周围有效像素的数量 由第 i 行的第 j 个像素定义的任何特定像素。

  3. 我设置了两个 for 循环来遍历 3x3 网格状结构 围绕有问题的像素。这里起作用的变量是 k 和 l. K 必须在 i 上方 1 行(因此 k = i-1),在 i 下方结束 1 行 (因此 k

  4. 在“3”中所述的 for 循环内。我使用“if循环”来确定 在 3x3 网格中迭代的像素是否存在 - 通过 说明 k 必须大于 -1 且小于“高度”并且 l 同上。

  5. pixelcounter++ 添加到周围的有效像素总数 第 [i][j] 个像素。

  6. 退出凌乱的循环,确保所有像素都被计算在内,数组 pixelcolor 声明为 pixelcounter 的大小。

  7. 我使用与步骤 3 中相同的循环来循环 3x3 围绕第 [i][j] 个像素的像素网格,具有相同的 if 健康)状况。只有这一次我使用 z 将它嵌套在一个 for 循环中 我的像素计数器,这样它就可以遍历一维数组 像素颜色,在它的第 z 个位置存储 [k][l] 处的图像 RGB 颜色(如果有效)。

  8. 我声明了 3 个变量 - rawred, rawblue, rawgreen 的目的 只是将红色绿色和蓝色的值相加。

  9. For 循环执行第 8 步。

  10. 然后我初始化 RGB 分量的平均值 avgred/avgblue/avggreen 与 rawred/rawblue/rawgreen 浮点数分开 由 pixelcounter 转换为浮点数。将结果四舍五入给出 整数值。

  11. 然后我将这些整数值输入到第 [i][j] 个像素中 像素。

代码如下:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // Looping through height of the image
    for (int i = 0; i < height; i++)
    {
        // Looping through the individual pixels in each row
        for (int j = 0; j < width; j++)
        {
            int pixelcounter = 0;
            // Looping through a 3x3 pixel grid surrounding of the individual pixel - Height
            for (int k = i - 1; k <= i + 1; k++)
            {
                // Looping through individual pixels within the kth row
                for (int l = j - 1; l <= j + 1; l++)
                {
                    // Counting the number of valid pixels in the 3x3 grid
                    if ((k > -1) && (k < height) && (l > -1) && (l < width))
                    {
                        pixelcounter++;
                    }
                }
            }
            
            RGBTRIPLE pixelcolour[pixelcounter];
            // Looping through array 3x3 pixel grid surrounding the individual pixel - height
            for (int z = 0; z < pixelcounter; z++)
            {
                for (int k = i - 1; k <= i + 1; k++)
                {
                    for (int l = j - 1; l <= j + 1; l++)
                    {
                        // Storing valid pixels in an array of valid pixels
                        if ((k > -1) && (k < height) && (l > -1) && (l < width))
                        {
                        
                            pixelcolour[z] = image[k][l];
                        
                        }
                    }
                }
            }
            
            // adding all RGB components
            float rawred = 0;
            float rawblue = 0;
            float rawgreen = 0;
            for (int a = 0; a < pixelcounter; a++)
            {
                rawred = rawred + pixelcolour[a].rgbtRed;
                rawblue = rawblue + pixelcolour[a].rgbtBlue;
                rawgreen = rawgreen + pixelcolour[a].rgbtGreen;
            }
            
            // Calculating average values of RGB component
            int avgred = round(rawred / (float) pixelcounter);
            int avgblue = round(rawblue / (float) pixelcounter);
            int avggreen = round(rawgreen / (float) pixelcounter);
            
            // Dereferencing original pixel colour to new colour
            image[i][j].rgbtRed = avgred;
            image[i][j].rgbtBlue = avgblue;
            image[i][j].rgbtGreen = avggreen;
        }
    }

    return;
}

P.S:我知道可能有一种更有效的方法可以做到这一点,但我真的很想看看这段代码到底哪里出了问题。它编译,最终结果图片奇怪地移到了左角。没有什么是模糊的,整个图片移动了一个像素,并且没有不应该存在的像素(具有随机颜色的杂散像素)。

编辑 1: 以下是我收到的错误:

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "145 160 169\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "90 106 116\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n90 1..."

【问题讨论】:

  • 这个循环没有任何意义:for (int z = 0; z &lt; pixelcounter; z++)。您将为每个像素执行内部循环,并始终将最后一个像素存储在 pixelcolour 数组的每个元素中。删除该循环,只需设置 int z=0; 并将 pixelcolour[z] = image[k][l]; 替换为 pixelcolour[z++] = image[k][l];
  • 好的,我取消了 for 循环,添加了你说的代码行。我应该将块嵌套在 while 为 (z
  • 你为什么需要那个?您应该得到与上述循环相同的结果。为了使它更容易,您可以简单地定义具有固定大小的数组 [9] 并在您执行 pixelcounter++ 的同一位置执行所有操作@
  • 不知道问题这么小,谢谢。模糊了!至少在视觉上是这样。我仍然遇到一些错误。仍然比所有错误都要好。我会将这些错误放在编辑中。
  • 我解决了这个问题,谢谢!

标签: c filter cs50


【解决方案1】:

解决了一直困扰我的问题。我意识到模糊功能受到周围模糊像素的影响,解决方案是使用临时数组创建图像的副本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-11
    • 2020-12-05
    • 1970-01-01
    • 2022-11-22
    • 2020-09-15
    • 2022-07-11
    • 2013-11-20
    • 2020-09-28
    相关资源
    最近更新 更多