Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw rotated rectangle in opencv c++

Tags:

c++

opencv

I want to draw a rotated rectangle in opencv with c++. I use "rectangle" function like bellow:

rectangle(RGBsrc, vertices[0], vertices[2], Scalar(0, 0, 0),  CV_FILLED, 8, 0);

but this function draw an rectangle with 0 angle. How can i draw rotated rectangle with special angle in opencv with c++?

like image 206
narges Avatar asked Apr 11 '17 09:04

narges


People also ask

How to draw a rectangle in OpenCV using C++?

The rectangle () function from OpenCV C++ library will be used. image: It is the image on which the rectangle is to be drawn. start (pt1): It is the top left corner of the rectangle represented as the tuple of two coordinates i.e., (x-coordinate, y-coordinate).

How to draw a rectangle in C graphics?

Draw Rectangle in C graphics. rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner,...

Which function is used to draw a rectangle?

rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle.

Which two co-ordinates are required to draw the rectangle?

Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.


1 Answers

Since you want a filled rectangle, you should use fillConvexPoly:

// Include center point of your rectangle, size of your rectangle and the degrees of rotation  
void DrawRotatedRectangle(cv::Mat& image, cv::Point centerPoint, cv::Size rectangleSize, double rotationDegrees)
{
    cv::Scalar color = cv::Scalar(255.0, 255.0, 255.0); // white

    // Create the rotated rectangle
    cv::RotatedRect rotatedRectangle(centerPoint, rectangleSize, rotationDegrees);

    // We take the edges that OpenCV calculated for us
    cv::Point2f vertices2f[4];
    rotatedRectangle.points(vertices2f);

    // Convert them so we can use them in a fillConvexPoly
    cv::Point vertices[4];    
    for(int i = 0; i < 4; ++i){
        vertices[i] = vertices2f[i];
    }

    // Now we can fill the rotated rectangle with our specified color
    cv::fillConvexPoly(image,
                       vertices,
                       4,
                       color);
}
like image 126
Jurjen Avatar answered Sep 17 '22 14:09

Jurjen