|
凸包多边形https://blog.csdn.net/jimtien/article/details/118360090
- public static void ShapeTrans0(Mat Region, out Mat RegionTrans, string Type)
- {
- // 此函数参考GetRegionPoint函数,Mat NonZeros = new Mat(); Cv2.FindNonZero(Region, NonZeros); // 非零点
- // 需要改写
- int ImageWidth = 0;
- int ImageHeight = 0;
- GetImageSize(Region, out ImageWidth, out ImageHeight);
- Mat RectMask = new Mat();
- GenImageConst(out RectMask, "byte", ImageWidth, ImageHeight);
- RegionTrans = new Mat(Region.Height, Region.Width, MatType.CV_8UC1, Scalar.Black);
- GenImageProto(RectMask, out RegionTrans, 0);
- //寻找轮廓
- //OpenCvSharp.Point[][] contours;
- FindImageContours(Region, out Common.ContoursCC);
- // 将全部的二值化块连在一起
- //Mat RectMask1 = new Mat();
- //GenImageConst(out RectMask1, "byte", ImageWidth, ImageHeight);
- Mat RegionTrans1 = new Mat();
- //GenImageProto(RectMask1, out RegionTrans1, 0);
- //Cv2.ImShow("sf", Region);
- Mat NewRegion = new Mat();
- NewRegion = Region.Clone();
- for (int i = 0; i < Common.ContoursCC.Length - 1; i++)
- {
- GenRegionLine(out RegionTrans1, (int)(Common.ContoursCC[i][0].Y), (int)(Common.ContoursCC[i][0].X), (int)(Common.ContoursCC[i + 1][0].Y), (int)(Common.ContoursCC[i + 1][0].X), ImageWidth, ImageHeight);
- OverPaintGray(ref NewRegion, RegionTrans1, RegionTrans1);
- }
- //Cv2.ImShow("sf1", Region);
- FindImageContours(NewRegion, out Common.ContoursCC);
- if (Type.ToLower() == "rectangle2")
- {
- RotatedRect rect0 = Cv2.MinAreaRect(Common.ContoursCC[0]); // 最小外接矩形
- #region
- //float Rowy = rect0.Center.Y;
- //float Colx = rect0.Center.X;
- Rect box = rect0.BoundingRect();
- // 不带角度,所以错误
- //GenRectangle1(out RegionTrans, Double2Int(Rowy - rect0.Size.Width / 2), Double2Int(Colx - rect0.Size.Height / 2), Double2Int(Rowy + rect0.Size.Width / 2), Double2Int(Colx + rect0.Size.Height / 2), ImageWidth, ImageHeight);
- #endregion
- // 带角度
- OpenCvSharp.Point2f[] P = rect0.Points();
- for (int j = 0; j <= 3; j++)
- {
- Cv2.Line(RegionTrans, (OpenCvSharp.Point)P[j], (OpenCvSharp.Point)P[(j + 1) % 4], Scalar.White, 3, LineTypes.Link8, 0);
- }
- if (Common.MarginOrFill == 0)
- {
- //Cv2.Rectangle(RegionTrans, box, Scalar.White, 3, LineTypes.Link8);
- ;
- }
- else
- {
- FillUp(RegionTrans, out RegionTrans);
- }
- }
- else if (Type.ToLower() == "rectangle1")
- {
- RotatedRect rect0 = Cv2.MinAreaRect(Common.ContoursCC[0]); // 最小外接矩形
- //float Rowy = rect0.Center.Y;
- //float Colx = rect0.Center.X;
- Rect box = rect0.BoundingRect();
- // 不带角度,所以错误
- //GenRectangle1(out RegionTrans, Double2Int(Rowy - rect0.Size.Width / 2), Double2Int(Colx - rect0.Size.Height / 2), Double2Int(Rowy + rect0.Size.Width / 2), Double2Int(Colx + rect0.Size.Height / 2), ImageWidth, ImageHeight);
- if (Common.MarginOrFill == 0)
- {
- Cv2.Rectangle(RegionTrans, box, Scalar.White, 3, LineTypes.Link8);
- }
- else
- {
- Cv2.Rectangle(RegionTrans, box, Scalar.White, -1, LineTypes.Link8);
- }
- }
- else if (Type.ToLower() == "circle")
- {
- OpenCvSharp.Point2f center; //定义圆中心坐标
- float radius = 0.1f; //定义圆半径
- Cv2.MinEnclosingCircle(Common.ContoursCC[0], out center, out radius);
- if (Common.MarginOrFill == 0)
- {
- //Cv2.DrawContours(RegionTrans, contours, 0, Scalar.White, 3, LineTypes.Link8);
- Cv2.Circle(RegionTrans, (int)center.X, (int)center.Y, (int)radius, Scalar.White, 3, LineTypes.Link8);
- }
- else
- {
- //Cv2.DrawContours(RegionTrans, contours, 0, Scalar.White, -1, LineTypes.Link8);
- Cv2.Circle(RegionTrans, (int)center.X, (int)center.Y, (int)radius, Scalar.White, -1, LineTypes.Link8);
- }
- }
- else if (Type.ToLower() == "convex")
- {
- OpenCvSharp.Point[] ContPoint = Cv2.ConvexHull(Common.ContoursCC[0]);
- if (Common.MarginOrFill == 0)
- {
- for (int j = 0; j < ContPoint.Length; j++)
- {
- Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, 3, LineTypes.Link8, 0);
- }
- }
- else
- {
- for (int j = 0; j < ContPoint.Length; j++)
- {
- Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, 1, LineTypes.Link8, 0);
- }
- FillUp(RegionTrans, out RegionTrans);
- }
- }
- else
- {
- OpenCvSharp.Point[] ContPoint = Cv2.ConvexHull(Common.ContoursCC[0]);
- if (Common.MarginOrFill == 0)
- {
- for (int j = 0; j < ContPoint.Length; j++)
- {
- Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, 3, LineTypes.Link8, 0);
- }
- }
- else
- {
- for (int j = 0; j < ContPoint.Length; j++)
- {
- Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, -1, LineTypes.Link8, 0);
- }
- }
- }
- RectMask.Dispose();
- RegionTrans1.Dispose();
- NewRegion.Dispose();
- }
复制代码
|
|