如何在OpenCV中绘制旋转的空格?

时间:2015-05-13 08:52:53

标签: c++ opencv

我知道如何使用RotatedRect绘制旋转的矩形。但是我想使用opencv绘制一个旋转的Trapeze。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

A)将梯形的四个点定义为轮廓:

std::vector<std::vector<cv::Point> > MakeRotatedTrapeze(
    const cv::Size& BBSize,
    const float& angleRad,
    const cv::Point2f& trapezeCenter,
    const float& topToBottomRatio)
{
    //Make trapeze
    cv::Point2f deltaDOWN( BBSize.height/2*sin(angleRad), BBSize.height/2*cos(angleRad));//bottom
    cv::Point2f deltaUP = -deltaDOWN;//top
    cv::Point2f deltaLEFT(-BBSize.width/2*cos(angleRad), BBSize.width/2*sin(angleRad));//left
    cv::Point2f deltaRIGHT = -deltaLEFT;//right

    cv::Point2f bl = trapezeCenter + deltaDOWN + deltaLEFT;
    cv::Point2f br = trapezeCenter + deltaDOWN + deltaRIGHT;
    cv::Point2f tl = trapezeCenter + deltaUP + topToBottomRatio*deltaLEFT;
    cv::Point2f tr = trapezeCenter + deltaUP + topToBottomRatio*deltaRIGHT;

    cv::Point trapez_pts[4] = {tl, tr, br, bl};
    vector<vector<Point> > trapeze(1, vector<Point>(trapez_pts, trapez_pts + 4));

    return trapeze;
}

B)使用drawContours

画出空中飞人
float trapezTopScale = 0.5;
std::vector<std::vector<cv::Point> > trapeze = MakeRotatedTrapeze( cv::Size(100, 50), 45.0, cv::Point2f(320, 240), trapezTopScale);
cv::Mat image(480, 640, CV_8UC1);
image.setTo(0);
drawContours( image, trapeze, 0, cv::Scalar(255), CV_FILLED, 8, vector<Vec4i>() );