Find the distance between contours / objects in opencv

I found the contours using cvfindcontour, and now I want to get the first and second contour and find the Euclidean distance between them. Can someone help me with his code?

CvPoint *contourPoint, *contourPoint2; contourPoint = (CvPoint *)CV_GET_SEQ_ELEM(CvPoint,contours,1); contourPoint2 = (CvPoint *)CV_GET_SEQ_ELEM(CvPoint,contours,2); double dis = sqrt(double((contourPoint->x - contourPoint2->x) * (contourPoint->x - contourPoint2->x) + (contourPoint->y - contourPoint2->y) * (contourPoint->y - contourPoint2->y)) ); 

Is it correct?

+4
source share
1 answer

I would use cvMoments to calculate the centroid of each contour, and then calculate the Euclidean distance between the two centroids. Here is a message about open angles. Here is a message from the opencv mailing list.

Hope this helps!

+3
source

Source: https://habr.com/ru/post/1381642/


All Articles