c# - 寻找等高线之间的最小距离

标签 c# opencv

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

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

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

enter image description here

找到轮廓后:

enter image description here

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

最佳答案

可以引用this link & this wiki用于检测图像的轮廓。

要找到两个形状的最小距离,请按照以下步骤操作:

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

这是该算法的 EMGUCV 实现。

private void button2_Click(object sender, EventArgs e)
{
    Image<Gray, byte> Img_Scene_Gray = Img_Source_Bgr.Convert<Gray, byte>();
    Image<Bgr, byte> 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<Point> 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<Point> a, Contour<Point> 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 = p.X - q.X;
    int Y_Diff = p.Y - q.Y;
    return Math.Sqrt((X_Diff * X_Diff) + (Y_Diff * Y_Diff));
}

enter image description here

关于c# - 寻找等高线之间的最小距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24619645/

相关文章:

c# - WatiN:尝试将文本 ("WatiN") 键入 Google 的搜索文本框时出错

c++ - OpenCV 中的直方图函数

python - 对视频文件中每个蒙版帧进行 'White'像素计数

c++ - 在 Eclipse 中使用 OpenCV

Opencv 'undefined reference to ` cv::namedWindow....'(链接错误)

c# - 启动没有表单但包含表单的 C# 应用程序

c# - 监控和计算 asp.net 网站性能

python - MSER 文本检测问题

c# - 当使用MVVM + WP7 + LongListSelector选中对号时,如何调用服务器

c# - 当自定义凭据身份验证提供程序的身份验证失败时返回自定义 HTTP 响应代码