opencv(Pythonc++):画矩形框(普通的与旋转的矩形框)我们常常需要⽤矩形框
效果图1:普通矩形+⽂字
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include"opencv2/imgproc/imgproc.hpp"
using namespace cv;
/
/宏定义部分
#define Win_Name3 "矩阵+字"
//main函数
int main(int argc,char** argv)
rectangle函数opencv{
Mat image3 = Mat::zeros(800,800, CV_8UC3);//⽣成⼀个800x800的窗⼝
rectangle (image3,Point(100,300),Point(700,500),Scalar(0,0,255),2,8,0);//黄⾊矩形框
putText(image3,"hit area",Point(100,300),FONT_HERSHEY_SIMPLEX,1,Scalar(255,0,0),2);
imshow(Win_Name3, image3);
while(1)
{
int key=cvWaitKey(10);
if(key==27)
{
break;
}
}
return0;
}
效果图2:旋转矩形
⽬前opencv中没有直接绘制旋转矩形的函数,⽽是通过⾃主绘制的。
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat image_source =imread("/home/liuxin/桌⾯/opencv/hammer.jpeg");
vector<Point> vec_pts;
Point p1(800,400),p2(1100,605),p3(902,970),p4(802,780);//随意给了4个点 vec_pts.push_back(p1);
vec_pts.push_back(p2);
vec_pts.push_back(p3);
vec_pts.push_back(p4);
//将四个点表⽰出来,以圆的形式
circle(image_source,p1,20,Scalar(0,255,255),5);
circle(image_source,p2,20,Scalar(0,255,255),5);
circle(image_source,p3,20,Scalar(0,255,255),5);
circle(image_source,p4,20,Scalar(0,255,255),5);
//根据外接矩形的顶点,再⼀个⼀个连线连起来,构成带⾓度的矩形
RotatedRect rect =minAreaRect(vec_pts);//外接矩形
Point2f vertices[4];
rect.points(vertices);//外接矩形的4个顶点
for(int i =0; i <4; i++)//画矩形
line(image_source , vertices[i], vertices[(i +1)%4],Scalar(255,0,0),3); imshow("rotation rect", image_source);
//将roi区域提取出来
Point2f center = ;//外接矩形中⼼点坐标
Mat rot_mat =getRotationMatrix2D(center, rect.angle,1.0);//求旋转矩阵
Mat rot_image;
Size dst_sz(image_source.size());
warpAffine(image_source, rot_image, rot_mat, dst_sz);//原图像旋转
Mat result1 =rot_image(Rect(center.x -(rect.size.width /2),
center.y -(rect.size.height/2),
rect.size.width,
rect.size.height));//提取ROI
imshow("result", result1);
while(1)
{
int key=cvWaitKey(10);
if(key==27)
{
break;
}
}
return0;
return0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论