rectangle()函数:
在给定的图片上画出一个矩形框;有两个定义:
定义1:
@param img Image.
@param pt1 Vertex of the rectangle.矩形的顶点
@param pt2 Vertex of the rectangle opposite to pt1 .与p1相反的矩形的顶点
@param color Rectangle color or brightness (grayscale image).
@param thickness Thickness of lines that make up the rectangle. Negative values, like #FILLED,
mean that the function has to draw a filled rectangle.
@param lineType Type of the line. See #LineTypes
@param shift Number of fractional bits in the point coordinates.
*/
CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc,char *agrv[])
{
Mat imag = imread("C:\\Users\\Desktop\\picture.jpg");
if (!imag.data)
{
cout << "Open error!" << endl;
return -1;
}
rectangle(imag, Point(1000, 400), Point(imag.rows, imag.cols*0.55), Scalar(0, 255, 0), 2, 8);
imshow("【创建矩形框】", imag);
waitKey(0);
return 0;
}
定义2:此方法需要借助Rect类;
CV_EXPORTS_W void rectangle(InputOutputArray img, Rect rec,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
Rect的一个最常用的重载函数:
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
_Tp x; //!< x coordinate of the top-left corner,左上角y的坐标
_Tp y; //!< y coordinate of the top-left corner,左上角y的坐标
_Tp width; //!< width of the rectangle,矩形的宽度
_Tp height; //!< height of the rectangle,矩形的高度;
x,y是确定矩形框在输入图片的位置;
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc,char *agrv[])
{
Mat imag = imread("C:\\Users\\Desktop\\picture.jpg");
if (!imag.data)
{
cout << "Open error!" << endl;
return -1;
}
/*rectangle(imag, Point(1000, 400), Point(imag.rows, imag.cols*0.55), Scalar(0, 255, 0), 2, 8);*/
Rect rec(750, 400, 280,250);
rectangle(imag, rec, Scalar(0,255, 0), 2, 8, 0);
imshow("【创建矩形框】", imag);
waitKey(0);
return 0;
}