openCV实战(一):rectangle函数使用
rectangle函數(shù)使用
- rect類
- Rect對象的定義:
- rectangle函數(shù)
- 連續(xù)繪制多個矩形
rect類
Rect對象的定義:
typedef Rect_<int> Rect;再看Rect_的定義:
/*!The 2D up-right rectangle classThe 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 constructorsRect_();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 cornerPoint_<_Tp> tl() const;//! the bottom-right cornerPoint_<_Tp> br() const;//! size (width, height) of the rectangleSize_<_Tp> size() const;//! area (width*height) of the rectangle_Tp area() const;//! conversion to another data typetemplate<typename _Tp2> operator Rect_<_Tp2>() const;//! conversion to the old-style CvRectoperator CvRect() const;//! checks whether the rectangle contains the pointbool contains(const Point_<_Tp>& pt) const;_Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle };從上面的定義至少可以發(fā)現(xiàn)兩點:
- 類Rect_的類模板中的數(shù)據(jù)類型_Tp在Rect_中被指定為整型;
- Rect_的構造函數(shù)可以看出,其形參列表一共有6種形式:
在OpenCV庫中,圖像像素坐標與所在行列數(shù)的對應關系為:x -> col, y -> row, width -> cols, height -> rows
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); roi1.copyTo(image(rect5)); // copy the region rect1 to the designated region in the image imshow("5", image); waitKey(0);rectangle函數(shù)
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)函數(shù)繪制填充了色彩的矩形
@param lineType Type of the line.
連續(xù)繪制多個矩形
#include <iostream> #include <opencv2/opencv.hpp>#include "core/core.hpp" #include "highgui/highgui.hpp" #include "imgproc/imgproc.hpp"//連續(xù)畫矩形框 using namespace cv; using namespace std; //去掉以上兩行代碼注釋之后就可以不用在下面的代碼中加上cv::和std:: //cv::為OpenCV的函數(shù)庫 //std::為C++的標準函數(shù)庫void OnMouseAction(int event, int x, int y, int flags, void *ustc); //鼠標回調(diào)事件函數(shù) Rect rect,temp; Mat src, src1;void draw_rec(Mat str, Mat src1); void draw_rec1(Mat str, Mat src1); int static times; //記錄調(diào)用次數(shù) 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);return 0; } //*******************************************************************// //鼠標回調(diào)函數(shù)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 << "觸發(fā)左鍵按下坐標為" << x << "," << y << endl;break;//鼠標移動事件case CV_EVENT_MOUSEMOVE://當左鍵按下時根據(jù)左鍵起始點繪制生成的矩形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 << "觸發(fā)左鍵松開坐標為" << x << "," << y << endl;cout << "選中區(qū)域個數(shù)為" << rec_num << endl;default:break;}//cout << "第 " << times << " 次回調(diào)鼠標事件" << endl;//if (event == CV_EVENT_MOUSEMOVE)//{// cout << "觸發(fā)鼠標移動事件" << endl;//}//if (event == CV_EVENT_LBUTTONDOWN)//{// cout << "觸發(fā)左鍵按下事件" << endl;//}//if (event == CV_EVENT_LBUTTONUP)//{// cout << "觸發(fā)左鍵抬起事件" << endl;//}//if (event == CV_EVENT_RBUTTONDOWN)//{// cout << "觸發(fā)右鍵按下事件" << endl;// DrawRect(); //在圖像上畫四邊形//}//if (event == CV_EVENT_RBUTTONUP)//{// cout << "觸發(fā)右鍵抬起事件" << endl;//}//if (event == CV_EVENT_LBUTTONDBLCLK)//{// cout << "觸發(fā)左鍵雙擊事件" << endl;//}//if (event == CV_EVENT_RBUTTONDBLCLK)//{// cout << "觸發(fā)右鍵雙擊事件" << endl;//} }void draw_rec(Mat src, Mat src1) {src.copyTo(src1);rectangle(src1, rect, Scalar(0, 0, 255));src1.copyTo(src);imshow("title", src1);}void draw_rec1(Mat src, Mat src1) {src.copyTo(src1);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);if (mat.empty()) {throw("Faild open file.");}cv::Point p0 = cv::Point(mat.cols / 8, mat.rows / 8);cv::Point p1 = cv::Point(mat.cols * 7 / 8, mat.rows * 7 / 8);//設定點的起始和終止坐標rectangle(mat, p0, p1, cv::Scalar(0, 255, 0), 5, 8);//畫四邊形的函數(shù)//第一個參數(shù)為畫圖的目標圖像//第二個參數(shù)為畫圖的起始坐標//第三個參數(shù)為畫圖的終止坐標//第四個參數(shù)為畫圖的顏色cv::Point p2 = cv::Point(mat.cols * 2 / 8, mat.rows * 2 / 8);cv::Point p3 = cv::Point(mat.cols * 6 / 8, mat.rows * 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();return 0; }總結
以上是生活随笔為你收集整理的openCV实战(一):rectangle函数使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openCV学习教程(一):Mat类的使
- 下一篇: linux:命令常用操作