openCV 画矩形框或填充矩形

目录(?)[+]

rectangle:画矩形
Draws a simple, thick, or filled up-right rectangle.画一个简单或填充的矩形
C++: void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)函数原型一
C++: void rectangle(Mat& img, Rect r, const Scalar& color, int thickness=1, int lineType=8, int shift=0)函数原型二
Pythoncv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift ]]])! None
C: void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line Type=8, int shift=0 )函数原型三
Python: cv.Rectangle(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) !None
Parameters参数
img – Image.
pt1 – Vertex of the rectangle.
pt2 – Vertex of the recangle opposite to pt1 .
r – Alternative specification of the drawn rectangle.
color – Rectangle color or brightness (grayscale image).
thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED
, mean that the function has to draw a filled rectangle.
lineType – Type of the line. See the line() description.
shift – Number of fractional bits in the point coordinates.
The function rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are pt1 and pt2,
or r.tl() and r.br()-Point(1,1).

 

 

方法一:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <string>  
  3. #include <opencv2\opencv.hpp>  
  4. #include "cv.h"  
  5. #include "highgui.h"  
  6.   
  7. using namespace cv;  
  8. using namespace std;  
  9.   
  10. int main()  
  11. {  
  12.  IplImage* img = cvLoadImage( "lena.jpg" );    
  13.  // cvRectangle函数参数: 图片, 左上角, 右下角, 颜色, 线条粗细, 线条类型,点类型    
  14.  cvRectangle( img, cvPoint(100, 100), cvPoint(200, 200), cvScalar(0, 0, 255), 3, 4, 0 );   
  15.  cvNamedWindow( "donkeyaime", CV_WINDOW_AUTOSIZE );    
  16.  cvShowImage( "donkeyaime", img );    
  17.  cvWaitKey(0);    
  18.  cvReleaseImage( &img );  
  19. }  

 

 

方法二:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <string>  
  2. #include <opencv2\opencv.hpp>  
  3. #include "cv.h"  
  4. #include "highgui.h"  
  5.   
  6. using namespace cv;  
  7. using namespace std;  
  8.   
  9. Rect select;//声明矩形  
  10. Point pt1;//点坐标1  
  11. Point pt2;//点坐标2  
  12.   
  13. int main()  
  14. {  
  15. string imagename = "lena.jpg";   //此处需要填写绝对地址,我测试时使用相对地址出错。  
  16. Mat img = imread(imagename);//读入图像  
  17.   
  18. select.x = 50;  
  19. select.y = 50;  
  20. select.width =100;  
  21. select.height =100;  
  22.   
  23. pt1.x = 200;  
  24. pt1.y = 200;  
  25. pt2.x = 250;  
  26. pt2.y = 250;  
  27.     
  28. rectangle(img,select,Scalar(0,0,255),3,8,0);//用矩形画矩形窗  
  29. rectangle(img,pt1,pt2,Scalar(0,0,255),3,8,0);//用点画矩形窗  
  30. namedWindow("image",1);   
  31. imshow("image", img);  
  32. waitKey(0);  
  33. }  
  34.   
  35.    
1
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值