简体   繁体   中英

find distance between contours/objects in opencv

I have found the contours using cvfindcontour, and, now I want to access first and second contour and find the euclidean distance between them. Could anybody help me with its 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 the correct way?

I would use cvMoments to compute the centroid of each contour, and then compute the Euclidean distance between the two centroids. Here is a post on openframeworks about it. Here is a post from the opencv mailing-list.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM