大赵传奇

援引事类,扬搉古今,举要删芜,言辩而理切--QQ276605216

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
using OpenCvSharp;

Mat source = new Mat(@"D:\code-cs\FormsApplication5\aaaa\snap7.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);

 

posted on 2020-11-19 13:58  赵长青  阅读(2036)  评论(0编辑  收藏  举报