在c#中对图像应用高通滤波器

时间:2016-05-14 10:35:54

标签: c# image-processing

在关注this文章后,我发现可以通过从原始图像中减去低通图像来实现高通图像。我首先将图像灰化,然后在图像上应用以下过滤器(如文章中所述)。

 return new int[,] {{-1, -2, -1},
                    {-2, 12, -2},
                    {-1, -2, -1}};

现在只是应用这个过滤器,给我一个检测边缘的图像。

this文章中,建议在对图像应用过滤器后除以16 。当我分开时,我得到一个溢出异常并得到一个负值存储在图像中。

我需要以下结果

enter image description here

但我得到了这个结果

enter image description here

我需要帮助,如何获得第一张图片。

通常我将结果除以滤镜数组的总和(如果模糊图像)。

这是在图像上应用滤镜蒙版的代码..

  public static byte[,] applyFilterMask(byte[,] imageArray, int filterSize, int[,] filterMask, uint width, uint height)
    {
        byte[,] masked2DImage = new byte[height, width];
        for (int row = (filterSize/2) ; row < height - (filterSize/2); row++)
        {
            for (int col = (filterSize / 2); col < width - (filterSize / 2); col++)
            {
                int filterResult = 0;

                // these nested loops are to apply (generic) filter mask
                for (int filterRow = 0; filterRow < filterMask.GetLength(0); filterRow++)
                {
                    for (int filterColumn = 0; filterColumn < filterMask.GetLength(1); filterColumn++)
                    {
                        filterResult += imageArray[row - (filterSize/2) + filterRow, col - (filterSize/2) + filterColumn] * filterMask[filterRow, filterColumn];
                    }
                }

                filterResult = sumOfFilterMask(filterMask) == 0 ? filterResult : (filterResult / sumOfFilterMask(filterMask));
                filterResult = filterResult > 255 ? 255 : (filterResult < 0 ? 0 : filterResult);
                masked2DImage[row, col] = Convert.ToByte(filterResult);
            }
        }       

        return masked2DImage;
    }

2 个答案:

答案 0 :(得分:1)

引用文章:

  

请注意,生成的高通滤波器的所有元素之和为   永远为零。

为什么你想要一个灰色的背景?因为你在文章中看到了一个?

文章中的图片是用相机拍摄的图像。相机照片总是有噪音。因此,您很难在9x9邻域中找到任何具有相同灰度值的区域。小的变化将导致非黑色背景。

你的形象似乎是人为的。一些没有噪音的数字艺术品。背景很可能具有相同的值。因此,过滤结果为零,结果背景为黑色。

答案 1 :(得分:1)

高通滤波器的结果在零附近变化,因此,为了使负值可见,它们在图像中显示0 +值作为128 +值。

要获得相同的图片,您应该使用128

启动filterResult
int filterResult = 128;