# How to find the Euclidean distance between the center of gravity of an object in one frame and an adjacent frame

We are doing a project in vehicle counting (using OpenCV). Now we need to find the Euclidean distance from the center of gravity of an object in one frame to an adjacent frame ? In our project, we searched for the center of gravity.

+3

source to share

I'm going to assume that the camera hasn't moved between captures, so you don't have to worry about registration.

You should have two objects `cv::Point`

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 it helps!

+5

source

If `p`

and `q`

have a type `int`

, make sure the type `(diff.x*diff.x + diff.y*diff.y)`

is `double`

or `float`

. This way you can get a more accurate Euclidean distance.

0

source

All Articles