C#使用FFT识别模糊图像

时间:2018-02-12 16:47:36

标签: c# image fft aforge

我正在寻找一种方法来识别C#中的图像是否模糊。我看到this post,但我没有看到适用于我案件的方法。

我找到AForge.dll将FFT应用于我的图片。我正在寻找一种简单的方法来确定图像是否模糊(我对数学不太满意)。

有我的代码:

Bitmap  Picture;

// I'm working with images sized between 130x130 and 150x150
Bitmap tmp = new Bitmap(pictureFile);

// Crop to be 128x128
Bitmap cropped = cropBitmap(tmp, 128, 128);
using (MemoryStream ms = new MemoryStream())
{
    cropped.Save(ms, ImageFormat.Gif);
    ms.Position = 0;
    // Save in grayscale
    Picture = new Bitmap(ms);
}

// Create the ComplexImage with AForge.dll
ComplexImage output = ComplexImage.FromBitmap(Picture);
// Apply FFT
output.ForwardFourierTransform();
Bitmap result = output.ToBitmap();

// to be continued...

1 个答案:

答案 0 :(得分:2)

这应该可以解决问题。

calcBlurriness()的结果越小(接近于零),图像越清晰。

using OpenCvSharp;    
namespace BlurDetectSO {
    class Program
    {

        static float calcBlurriness(Mat src)
        {
            Mat Gx = new Mat();
            Mat Gy = new Mat();
            Cv2.Sobel(src, Gx, MatType.CV_32F, 1, 0);
            Cv2.Sobel(src, Gy, MatType.CV_32F, 0, 1);
            double normGx = Cv2.Norm(Gx);
            double normGy = Cv2.Norm(Gy);
            double sumSq = normGx * normGx + normGy * normGy;
            return (float)(1.0 / (sumSq / (src.Size().Height * src.Size().Width) + 1e-6));
        }

        static void Main(string[] args)
        {
            Mat src = Cv2.ImRead("lenna.png", ImreadModes.GrayScale);
            Mat dst = new Mat();

            var blurIndex = calcBlurriness(src);

            //test: find edges...
            //Cv2.Canny(src, dst, 50, 200);
            //using (new Window("src image", src))
            //using (new Window("dst image", dst))
            //{Cv2.WaitKey();}
        }
    }
}

注意:

  • 正如您所看到的,我使用了.NET包装器OpenCVSharp(有些使用了Emgucv - 它看起来更复杂但可能更高级)。
  • 我做了一些测试。对于某些类型的图像,此方法不能很好地工作。我观察到包含某种自然噪声的图像存在一些问题,可以解释为模糊。
  • 这是我的第一次OpenCV努力。所以,要小心。改编自sample