找到等高线之间的最短距离 [英] Finding Minimum Distance between Contours

查看:493
本文介绍了找到等高线之间的最短距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在图像许多形状,我想保存自己的轮廓在数组中。
我的意思是,我想在阵列2 EXT在阵列1轮廓线形态1的坐标,对于形状2 ...

I have many shapes in image which I want to save their contours in arrays . I mean that I want the coordinates for contours for shape 1 in array 1 , for shape 2 in array 2 ext...

如果有两个形状如何使用他们的坐标,我得出他们之间的最短路线?

And if there are two shapes how can I draw the shortest line between them using their coordinates?

例如我有很多操作后,这个结果的图像

for example I had this result after many operations on an image

寻找轮廓后:

所以,我需要为每个形状轮廓的坐标来计算

So I need the coordinates for each shape contour to calculate the shortest distance between them

推荐答案

您可以参考这链接&安培; 这个wiki 用于从检测轮廓。图片

You can refer this link & this wiki for detecting Contours from an Image.

要找到最小离两个形状遵循以下步骤:

To find the min Distance from two Shapes follow the following steps:


  1. 找到您要找到它们之间的距离最小的两个轮廓

  2. 通过在两个轮廓和放每个点循环。发现它们之间的距离

  3. 通过比较所有其它的距离和放取的最小距离。马克的点。

下面是该算法的EMGUCV实现。


Here is the EMGUCV Implementation for this algorithm.

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));
}



这篇关于找到等高线之间的最短距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆