我正在寻找一种方法来识别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...
答案 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();}
}
}
}
注意: