获得旋转角度的当前方式。
cv::Rect rRect(pTopLeft.x,pTopLeft.y,pBottomRight.x-pTopLeft.x,pBottomRight.y-pTopLeft.y);
img(rRect);
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = img.begin<uchar>();
cv::Mat_<uchar>::iterator end = img.end<uchar>();
for (; it != end; ++it)
if (*it)
points.push_back(it.pos());
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
double angle = box.angle;
if (angle < -45.)
angle += 90.;
有没有办法避免for循环并直接使用rRect获取Rect的角度,或者openCV中是否有任何内置函数使用Rect获取角度?
答案 0 :(得分:0)
如果我们有方格点,我们可以执行以下操作。
cv::Mat debugSquares( std::vector<std::vector<cv::Point> > squares, cv::Mat image )
{
for ( int i = 0; i< squares.size(); i++ ) {
// draw contour
cv::drawContours(image, squares, i, cv::Scalar(255,0,0), 1, 8, std::vector<cv::Vec4i>(), 0, cv::Point());
// draw bounding rect
cv::Rect rect = boundingRect(cv::Mat(squares[i]));
cv::rectangle(image, rect.tl(), rect.br(), cv::Scalar(0,255,0), 1, 8, 0);
// draw rotated rect
cv::RotatedRect minRect = minAreaRect(cv::Mat(squares[i]));
cout<<"\nangle"<<minRect.angle;
cv::Point2f rect_points[4];
minRect.points( rect_points );
for ( int j = 0; j < 4; j++ ) {
cv::line( image, rect_points[j], rect_points[(j+1)%4], cv::Scalar(0,0,255), 3, 8 ); // blue
}
}
imshow(" Image", image);
return image;
}
`