Box Blur Edges有问题

时间:2012-01-02 13:47:48

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

我是一个新手爱好者,试图编制一个盒子模糊,我在边缘方面遇到麻烦。我希望有人能发现错误。

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

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

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

1 个答案:

答案 0 :(得分:2)

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