In the previous post Advanced Object Marker ::: [Tracking defined ROI (Region Of Interest)] the second video shows the new region of interest (ROI) as the YELLOW rotated rectangle. For our training purposes, a rotated rectangle can not be input to the training application as it expects the defining members of a rectangle as x, y, width and height. With x and y repseenting the upper left most corner of the box. To get around this we will simply create a bounding box for our rotated rectangle.

OpenCV has a built in class called RotatedRect for handling rotated rectangles as well as a helper function that will return the bounding rectangle (box) of the rotated rectangle. But for our purposes we do not want to rotate a rectangle by its center point but along the top left anchor point.
 
 
 
 
 
 
 

class RotatedRect
{
public:
    // constructors
    RotatedRect();
    RotatedRect(const Point2f& _center, const Size2f& _size, float _angle);
    RotatedRect(const CvBox2D& box);

    // returns minimal up-right rectangle that contains the rotated rectangle
    Rect boundingRect() const;
    // backward conversion to CvBox2D
    operator CvBox2D() const;

    // mass center of the rectangle
    Point2f center;
    // size
    Size2f size;
    // rotation angle in degrees
    float angle;
};

So because we know the endpoints that make up our rotated rectangle we can then form the bounding box for the region of interest (ROI). Below is a little code excerpt that uses the rotated rectangle positions to find the upper most x and y values as well as the lowest most x and y values of that rectangle. Once the loop is completed we know what our top left most point will be as well as our bottom right most point which we can then use to get our width values and height values to form the rectangle.

for (int i = 0; i < NUMBER_OF_PTS_ON_RECT; i++)
{
	//Find the lowest values
	if(lowestX > box_vtx[i].x)
	{
		lowestX = cvRound(box_vtx[i].x); 
	}
	if(lowestY > box_vtx[i].y)
	{
		lowestY = cvRound(box_vtx[i].y); 
	}

	//Find the greatest values
	if(greatestX < box_vtx[i].x)
	{
		greatestX = cvRound(box_vtx[i].x); 
	}
	if(greatestY < box_vtx[i].y)
	{
		greatestY = cvRound(box_vtx[i].y); 
	}
				
}

Tags: ,

Leave a Reply