public static string Pic_Rectity(string FileName, string point_json) { string savepath = @"D:\OpenCV\Output\" + DateTime.Now.ToString("yyyyMMddhhmmssff") + ".jpg"; Base64Convert.Base64ToFileAndSave(FileName, savepath);//文件流保存到文件 Positions positions = new Positions(); string filepath = @"D:\OpenCV\Output\"; Mat src = Cv2.ImRead(savepath); //原始图像 List <Point> getpt = JsonConvert.DeserializeObject <List <Point> >(point_json); Mat dst = new Mat(); //new Window("1", WindowMode.FreeRatio, src); Mat roi = Mat.Zeros(src.Size(), MatType.CV_8U); Point[] points = getpt.ToArray(); int maxX = points[0].X, maxY = points[0].Y, minX = points[0].X, minY = points[0].Y; Point[][] contours = { points }; for (int i = 0; i < points.Length; i++)//求出最小坐标 { if (points[i].X > maxX) { maxX = points[i].X; } if (points[i].Y > maxY) { maxY = points[i].Y; } if (points[i].X < minX) { minX = points[i].X; } if (points[i].Y < minY) { minY = points[i].Y; } } int width = maxX - minX; int height = maxY - minY; Cv2.DrawContours(roi, contours, 0, Scalar.All(255), -1); src.CopyTo(dst, roi);//保存到新画布 //new Window("转化前", WindowMode.FreeRatio, dst); dst = dst.SubMat(new Rect(minX, minY, width, height)); Point2f[] pt = new Point2f[4]; for (int i = 0; i < points.Length; i++)//图像坐标 { pt[i].X = points[i].X - minX; pt[i].Y = points[i].Y - minY; } Point2f[] box = new Point2f[4];//画布角点 box[0] = new Point(0, 0); box[1] = new Point(width, 0); box[2] = new Point(width, height); box[3] = new Point(0, height); //图像的拉伸处理 var h = Cv2.GetPerspectiveTransform(pt, box); Cv2.WarpPerspective(dst, dst, h, new Size(width, height)); filepath += DateTime.Now.ToString("yyyy-MM-dd hh_mm_ss_ff") + ".jpg"; Cv2.ImWrite(filepath, dst); positions.FileName = Base64Convert.FileToBase64(filepath); //positions.point2 = Findarea(dst); string js = JsonConvert.SerializeObject(positions); Console.WriteLine(js); return(js); //new Window("效果", WindowMode.FreeRatio, dst); //Window.WaitKey(); }