我想用c ++在opencv中绘制一个旋转的矩形。我使用"rectangle"像波纹管这样的功能:
rectangle(RGBsrc, vertices[0], vertices[2], Scalar(0, 0, 0),  CV_FILLED, 8, 0);
Run Code Online (Sandbox Code Playgroud)
但是此函数绘制一个角度为0的矩形。如何使用C ++在opencv中绘制具有特殊角度的旋转矩形?
小智 11
下面的示例演示了如何在 opencv c++ 中绘制旋转矩形。
Mat test_image(200, 200, CV_8UC3, Scalar(0));
RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
Point2f vertices[4];
rRect.points(vertices);
for (int i = 0; i < 4; i++)
    line(test_image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0), 2);
Rect brect = rRect.boundingRect();
rectangle(test_image, brect, Scalar(255,0,0), 2);
imshow("rectangles", test_image);
waitKey(0);
Run Code Online (Sandbox Code Playgroud)
参考:
OpenCV 文档
由于要填充矩形,因此应使用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);
}
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           11500 次  |  
        
|   最近记录:  |