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)函数原型二
Python: cv2.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).

 

 

方法一:

#include "stdafx.h"
#include <string>
#include <opencv2\opencv.hpp>
#include "cv.h"
#include "highgui.h"

using namespace cv;
using namespace std;

int main()
{
 IplImage* img = cvLoadImage( "lena.jpg" );  
 // cvRectangle函数参数: 图片, 左上角, 右下角, 颜色, 线条粗细, 线条类型,点类型  
 cvRectangle( img, cvPoint(100, 100), cvPoint(200, 200), cvScalar(0, 0, 255), 3, 4, 0 ); 
 cvNamedWindow( "donkeyaime", CV_WINDOW_AUTOSIZE );  
 cvShowImage( "donkeyaime", img );  
 cvWaitKey(0);  
 cvReleaseImage( &img );
}

 

 

方法二:

#include <string>
#include <opencv2\opencv.hpp>
#include "cv.h"
#include "highgui.h"

using namespace cv;
using namespace std;

Rect select;//声明矩形
Point pt1;//点坐标1
Point pt2;//点坐标2

int main()
{
string imagename = "lena.jpg";   //此处需要填写绝对地址,我测试时使用相对地址出错。
Mat img = imread(imagename);//读入图像

select.x = 50;
select.y = 50;
select.width =100;
select.height =100;

pt1.x = 200;
pt1.y = 200;
pt2.x = 250;
pt2.y = 250;
  
rectangle(img,select,Scalar(0,0,255),3,8,0);//用矩形画矩形窗
rectangle(img,pt1,pt2,Scalar(0,0,255),3,8,0);//用点画矩形窗
namedWindow("image",1); 
imshow("image", img);
waitKey(0);
}

 
  • 11
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值