简体   繁体   中英

Drawing Bounding Box for a rotated object

I have a video stream where the objects( boxes) move in any direction and in any pose. I want to track each and every object. So this is what i did:

1) I found the contours of the object

2) drew bounded boxes around the objects.

3) Calculated the centroid of the bounded box and tracked the objects.

All these works fine. But the centroid of the bounded box is not exactly equal to the center of the object when the object rotates or is in some other pose.

I want the bounded box also to rotate and fit to the shape of the box. When this happens the center of the bounded box is equal to the center of the object. This will also improve my tracking accuracy.

See the picture attached:

If Bounded box is not a solution, is there any there method to find the center of the object

My object is always box shaped and also only the top surface of the object is seen on the video stream.

在此处输入图片说明

From the OpenCV documentation here :

// contours : is your blobs found before
// Create a vector to store your rotated rectangles
vector<RotatedRect> minRect( contours.size() );

// in a for loop find rotated rectangles for each blob 
for( int i = 0; i < contours.size(); i++ )
{ 
    // minAreaRect() function is for finding Rotated Rectangles
    minRect[i] = minAreaRect( Mat(contours[i]) );
}

You can use the moments function once you have your contour. This will give you the centroid directly rather than having to use bounding box.

 cv::Moments mu = moments(box_contour, false);
 cv::Point2f centroid = Point2f( mu.m10/mu.m00 , mu.m01/mu.m00 ); 

More detailed example here

Not super experienced with C++ (usually use openCV in Python) but should look something like that :)

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