4

We are doing a project in vehicle counting (using OpenCV). Now we have to find the euclidian distance from the centroid of the object in one frame to the adjacent frame? In our project we have done up to finding the centroid.

Bart
  • 19,692
  • 7
  • 68
  • 77
user1244643
  • 61
  • 2
  • 5
  • 1
    I think you have specified too little information. Kindly add more info – Rohit Vipin Mathews Mar 07 '12 at 06:42
  • If you have stored the centroid values for the first frame and then find the centroid for the second frame, you can calculate the euclidean distance. But you have posted limited information so cant more. –  Mar 07 '12 at 07:12

2 Answers2

5

I am going to assume the camera has not moved between captures, so that you do not have to worry about registration.

You should have two cv::Point objects representing the two acquired centroids. Euclidean distance can be calculated as follows:

double euclideanDist(Point p, Point q)
{
    Point diff = p - q;
    return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
}

int main(int /*argc*/, char** /*argv*/)
{
    Point centroid1(0.0, 0.0);
    Point centroid2(3.0, 4.0);

    cout << euclideanDist(centroid1, centroid2) << endl;

    return 0;
}

This outputs 5 (i.e., 3-4-5 triangle)...

Hope that helps!

mevatron
  • 13,911
  • 4
  • 55
  • 72
0

If p and q are of type int, make sure to typecast the (diff.x*diff.x + diff.y*diff.y) term to either double or float. That way you can get a more accurate euclidean distance.

krsteeve
  • 1,794
  • 4
  • 19
  • 29
jjf
  • 115
  • 3
  • 11