资源简介
水平矫正图片角度,采用傅立叶变换,频谱图,相谱图,霍夫检测直线,计算角度,矫正角度
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using OpenCvSharp.Utilities;
namespace OpenCvRotate
{
class Program
{
static void Main(string[] args)
{
//以灰度方式读入原文件
string filename = “source.jpg“;
var src = IplImage.FromFile(filename LoadMode.GrayScale);
//转换到合适的大小,以适应快速变换
int width = Cv.GetOptimalDFTSize(src.Width);
int height = Cv.GetOptimalDFTSize(src.Height);
var padded = new IplImage(width height BitDepth.U8 1);
Cv.CopyMakeBorder(src padded new CvPoint(0 0) BorderType.Constant CvScalar.ScalarAll(0));
//实部、虚部(单通道)
var real = new IplImage(padded.Size BitDepth.F32 1);
var imaginary = new IplImage(padded.Size BitDepth.F32 1);
//合并(双通道)
var fourier = new IplImage(padded.Size BitDepth.F32 2);
//图像复制到实部,虚部清零
Cv.ConvertScale(padded real);
Cv.Zero(imaginary);
//合并、变换、再分解
Cv.Merge(real imaginary null null fourier);
Cv.DFT(fourier fourier DFTFlag.Forward);
Cv.Split(fourier real imaginary null null);
//计算sqrt(re^2+im^2),再存回re
Cv.Pow(real real 2.0);
Cv.Pow(imaginary imaginary 2.0);
Cv.Add(real imaginary real);
Cv.Pow(real real 0.5);
//计算log(1+re),存回re
Cv.AddS(real CvScalar.ScalarAll(1) real);
Cv.Log(real real);
//归一化,落入0-255范围
Cv.Normalize(real real 0 255 NormType.MinMax);
//把低频移动到中心
ShiftDFT(real);
//二值化,以150作为分界点,经验值,需要根据实际情况调整
Cv.Threshold(real real 150 255 ThresholdType.Binary);
//由于HoughLines2方法只接受8UC1格式的图片,因此进行转换
var gray = new IplImage(real.Size BitDepth.U8 1);
Cv.ConvertScale(real gray);
//找直线,threshold参数取100,经验值,需要根据实际情况调整
var storage = Cv.CreateMemStorage();
var lines = Cv.HoughLines2(gray storage HoughLinesMethod.Standard 1 Cv.PI / 180 100);
//找到符合条件的那条斜线
float angel = 0f;
float piThresh = (float)Cv.PI / 90;
float pi2 = (float)Cv.PI / 2;
for (int i = 0; i < lines.Total; ++i)
{
//极坐标下的点,X是极径,Y是夹角,我们只关心夹角
var p = lines.GetSeqElem(i);
float theta = p.Value.Y;
if (Math.Abs(theta) >= piThresh && Math.Abs(theta - pi2) >= piThresh)
{
angel = theta;
break;
}
}
angel = angel < pi2 ? angel : (angel - (float)Cv.PI);
Cv.ReleaseMemStorage(storage);
//转换角度
if (angel != pi2)
{
float angelT = (float)(src.Height * Math.Tan(angel) * 1.0 / src.Width);
angel = (float)Math.Atan(angelT);
}
float angelD = angel * 180 / (float)Cv.PI;
//旋转
var center = new CvPoint2D32f(src.Width / 2.0 src.Height / 2.0);
var rotMat = Cv.GetRotationMatrix2D(center angelD 1.0);
var dst = new IplImage(src.Size BitDepth.U8 1);
Cv.WarpAffine(src dst rotMat Interpolation.Cubic | Interpolation.FillOutliers CvScalar.ScalarAll(255));
Console.WriteLine(“如果旋转失败,说明HoughLines2方法未检测到直线,请适当调整源码66行的最后一个参
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
..A..H. 36864 2017-07-25 09:22 OpenCvRotate\.vs\OpenCvRotate\v14\.suo
文件 1013248 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\OpenCvSharpExtern.dll
文件 1200128 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_calib3d2410.dll
文件 1403904 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_contrib2410.dll
文件 2573824 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_core2410.dll
文件 868864 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_features2d2410.dll
文件 9720042 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_ffmpeg2410_64.dll
文件 656384 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_flann2410.dll
文件 482304 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_gpu2410.dll
文件 2410496 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_highgui2410.dll
文件 2236416 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_imgproc2410.dll
文件 1489408 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_legacy2410.dll
文件 597504 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_ml2410.dll
文件 657408 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_nonfree2410.dll
文件 805888 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_objdetect2410.dll
文件 2967040 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_ocl2410.dll
文件 235008 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_photo2410.dll
文件 1228800 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_stitching2410.dll
文件 716800 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_superres2410.dll
文件 443904 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_video2410.dll
文件 632320 2015-06-04 04:55 OpenCvRotate\bin\dll\x64\opencv_videostab2410.dll
文件 6656 2017-07-25 09:24 OpenCvRotate\bin\OpenCvRotate.exe
文件 13824 2017-07-25 09:24 OpenCvRotate\bin\OpenCvRotate.pdb
文件 22688 2017-07-25 09:24 OpenCvRotate\bin\OpenCvRotate.vshost.exe
文件 490 2016-07-16 19:44 OpenCvRotate\bin\OpenCvRotate.vshost.exe.manifest
文件 40960 2015-06-08 11:12 OpenCvRotate\bin\OpenCvSharp.Blob.dll
文件 496640 2015-06-08 11:12 OpenCvRotate\bin\OpenCvSharp.dll
文件 35840 2015-06-08 11:12 OpenCvRotate\bin\OpenCvSharp.Extensions.dll
文件 13312 2015-06-08 11:12 OpenCvRotate\bin\OpenCvSharp.UserInterface.dll
文件 319692 2013-11-02 00:11 OpenCvRotate\bin\source.jpg
............此处省略30个文件信息
- 上一篇:校园导游图 C# floyd算法
- 下一篇:C#程序设计及应用教程第三版-马俊主编
评论
共有 条评论