A.P*_*cat 6 c# image fft aforge
我正在寻找一种方法来识别 C# 中的图像是否模糊。我看到了这篇文章,但我没有看到适用于我的案例的方法。
我找到了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...
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题。
的结果越小(接近于零)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();}
}
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
归档时间: |
|
查看次数: |
5187 次 |
最近记录: |