裁剪图像
OpenCvSharp.Rect rect = new OpenCvSharp.Rect(4800, 2100, 400, 900);//设置范围 OpenCvSharp.Mat cropped_image = new OpenCvSharp.Mat(Img, rect);//裁剪图像裁剪图像
static private void FindCenterOfCircle(Mat mat) { mat.ConvertTo(mat, MatType.CV_8UC1,1.0/255,0);//转换成CV_8UC1 Cv2.ImWrite("显示原图.tiff", mat); var mean = Cv2.Mean(mat); //阈值处理 double thresh = 40, maxval = 255; threImg = mat.Threshold(thresh, maxval, ThresholdTypes.Binary); //Mat threImg = new Mat(); //Cv2.AdaptiveThreshold(mat, threImg, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 33, 20); //Cv2.ImShow("自动阈值", threImg); mat.Release(); //膨胀处理 element = Cv2.GetStructuringElement(MorphShapes.Ellipse,new Size(15,15)); DilateImg = threImg.Dilate(element); Cv2.ImShow("显示膨胀", DilateImg); Cv2.ImWrite("保存膨胀.tiff", DilateImg); //threImg.Release(); //腐蚀处理 ErodeImg = DilateImg.Erode(element); Cv2.ImShow("显示腐蚀", ErodeImg); Cv2.ImWrite("保存腐蚀.tiff", ErodeImg); //寻找图像轮廓 OpenCvSharp.Point[][] contours; HierarchyIndex[] hierachy; Cv2.FindContours(DilateImg, out contours, out hierachy, RetrievalModes.List, ContourApproximationModes.ApproxTC89KCOS); threImg.Release(); DilateImg.Release(); //根据找到的轮廓点,拟合椭圆 for (int j = 0; j < contours.Length; j++) { //拟合函数必须至少5个点,少于则不拟合 if (contours[j].Length < 5) continue; //椭圆拟合 var rrt = Cv2.FitEllipse(contours[j]); if (rrt.Size.Width > 20 && rrt.Size.Height > 20) { //imageShows[imageShows.Count - 1].AddCrossLine(rrt); strLog += rrt.Center.X.ToString("0.00") + "," + rrt.Center.Y.ToString("0.00") + "\r\n"; } } contours = null; hierachy = null; System.GC.Collect(); System.GC.WaitForPendingFinalizers(); }寻找圆心
标签:threImg,mat,裁剪,Cv2,圆心,contours,OpenCvSharp,rrt From: https://www.cnblogs.com/lizhiqiang0204/p/17160129.html