using OpenCvSharp; using OpenCvSharp.Extensions; using DlibDotNet; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; namespace app { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } /// <summary> /// 透视变换 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnWarpPerspectiveEasy_Click(object sender, EventArgs e) { // 加载图像 Mat src = Cv2.ImRead(@"img\bianxing1.png"); Cv2.ImShow("原图", src); // 透视变换 List<Point2f> srcContoursPoints = new List<Point2f>(); srcContoursPoints.Add(new Point2f(138, 60)); srcContoursPoints.Add(new Point2f(426, 60)); srcContoursPoints.Add(new Point2f(56, 316)); srcContoursPoints.Add(new Point2f(521, 316)); List<Point2f> dstContoursPoints = new List<Point2f>(); dstContoursPoints.Add(new Point2f(0, 0)); dstContoursPoints.Add(new Point2f(580, 0)); dstContoursPoints.Add(new Point2f(0, 380)); dstContoursPoints.Add(new Point2f(580, 380)); // 透视变换矩阵 Mat retImg = new Mat(); Mat warpMat = Cv2.GetPerspectiveTransform(srcContoursPoints, dstContoursPoints); Cv2.WarpPerspective(src, retImg, warpMat, retImg.Size(), InterpolationFlags.Linear); Cv2.ImShow("透视变换", retImg); } } }
以上仅仅为了便于演示透视变换的效果。实际需要灰度处理、模糊处理、二值处理、形态学操作、按位运算、轮廓发现、轮廓绘制、灰度转换、霍夫变换直线检测等步骤,再计算上下左右4条直线的交点,再进行透视变化矩阵的调整。
其他说明:
两条直线的交点坐标