openCV实战(⼀):rectangle函数使⽤rectangle函数使⽤
rect类
Rect对象的定义:
typedef Rect_<int> Rect;
再看Rect_的定义:
/*!
The 2D up-right rectangle class
The class represents a 2D rectangle with coordinates of the specified data type.
Normally, cv::Rect ~ cv::Rect_<int> is used.
*/
template<typename _Tp>class Rect_
{
public:
typedef _Tp value_type;
//! various constructors
Rect_();
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
Rect_(const Rect_& r);
Rect_(const CvRect& r);
Rect_(const Point_<_Tp>& org,const Size_<_Tp>& sz);
Rect_(const Point_<_Tp>& pt1,const Point_<_Tp>& pt2);
Rect_&operator=(const Rect_& r );
/
/! the top-left corner
Point_<_Tp>tl()const;
//! the bottom-right corner
Point_<_Tp>br()const;
//! size (width, height) of the rectangle
Size_<_Tp>size()const;
//! area (width*height) of the rectangle
_Tp area()const;
//! conversion to another data type
template<typename _Tp2>operator Rect_<_Tp2>()const;
//! conversion to the old-style CvRect
operator CvRect()const;
//! checks whether the rectangle contains the point
bool contains(const Point_<_Tp>& pt)const;
_Tp x, y, width, height;//< the top-left corner, as well as width and height of the rectangle
};
从上⾯的定义⾄少可以发现两点:
类Rect_的类模板中的数据类型_Tp在Rect_中被指定为整型;
Rect_的构造函数可以看出,其形参列表⼀共有6种形式:
Rect_(),// 形参列表为空,即定义⼀个空窗⼝(默认值为:x=y=width=height=0);
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height),//定义⼀个左上⾓点坐标为(_x, _y)的_width*_height矩形窗⼝;
Rect_(const Rect_& r),//使⽤其他的Rect_对象初始化;
Rect_(const CvRect& r),//使⽤CvRect对象初始化;
Rect_(const Point_<_Tp>& org,const Size_<_Tp>& sz),//分别将位置坐标(_x, _y)和窗⼝⼤⼩(_width, _height)⽤Point_和Size_对象初始化;
Rect_(const Point_<_Tp>& pt1,const Point_<_Tp>& pt2),//分别将坐标位置(_x, _y)和窗⼝⼤⼩(_width, _height)⽤Point_和Point_对象初始化。
在OpenCV库中,图像像素坐标与所在⾏列数的对应关系为:x -> col, y -> row, width -> cols, height -> rows
mousemove是什么键Mat image =imread("C:\\Users\\Leo\\Desktop\\lena.jpg");
Rect rect1(256,256,128,128);
Rect rect2(224,224,128,128);
Mat roi1;
image(rect1).copyTo(roi1);// copy the region rect1 from the image to roi1
imshow("1", roi1);
waitKey(0);
Mat roi2;
image(rect2).copyTo(roi2);// copy the region rect2 from the image to roi2
imshow("2", roi2);
waitKey(0);
cv::Rect rect3 = rect1&rect2;// intersection of the two sets
Mat roi3;
image(rect3).copyTo(roi3);
imshow("3", roi3);
waitKey(0);
Rect rect4 = rect1|rect2;// union of the two sets (the minimum bounding rectangle)
Mat roi4;
image(rect4).copyTo(roi4);
imshow("4", roi4);
waitKey(0);
Rect rect5(10,10,128,128);
imshow("5", image);
waitKey(0);
rectangle函数
void rectangle(InputOutputArray img, Point pt1, Point pt2,const Scalar& color,int thickness =1,int lineType = LINE_8,int shift =0);
简介:使⽤对⾓线的两点pt1,pt2画⼀个矩形轮廓或者填充矩形
@param img Image.
@param pt1 Vertex of the rectangle.
@param pt2 Vertex of the rectangle opposite to pt1 .
@param color Rectangle color or brightness (grayscale image).color 线条颜⾊ (RGB) 或亮度(灰度图像 )
@param thickness Thickness of lines that make up the rectangle. Negative values, like CV_FILLED ,
mean that the function has to draw a filled rectangle.thickness 组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了⾊彩的矩形
@param lineType Type of the line.
连续绘制多个矩形
#include<iostream>
#include<opencv2/opencv.hpp>
#include"core/core.hpp"
#include"highgui/highgui.hpp"
#include"imgproc/imgproc.hpp"
#include"imgproc/imgproc.hpp"
//连续画矩形框
using namespace cv;
using namespace std;
//去掉以上两⾏代码注释之后就可以不⽤在下⾯的代码中加上cv::和std::
//cv::为OpenCV的函数库
//std::为C++的标准函数库
void OnMouseAction(int event,int x,int y,int flags,void*ustc);//⿏标回调事件函数
Rect rect,temp;
Mat src, src1;
void draw_rec(Mat str, Mat src1);
void draw_rec1(Mat str, Mat src1);
int static times;//记录调⽤次数
int DrawRect();
int rec_num;
int main(int argc,char*argv[])
{
//以下的⽅法如果想运⾏,直接把前头的注释去掉即可
/
/运⾏代码的时候请改掉filename⾥的地址
//我的opencv是安装在C盘⽬录下
//⼀些⽬录地址请⾃⾏修改⼀下
//DrawRect(); //在图像上画四边形
const char* filename ="E:\\projects\\darknet-master\\build\\darknet\\x64\\data\\voc\\VOCdevkit\\VOC2007\\JPEGImages\\img1.bmp"; src =imread(filename);
imshow("title", src);
rec_num =0;
setMouseCallback("title", OnMouseAction,0);
waitKey(0);
return0;
}
//*******************************************************************//
//⿏标回调函数
void OnMouseAction(int event,int x,int y,int flags,void*ustc)
{
times++;
switch(event)
{
//左键按下事件
case EVENT_LBUTTONDOWN:
//左键按下定义起始点
rect.x = x;
rect.y = y;
rect.width =1;
rect.height =1;
cout <<"触发左键按下坐标为"<< x <<","<< y << endl;
break;
//⿏标移动事件
case CV_EVENT_MOUSEMOVE:
//当左键按下时根据左键起始点绘制⽣成的矩形
if(flags&EVENT_FLAG_LBUTTON)
{
rect =Rect(Point(rect.x, rect.y),Point(x, y));
draw_rec1(src,src1);//⿏标移动过程中显⽰移动轨迹
}
}
break;
//左键松开事件
case CV_EVENT_LBUTTONUP:
if(rect.width >1&& rect.height >1)
{
draw_rec(src,src1);
rec_num++;
}
cout <<"触发左键松开坐标为"<< x <<","<< y << endl;  cout <<"选中区域个数为"<< rec_num << endl; default:
break;
}
//cout << "第 " << times << " 次回调⿏标事件" << endl; //if (event == CV_EVENT_MOUSEMOVE)
//{
// cout << "触发⿏标移动事件" << endl;
//}
//if (event == CV_EVENT_LBUTTONDOWN)
//{
// cout << "触发左键按下事件" << endl;
//}
//if (event == CV_EVENT_LBUTTONUP)
//{
// cout << "触发左键抬起事件" << endl;
//}
//if (event == CV_EVENT_RBUTTONDOWN)
//{
// cout << "触发右键按下事件" << endl;
// DrawRect(); //在图像上画四边形
//}
//if (event == CV_EVENT_RBUTTONUP)
/
/{
// cout << "触发右键抬起事件" << endl;
//}
//if (event == CV_EVENT_LBUTTONDBLCLK)
//{
// cout << "触发左键双击事件" << endl;
//}
//if (event == CV_EVENT_RBUTTONDBLCLK)
//{
// cout << "触发右键双击事件" << endl;
//}
}
void draw_rec(Mat src, Mat src1)
{
rectangle(src1, rect,Scalar(0,0,255));
imshow("title", src1);
}
void draw_rec1(Mat src, Mat src1)
{
rectangle(src1, rect,Scalar(0,0,255));
rectangle(src1, rect,Scalar(0,0,255));
imshow("title", src1);
}
int DrawRect(){
const char* filename ="E:\\projects\\darknet-master\\build\\darknet\\x64\\data\\voc\\VOCdevkit\\VOC2007\\JPEGImages\\img1.bmp";
cv::Mat mat = cv::imread(filename);
pty()){
throw("Faild open file.");
}
cv::Point p0 = cv::ls /8, ws /8);
cv::Point p1 = cv::ls *7/8, ws *7/8);
//设定点的起始和终⽌坐标
rectangle(mat, p0, p1, cv::Scalar(0,255,0),5,8);
//画四边形的函数
//第⼀个参数为画图的⽬标图像
//第⼆个参数为画图的起始坐标
//第三个参数为画图的终⽌坐标
//第四个参数为画图的颜⾊
cv::Point p2 = cv::ls *2/8, ws *2/8);
cv::Point p3 = cv::ls *6/8, ws *6/8);
rectangle(mat, p2, p3, cv::Scalar(0,255,255),2,4);
cv::imshow("mat", mat);
//cv::imwrite("C:\\Code\\FirstOpenCVProgramming\\DrawRect.jpg", mat);
cv::waitKey();
return0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。