结构分析与形状识别(外接矩形 旋转角度)
来源:互联网 发布:软件开发工资 北京 编辑:程序博客网 时间:2024/04/27 21:58
class CV_EXPORTS RotatedRect {public: //! various constructors RotatedRect(); RotatedRect(const Point2f& center, const Size2f& size, float angle); RotatedRect(const CvBox2D& box); //! returns 4 vertices of the rectangle void points(Point2f pts[]) const; //! returns the minimal up-right rectangle containing the rotated rectangle Rect boundingRect() const; //! conversion to the old-style CvBox2D structure operator CvBox2D() const; Point2f center; //< the rectangle mass center Size2f size; //< width and height of the rectangle float angle; //< the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle. };RotatedRect::points(Point2f pt[]) const { double _angle = angle*CV_PI/180.; float b = (float)cos(_angle)*0.5f; float a = (float)sin(_angle)*0.5f; pt[0].x = center.x - a*size.height - b*size.width; pt[0].y = center.y + b*size.height - a*size.width; pt[1].x = center.x + a*size.height - b*size.width; pt[1].y = center.y - b*size.height - a*size.width; pt[2].x = 2*center.x - pt[0].x; pt[2].y = 2*center.y - pt[0].y; pt[3].x = 2*center.x - pt[1].x; pt[3].y = 2*center.y - pt[1].y; }
#include"iostream" #include"opencv2/opencv.hpp" using namespace std; using namespace cv; int main() { Mat image(200, 200, CV_8UC3, Scalar(0)); RotatedRect rRect(Point2f(100, 100), Size2f(100, 50), 30); Point2f vertices[4]; //定义矩形的4个顶点 rRect.points(vertices); //计算矩形的4个顶点 for (int i = 0; i < 4; i++) line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0)); Rect brect = rRect.boundingRect(); //返回包含旋转矩形的最小矩形 rectangle(image, brect, Scalar(255, 0, 0)); imshow("rectangles", image); waitKey(0); }RotatedRect是一个存储旋转矩形的类,通常用来存储最小外包矩形函数minAreaRect( )和椭圆拟合函数fitEllipse( )返回的结果。存储的值,完全取决在于函数的返回
之前用到OpenCV最小外接矩形去表示一个类椭圆形的高度,特此记录备查。对给定的 2D 点集,寻找最小面积的包围矩形,使用函数:CvBox2D cvMinAreaRect2( const CvArr* points, CvMemStorage* storage=NULL ); points 点序列或点集数组 storage 可选的临时存储仓 函数 cvMinAreaRect2 通过建立凸外形并且旋转外形以寻找给定 2D 点集的最小面积的包围矩形。其中返回的2D盒子定义如下:1 typedef struct CvBox2D 2 { 3 CvPoint2D32f center; /* 盒子的中心 */ 4 CvSize2D32f size; /* 盒子的长和宽 */ 5 float angle; /* 水平轴与第一个边的夹角,用弧度表示*/ 6 }CvBox2D; 注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角。如下图 可用函数 cvBoxPoints(box[count], point); 寻找盒子的顶点1 void cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] ) 2 { 3 double angle = box.angle*CV_PI/180. 4 float a = (float)cos(angle)*0.5f; 5 float b = (float)sin(angle)*0.5f; 6 7 pt[0].x = box.center.x - a*box.size.height - b*box.size.width; 8 pt[0].y = box.center.y + b*box.size.height - a*box.size.width; 9 pt[1].x = box.center.x + a*box.size.height - b*box.size.width; 10 pt[1].y = box.center.y - b*box.size.height - a*box.size.width; 11 pt[2].x = 2*box.center.x - pt[0].x; 12 pt[2].y = 2*box.center.y - pt[0].y; 13 pt[3].x = 2*box.center.x - pt[1].x; 14 pt[3].y = 2*box.center.y - pt[1].y; 15 } 简单证明此函数的计算公式: 计算x,由图可得到三个方程式: pt[1].x - pt[0].x = width*sin(angle) pt[2].x - pt[1].x = height*cos(angle) pt[2].x - pt[0].x = 2(box.center.x - pt[0].x) 联立方程可解得函数里的计算式,算 y 略。写了个函数绘制CvBox2D1 void DrawBox(CvBox2D box,IplImage* img) 2 { 3 CvPoint2D32f point[4]; 4 int i; 5 for ( i=0; i<4; i++) 6 { 7 point[i].x = 0; 8 point[i].y = 0; 9 } 10 cvBoxPoints(box, point); //计算二维盒子顶点 11 CvPoint pt[4]; 12 for ( i=0; i<4; i++) 13 { 14 pt[i].x = (int)point[i].x; 15 pt[i].y = (int)point[i].y; 16 } 17 cvLine( img, pt[0], pt[1],CV_RGB(255,0,0), 2, 8, 0 ); 18 cvLine( img, pt[1], pt[2],CV_RGB(255,0,0), 2, 8, 0 ); 19 cvLine( img, pt[2], pt[3],CV_RGB(255,0,0), 2, 8, 0 ); 20 cvLine( img, pt[3], pt[0],CV_RGB(255,0,0), 2, 8, 0 );
阅读全文
0 0
- 结构分析与形状识别(外接矩形 旋转角度)
- 《cv中文参考手册-图像轮廓处理-结构分析与形状识别--外接正矩形轮廓区域》
- 《cv中文参考手册-图像轮廓处理-结构分析与形状识别-外接轮廓》
- OpenCV 求外接矩形以及旋转角度
- Opencv寻找轮廓的最小外接矩形,并获取矩形的中心点,旋转角度
- 提取有角度的形状的最小矩形,求其旋转角度、长宽
- 计算矩形旋转角度(不精确)
- opencv结构分析与形状识别-轮廓检测和填充(连通区域-边缘与整个图像的目标)
- Opencv 图像结构分析与形状识别—— drawContours函数用于绘制和填充
- hdu 5251 矩形面积 (旋转卡壳求最小面积外接矩形)
- opencv图像结构与形状分析
- 最小外接矩形(ROI)
- opencv--外接矩形(圆)
- 旋转卡壳法求点集最小外接矩形(面积)并输出四个顶点坐标
- 《cv中文参考手册-图像轮廓处理-结构分析与形状识别-获取各个对象的质心》HuMoments
- opencv 在图片中找对应矩形(形状分析)
- 旋转卡壳——凸多边形最小面积外接矩形
- 旋转卡壳——凸多边形最小周长外接矩形
- 关于typedef和define的两个错误用法
- 二叉树递归与非递归层次遍历
- WebStorm使用教程
- lua 二进制字节与int相互转换
- 2017百度之星初赛(A)1001 小C的倍数问题(求因子数)
- 结构分析与形状识别(外接矩形 旋转角度)
- 解决linux运行脚本报错^M: bad interpreter: No such file or directory
- VM虚拟机设置笔记本wifi网络
- 手写TCP
- LeetCode 402. Remove K Digits
- ionic 照片不能上传
- Android架构组件
- [RK3288][Android7.1.2] MTP模式
- [bzoj3674/3673]可持久化并查集加强版 主席树