1、RotatedRect旋转矩形
旋转矩形RotatedRect是minAreaRect()方法的返回值。RotatedRect minAreaRect( InputArray points )
RotatedRect由三个变量组成:1)旋转矩形的中心center,由(x,y)表示;2)旋转矩形的长和宽size,由(width,height)表示;3)旋转矩形的角度angle。
旋转矩形RotatedRect提供了将旋转矩形转化成四个点的方法,points函数,位置顺序分别为The order is bottomLeft topLeft, topRight, bottomRight。
findContours(findCon_result_ROI, contours, hierarchy, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
if (contours.size() == 0) {
return false;
}
Point2f rect[4];
for (int i = 0; i < contours.size(); i++) {
RotatedRect cur_box;
cur_box = minAreaRect(Mat(contours[i]));
cur_box.points(rect);
for (int j = 0; j < 4; j++)
{
line(SHOW_IMAGE, rect[j], rect[(j + 1) % 4], Scalar(0, 0, 255), 2, 8); //绘制最小外接矩形每条边
}
}
2、最小外接矩形的计算函数API---minAreaRect()
The function calculates and returns the minimum-area bounding rectangle (possibly rotated) for a specified point set. Developer should keep in mind that the returned RotatedRect can contain negative indices when data is close to the containing Mat element boundary. @param points Input vector of 2D points, stored in std::vector\<\> or Mat
3、截取旋转矩形区域
截取旋转矩形区域如下方法:
Mat WarpQuadrangle(const cv::Mat & img, const std::vector<cv::Point2f> & quadrangle, const cv::Size & wrap_size)
{
cv::Point2f point_dst[4], point_src[4];
for (int j = 0; j < 4; j++) point_src[j] = quadrangle[j];
point_dst[0] = (cv::Point2f(0, 0));
point_dst[1] = cv::Point2f(0.f, static_cast<float>(wrap_size.height - 1));
point_dst[2] = cv::Point2f(static_cast<float>(wrap_size.width - 1), static_cast<float>(wrap_size.height - 1));
point_dst[3] = cv::Point2f(static_cast<float>(wrap_size.width - 1), 0.f);
cv::Mat M = getPerspectiveTransform(point_src, point_dst);
cv::Mat out;
cv::warpPerspective(img, out, M, wrap_size, cv::INTER_LINEAR);
return out;
}
参考: