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 
#include 
#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 );
}

 

 openCV 画矩形框或填充矩形_第1张图片

方法二:

#include 
#include 
#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);
}

 
 
 

你可能感兴趣的:(openCV)