最小外接矩形minAreaRect()及截取旋转矩形区域

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;
}

 

 

参考:

https://blog.csdn.net/u010847519/article/details/72625326

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值