Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

c# - Finding Minimum Distance between Contours

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

enter image description here

after finding contours :

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. Find the two Contours for which you want to find the min distance between them.
  2. Cycle through each point in the Two contours & find the distance between them.
  3. Take the minimum Distance by comparing all other distances & Mark that Points.

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...