【问题标题】:Applying high pass filter on image in c#在c#中对图像应用高通滤波器
【发布时间】:2016-05-15 15:49:49
【问题描述】:

在关注this的文章之后,我了解到可以通过从原始图像中减去低通图像来获得高通图像。我首先将图像变灰,然后在图像上应用以下滤镜(如文章中所述)。

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

现在只要应用这个过滤器,就可以得到一个检测边缘的图像。

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

我需要以下结果

但我得到了这个结果

我需要这方面的帮助,我怎样才能获得第一张图片。

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

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

  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;
    }

【问题讨论】:

    标签: c# image-processing


    【解决方案1】:

    引用文章:

    请注意,生成的高通滤波器的所有元素的总和是 总是零。

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

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

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

    【讨论】:

      【解决方案2】:

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

      要获得相同的图像,您应该使用 128 发起 filterResult

      int filterResult = 128;
      

      【讨论】:

      • 嗨,你能再解释一下吗?我没明白你的意思。我应该在上面的代码中进行哪些更改?是检查 128 而不是 255 的条件吗?
      • 不,我的意思是从 int filterResult = 0 更改第 8 行;到 int filterResult = 128;
      猜你喜欢
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2014-07-29
      • 2015-07-28
      相关资源
      最近更新 更多