查找轮廓之间的最小距离

我在图像中有很多形状,我想在arrays中保存它们的轮廓。 我的意思是我想要数组1中形状1的轮廓坐标,数组2中的形状2的坐标…

如果有两个形状,我如何使用它们的坐标绘制它们之间的最短线?

例如,在对图像进行多次操作后,我得到了这个结果

在此处输入图像描述

找到轮廓后:

在此处输入图像描述

所以我需要每个形状轮廓的坐标来计算它们之间的最短距离

您可以参考此链接和此Wiki来检测图像中的轮廓。

要从两个形状中查找最小距离,请执行以下步骤:

  1. 找到要查找它们之间最小距离的两个等高线。
  2. 循环通过两个轮廓中的每个点并找到它们之间的距离。
  3. 通过比较所有其他距离并标记该点来获取最小距离。

以下是此算法的EMGUCV实现。

private void button2_Click(object sender, EventArgs e) { Image Img_Scene_Gray = Img_Source_Bgr.Convert(); Image Img_Result_Bgr = Img_Source_Bgr.Copy(); LineSegment2D MinIntersectionLineSegment = new LineSegment2D(); Img_Scene_Gray = Img_Scene_Gray.ThresholdBinary(new Gray(10), new Gray(255)); #region Finding Contours using (MemStorage Scene_ContourStorage = new MemStorage()) { for (Contour Contours_Scene = Img_Scene_Gray.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL, Scene_ContourStorage); Contours_Scene != null; Contours_Scene = Contours_Scene.HNext) { if (Contours_Scene.Area > 25) { if (Contours_Scene.HNext != null) { MinIntersectionLine(Contours_Scene, Contours_Scene.HNext, ref MinIntersectionLineSegment); Img_Result_Bgr.Draw(MinIntersectionLineSegment, new Bgr(Color.Green), 2); } Img_Result_Bgr.Draw(Contours_Scene, new Bgr(Color.Red), 1); } } } #endregion imageBox1.Image = Img_Result_Bgr; } void MinIntersectionLine(Contour a, Contour b,ref LineSegment2D Line) { double MinDist = 10000000; for (int i = 0; i < a.Total; i++) { for (int j = 0; j < b.Total; j++) { double Dist = Distance_BtwnPoints(a[i], b[j]); if (Dist < MinDist) { Line.P1 = a[i]; Line.P2 = b[j]; MinDist = Dist; } } } } double Distance_BtwnPoints(Point p, Point q) { int X_Diff = pX - qX; int Y_Diff = pY - qY; return Math.Sqrt((X_Diff * X_Diff) + (Y_Diff * Y_Diff)); } 

在此处输入图像描述