本文整理汇总了C#中OpenCvSharp.Mat.ConvertTo方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.ConvertTo方法的具体用法?C# Mat.ConvertTo怎么用?C# Mat.ConvertTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenCvSharp.Mat
的用法示例。
在下文中一共展示了Mat.ConvertTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
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();
}