【问题标题】:Having trouble with Box Blur Edges盒子模糊边缘有问题
【发布时间】:2012-01-02 13:47:48
【问题描述】:

我是一个新手爱好者,试图对框模糊进行编程,但我在边缘方面遇到了麻烦。我希望有人能发现错误。

边缘是黑色的,我假设这是因为边界没有被正确反射。我确信这已经与固定大小的内核进行了讨论,但是我使用的是可变大小的内核。

我正在使用在另一篇文章中找到的代码 --

Optimized float Blur variations

但是我就是不明白反射边框部分。

我不关心是否优化,也不关心其他内核形状,盒子形状就可以了。

代码是

        {// code from https://stackoverflow.com/questions/7860575/optimized-float-blur-variations

            //--------------------------------------
                int image_width ;
                int image_height ;
                int scale = 0;

                int weight = (radius * 2) + 1;

                int kernel_X = 0;
                int kernel_Y = 0;

                //--------------------------------------
                float sum = 0.0;

                int kernel_width = radius;//set both to the same to make the kernel square
                int kernel_height = radius;//set both to the same to make the kernel square


                // HORIZONTAL
                for(iy = 0; iy < image_height ;iy++)
                {
                    sum = 0.0;

                    // Process entire window for first pixel (including wrap-around edge)
                    for (kernel_X = 0; kernel_X <= kernel_width; kernel_X++)
                    {
                        if (kernel_X >= 0 && kernel_X < image_width)
                            //sum += src[iy * image_width ];
                            sum += src[iy * image_width + kernel_X];
                    }       

    //>--------------    border code does not reflect edges HELP!!
                    // Wrap  watch for left side of image & resulting black bar
                    for (kernel_X = (image_width - kernel_width); kernel_X < image_width; kernel_X++)
                    {
                        //  if (kernel_X >= 0 && kernel_X < image_width)// HORIZONTAL               width  = horizontal = X
                        //  sum += src[iy * kernel_width + image_width ];//<-------------------enter tester formula here
                        //  sum += src[iy + ix * image_width + kernel_X];//<-------------------FAIL
                        //  sum += src[iy * kernel_width + image_width ];//<-------------------streaky
                    }



                    // Store first window
                    tmp[iy * image_width] = (sum / weight );

                    for(ix = 1; ix < image_width; ix++)
                    {
                        // Subtract pixel leaving window
                        if (ix - kernel_width - 1 >= 0)
                            sum -= src[iy * image_width + ix - kernel_width - 1];

                        // Add pixel entering window
                        if (ix + kernel_width < image_width)
                            sum += src[iy * image_width + ix + kernel_width];
                        else
                            sum += src[iy * image_width + ix + kernel_width - image_width];

                        tmp[iy * image_width + ix] = (sum / weight);//just for testing
                    }
                }


                // VERTICAL
                for(ix = 0; ix < image_width; ix++)
                {
                    sum = 0.0;

                    // Process entire window for first pixel
                    for (kernel_Y = 0; kernel_Y <= kernel_height; kernel_Y++)
                    {
                        if (kernel_Y >= 0 && kernel_Y < image_height)
                            sum += tmp[kernel_Y * image_width + ix];
                    }
    //>--------------    border code does not reflect edges HELP!!
                    // Wrap  watch for top side of image & resulting black bar
                    for (kernel_Y = image_height-kernel_height; kernel_Y < kernel_height; kernel_Y++)
                    {
                        //if (kernel_Y >= 0 && kernel_Y < image_height)
                        //  sum += tmp[(iy + kernel_height - image_height) * image_width + ix];
                    }
                    for(iy=1;iy< image_height  ;iy++)
                    {
                        // Subtract pixel leaving window
                        if (iy-kernel_height-1 >= 0)
                            sum -= tmp[(iy - kernel_height-1) * image_width + ix];

                        // Add pixel entering window
                        if (iy + kernel_height < image_height)
                            sum += tmp[(iy + kernel_height) * image_width + ix];
                        else
                            sum += tmp[(iy + kernel_height - image_height) * image_width + ix];

                        dst[ (scale * image_width * image_height) + (iy * image_width + ix) ] = (sum / weight);

                    }
                }
        }

感谢您对此提供的任何帮助。

谢谢 约翰

编辑这里是边缘图像示例的一些链接。

具有适当框模糊的图像 http://img687.imageshack.us/img687/931/standardboxblur.jpg

使用上述代码边缘不正确的图像(注意顶部和左侧边缘的黑条,底部和右侧也不完全正确) http://img202.imageshack.us/img202/5137/boxblurbadedges.jpg

【问题讨论】:

  • 您可能希望将其放入Signal Processing 社区以获得更好的答案。
  • 你能发布一些图片来更好地理解确切的问题吗?

标签: c image-processing computer-vision filtering blur


【解决方案1】:

如果您将采样放入一个单独的例程中,这可能是最简单的,该例程给定 x 和 y 坐标,返回像素值。然后,您可以进行一些检查并将 x 和 y 值分别限制在 0 和宽度以及 0 和高度之间。然后您可以安全地传递负值或大于宽度或高度的值。它还允许您更轻松地尝试其他方案,例如反射、钳制颜色、外推等。只需将钳制的采样函数换成具有其他行为的采样函数即可。

【讨论】:

  • 另外,您可能需要查找总面积表。它们可以更快,有时更容易实施。
猜你喜欢
  • 2016-08-26
  • 2016-10-07
  • 1970-01-01
  • 2020-06-15
  • 2017-03-24
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多