opencvsharp的指定连通区域最小外接矩形获取

using OpenCvSharp;

Mat source = new Mat(@"D:code-csFormsApplication5aaaasnap7.png", ImreadModes.Grayscale); Cv2.ImShow("Demo", source); Cv2.WaitKey(0); Mat labels = new Mat(); int num_labels = Cv2.ConnectedComponents(source, labels, PixelConnectivity.Connectivity4); Point[][] contours; HierarchyIndex[] hierarchly; Cv2.FindContours(source, out contours, out hierarchly, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple, new Point(0, 0)); //将结果画出并返回结果 Mat dst_Image = Mat.Zeros(source.Size(), source.Type()); Random rnd = new Random(); for (int i = 0; i < contours.Length; i++) { Scalar color = new Scalar(255, 255, 255);//白色 //Cv2.DrawContours(dst_Image, contours, i, color, 1, LineTypes.Link8, hierarchly); } //查找指定 Point2f pt = new Point2f(1, 40); var ret = Cv2.PointPolygonTest(contours[0], pt, false); RotatedRect rect = Cv2.MinAreaRect(contours[0]); Point2f[] P = rect.Points(); for (int j = 0; j <= 3; j++) { Cv2.Line(dst_Image, (Point)P[j], (Point)P[(j + 1) % 4], new Scalar(255, 0, 0), 1); } Cv2.ImShow("dst_Image:", dst_Image); Cv2.WaitKey(0);
原文地址:https://www.cnblogs.com/yansc/p/14005202.html