Hello Mat

 找回密码
 立即注册
查看: 827|回复: 1

凸包多边形

[复制链接]

1306

主题

1532

帖子

114

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22685
发表于 2023-12-7 19:30:58 来自手机 | 显示全部楼层 |阅读模式
凸包多边形https://blog.csdn.net/jimtien/article/details/118360090
  1.   public static void ShapeTrans0(Mat Region, out Mat RegionTrans, string Type)
  2.         {
  3.             // 此函数参考GetRegionPoint函数,Mat NonZeros = new Mat();   Cv2.FindNonZero(Region, NonZeros); // 非零点
  4.             // 需要改写
  5.             int ImageWidth = 0;
  6.             int ImageHeight = 0;
  7.             GetImageSize(Region, out ImageWidth, out ImageHeight);
  8.             Mat RectMask = new Mat();
  9.             GenImageConst(out RectMask, "byte", ImageWidth, ImageHeight);
  10.             RegionTrans = new Mat(Region.Height, Region.Width, MatType.CV_8UC1, Scalar.Black);
  11.             GenImageProto(RectMask, out RegionTrans, 0);
  12.             //寻找轮廓            
  13.             //OpenCvSharp.Point[][] contours;
  14.             FindImageContours(Region, out Common.ContoursCC);
  15.             // 将全部的二值化块连在一起
  16.             //Mat RectMask1 = new Mat();
  17.             //GenImageConst(out RectMask1, "byte", ImageWidth, ImageHeight);
  18.             Mat RegionTrans1 = new Mat();
  19.             //GenImageProto(RectMask1, out RegionTrans1, 0);
  20.             //Cv2.ImShow("sf", Region);
  21.             Mat NewRegion = new Mat();
  22.             NewRegion = Region.Clone();
  23.             for (int i = 0; i < Common.ContoursCC.Length - 1; i++)
  24.             {
  25.                 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);
  26.                 OverPaintGray(ref NewRegion, RegionTrans1, RegionTrans1);
  27.             }
  28.             //Cv2.ImShow("sf1", Region);
  29.             FindImageContours(NewRegion, out Common.ContoursCC);
  30.             if (Type.ToLower() == "rectangle2")
  31.             {
  32.                 RotatedRect rect0 = Cv2.MinAreaRect(Common.ContoursCC[0]);  // 最小外接矩形
  33.                 #region
  34.                 //float Rowy = rect0.Center.Y;
  35.                 //float Colx = rect0.Center.X;
  36.                 Rect box = rect0.BoundingRect();
  37.                 // 不带角度,所以错误
  38.                 //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);
  39.                 #endregion
  40.                 // 带角度
  41.                 OpenCvSharp.Point2f[] P = rect0.Points();
  42.                 for (int j = 0; j <= 3; j++)
  43.                 {
  44.                     Cv2.Line(RegionTrans, (OpenCvSharp.Point)P[j], (OpenCvSharp.Point)P[(j + 1) % 4], Scalar.White, 3, LineTypes.Link8, 0);
  45.                 }
  46.                 if (Common.MarginOrFill == 0)
  47.                 {
  48.                     //Cv2.Rectangle(RegionTrans, box, Scalar.White, 3, LineTypes.Link8);
  49.                     ;
  50.                 }
  51.                 else
  52.                 {
  53.                     FillUp(RegionTrans, out RegionTrans);
  54.                 }
  55.             }
  56.             else if (Type.ToLower() == "rectangle1")
  57.             {
  58.                 RotatedRect rect0 = Cv2.MinAreaRect(Common.ContoursCC[0]);  // 最小外接矩形
  59.                 //float Rowy = rect0.Center.Y;
  60.                 //float Colx = rect0.Center.X;
  61.                 Rect box = rect0.BoundingRect();
  62.                 // 不带角度,所以错误
  63.                 //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);
  64.                 if (Common.MarginOrFill == 0)
  65.                 {
  66.                     Cv2.Rectangle(RegionTrans, box, Scalar.White, 3, LineTypes.Link8);
  67.                 }
  68.                 else
  69.                 {
  70.                     Cv2.Rectangle(RegionTrans, box, Scalar.White, -1, LineTypes.Link8);
  71.                 }
  72.             }
  73.             else if (Type.ToLower() == "circle")
  74.             {
  75.                 OpenCvSharp.Point2f center;  //定义圆中心坐标
  76.                 float radius = 0.1f;         //定义圆半径
  77.                 Cv2.MinEnclosingCircle(Common.ContoursCC[0], out center, out radius);
  78.                 if (Common.MarginOrFill == 0)
  79.                 {
  80.                     //Cv2.DrawContours(RegionTrans, contours, 0, Scalar.White, 3, LineTypes.Link8);
  81.                     Cv2.Circle(RegionTrans, (int)center.X, (int)center.Y, (int)radius, Scalar.White, 3, LineTypes.Link8);
  82.                 }
  83.                 else
  84.                 {
  85.                     //Cv2.DrawContours(RegionTrans, contours, 0, Scalar.White, -1, LineTypes.Link8);
  86.                     Cv2.Circle(RegionTrans, (int)center.X, (int)center.Y, (int)radius, Scalar.White, -1, LineTypes.Link8);
  87.                 }
  88.             }
  89.             else if (Type.ToLower() == "convex")
  90.             {
  91.                 OpenCvSharp.Point[] ContPoint = Cv2.ConvexHull(Common.ContoursCC[0]);
  92.                 if (Common.MarginOrFill == 0)
  93.                 {
  94.                     for (int j = 0; j < ContPoint.Length; j++)
  95.                     {
  96.                         Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, 3, LineTypes.Link8, 0);
  97.                     }
  98.                 }
  99.                 else
  100.                 {
  101.                     for (int j = 0; j < ContPoint.Length; j++)
  102.                     {
  103.                         Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, 1, LineTypes.Link8, 0);
  104.                     }
  105.                     FillUp(RegionTrans, out RegionTrans);
  106.                 }
  107.             }
  108.             else
  109.             {
  110.                 OpenCvSharp.Point[] ContPoint = Cv2.ConvexHull(Common.ContoursCC[0]);
  111.                 if (Common.MarginOrFill == 0)
  112.                 {
  113.                     for (int j = 0; j < ContPoint.Length; j++)
  114.                     {
  115.                         Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, 3, LineTypes.Link8, 0);
  116.                     }
  117.                 }
  118.                 else
  119.                 {
  120.                     for (int j = 0; j < ContPoint.Length; j++)
  121.                     {
  122.                         Cv2.Line(RegionTrans, (OpenCvSharp.Point)ContPoint[j], (OpenCvSharp.Point)ContPoint[(j + 1) % ContPoint.Length], Scalar.White, -1, LineTypes.Link8, 0);
  123.                     }
  124.                 }
  125.             }
  126.             RectMask.Dispose();
  127.             RegionTrans1.Dispose();
  128.             NewRegion.Dispose();
  129.         }
复制代码



回复

使用道具 举报

1306

主题

1532

帖子

114

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22685
 楼主| 发表于 2023-12-8 14:25:24 来自手机 | 显示全部楼层
boundingrect(points)外接正矩形边界
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-7-27 10:33 , Processed in 0.199354 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表