OpenCvSharp傅里叶变换与逆变换

using System;
using OpenCvSharp;
using SampleBase;
 
namespace SamplesCS
{
    /// <summary>
    /// DFT, inverse DFT
    /// http://stackoverflow.com/questions/19761526/how-to-do-inverse-dft-in-opencv
    /// </summary>
    class DFT : ISample
    {
        public void Run()
        {
            Mat img = Cv2.ImRead(FilePath.Image.Lenna, ImreadModes.GrayScale);
 
            // expand input image to optimal size
            Mat padded = new Mat(); 
            int m = Cv2.GetOptimalDFTSize(img.Rows);
            int n = Cv2.GetOptimalDFTSize(img.Cols); // on the border add zero values
            Cv2.CopyMakeBorder(img, padded, 0, m - img.Rows, 0, n - img.Cols, BorderTypes.Constant, Scalar.All(0));
             
            // Add to the expanded another plane with zeros
            Mat paddedF32 = new Mat();
            padded.ConvertTo(paddedF32, MatType.CV_32F);
            Mat[] planes = { paddedF32, Mat.Zeros(padded.Size(), MatType.CV_32F) };
            Mat complex = new Mat();
            Cv2.Merge(planes, complex);         
 
            // this way the result may fit in the source matrix
            Mat dft = new Mat();
            Cv2.Dft(complex, dft);            
 
            // compute the magnitude and switch to logarithmic scale
            // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
            Mat[] dftPlanes;
            Cv2.Split(dft, out dftPlanes);  // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
 
            // planes[0] = magnitude
            Mat magnitude = new Mat();
            Cv2.Magnitude(dftPlanes[0], dftPlanes[1], magnitude);
 
            magnitude += Scalar.All(1);  // switch to logarithmic scale
            Cv2.Log(magnitude, magnitude);
 
            // crop the spectrum, if it has an odd number of rows or columns
            Mat spectrum = magnitude[
                new Rect(0, 0, magnitude.Cols & -2, magnitude.Rows & -2)];
 
            // rearrange the quadrants of Fourier image  so that the origin is at the image center
            int cx = spectrum.Cols / 2;
            int cy = spectrum.Rows / 2;
 
            Mat q0 = new Mat(spectrum, new Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
            Mat q1 = new Mat(spectrum, new Rect(cx, 0, cx, cy));  // Top-Right
            Mat q2 = new Mat(spectrum, new Rect(0, cy, cx, cy));  // Bottom-Left
            Mat q3 = new Mat(spectrum, new Rect(cx, cy, cx, cy)); // Bottom-Right
 
            // swap quadrants (Top-Left with Bottom-Right)
            Mat tmp = new Mat();                           
            q0.CopyTo(tmp);
            q3.CopyTo(q0);
            tmp.CopyTo(q3);
 
            // swap quadrant (Top-Right with Bottom-Left)
            q1.CopyTo(tmp);                    
            q2.CopyTo(q1);
            tmp.CopyTo(q2);
 
            // Transform the matrix with float values into a
            Cv2.Normalize(spectrum, spectrum, 0, 1, NormTypes.MinMax); 
                                      
            // Show the result
            Cv2.ImShow("Input Image"       , img);
            Cv2.ImShow("Spectrum Magnitude", spectrum);
 
            // calculating the idft
            Mat inverseTransform = new Mat();
            Cv2.Dft(dft, inverseTransform, DftFlags.Inverse | DftFlags.RealOutput);
            Cv2.Normalize(inverseTransform, inverseTransform, 0, 1, NormTypes.MinMax);
            Cv2.ImShow("Reconstructed by Inverse DFT", inverseTransform);
            Cv2.WaitKey();
        }
    }
}

原文地址

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCVSharp提供了一些图像质量评价的方法,可以用于评估图像的清晰度、对比度等指标。以下是一些常用的方法: 1. Laplacian算子评价图像清晰度 ```csharp using OpenCvSharp; Mat src = Cv2.ImRead("image.jpg", ImreadModes.Grayscale); Mat laplacian = new Mat(); Cv2.Laplacian(src, laplacian, MatType.CV_64F); Scalar mean, stddev; Cv2.MeanStdDev(laplacian, out mean, out stddev); double focusMeasure = stddev.Val0 * stddev.Val0; ``` 2. 傅里叶变换评价图像清晰度 ```csharp using OpenCvSharp; Mat src = Cv2.ImRead("image.jpg", ImreadModes.Grayscale); Mat padded = new Mat(); Cv2.CopyMakeBorder(src, padded, 0, src.Rows, 0, src.Cols, BorderTypes.Constant, Scalar.All(0)); padded = padded.Norm(NormTypes.MinMax); Mat planes = new MatArray(padded, Mat.Zeros(padded.Size(), MatType.CV_32F)).GetMat(0); Cv2.Dft(planes, planes); Cv2.ShiftDft(planes); Mat magnitude = new Mat(); Cv2.Split(planes, out Mat[] complex); Cv2.Magnitude(complex[0], complex[1], magnitude); magnitude = magnitude.Norm(NormTypes.MinMax); double focusMeasure = Cv2.Sum(magnitude).Val0; ``` 3. SIFT特征点匹配评价图像对比度 ```csharp using OpenCvSharp; using OpenCvSharp.XFeatures2D; Mat src1 = Cv2.ImRead("image1.jpg", ImreadModes.Grayscale); Mat src2 = Cv2.ImRead("image2.jpg", ImreadModes.Grayscale); SIFT sift = SIFT.Create(); KeyPoint[] keypoints1, keypoints2; Mat descriptors1 = new Mat(), descriptors2 = new Mat(); sift.DetectAndCompute(src1, null, out keypoints1, descriptors1); sift.DetectAndCompute(src2, null, out keypoints2, descriptors2); BFMatcher matcher = new BFMatcher(NormTypes.L2, false); DMatch[] matches = matcher.Match(descriptors1, descriptors2); double maxDist = matches.Max(m => m.Distance); double minDist = matches.Min(m => m.Distance); double contrastMeasure = (maxDist - minDist) / maxDist; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值