- Point表示2D平面上的一个点(x,y)
1、Point p;
p.x = 10;
p.y = 8;
2、p = Point(10,8);
- Rect 矩阵类
Rect rect(点a,点b);
- Scalar表示四个元素的向量(不能大于四个,可以是零个)
Scalar对象,用来建立颜色对象,初始化通道。
例如
Scalar color = Scalar(255,0,0); //三通道
Scalar color = Scalar(255); //单通道
//函数size()的返回值是一个Size类,
//这个类的属性有width、height
绘制线、矩形、圆、椭圆等基本几何形状
线型有三种,默认LINE_8,LINE_AA是抗锯齿类型。
Size对象用来确定图形图像的长宽参数,初始化方式Size(int x,int y),ellipse(Mat对象,中心点对象,Size对象,旋转角度,绘制起始角度,绘制终止角度,颜色,线粗,线型)//Size对象得x代表椭圆长轴半径,y代表椭圆短轴半径,旋转角度表示椭圆得整体旋转角度,当角度为0时,长轴对应横轴,绘制起始和终止角度代表绘制覆盖的角度。
代码示例如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
char WindowName[] = "Black-Board";
void DrawLine(Mat& Image);
void DrawRect(Mat& Image);
void DrawEillpse(Mat& Image);
void DrawCricle(Mat& Image);
void DrawPoly(Mat& Image);
void DrawRandomColorText(Mat& Image, char text[]);
int main(int argc, char** argv)
{
Mat blackboard = Mat::zeros(400,400,CV_8UC3);
DrawLine(blackboard);
DrawRect(blackboard);
DrawEillpse(blackboard);
DrawCricle(blackboard);
DrawPoly(blackboard);
DrawRandomColorText(blackboard,"HelloOpencv");
waitKey(0);
return 0;
}
void DrawLine(Mat& Image)
{
Point a, b;
a.x = 0;
a.y = 0;
b.x = 400;
b.y = 400;
Scalar color(255,0,0);
line(Image,a,b,color,2);
}
void DrawRect(Mat& Image)
{
Point a = Point(Image.cols/4,Image.rows/4);
Point b = Point(Image.cols*3/4,Image.rows*3/4);
Rect rect(a,b);
Scalar color = Scalar(0,255,0);
rectangle(Image,rect,color,2);
}
void DrawEillpse(Mat& Image)
{
Point cen(Image.cols / 2, Image.rows / 2);
Size len = Size(100,200);
Scalar color(0,0,255);
ellipse(Image,cen,len,90,0,360,color);
}
void DrawCricle(Mat& Image)
{
Point cen = Point(Image.cols/2,Image.rows/2);
Scalar color(255, 255, 255);
int radius = 200;
circle(Image,cen,radius,color,2);
}
void DrawPoly(Mat& Image)
{
Point pts[1][4];
pts[0][0] = Point(50,50);
pts[0][1] = Point(150, 100);
pts[0][2] = Point(200, 400);
pts[0][3] = Point(50,50);
const Point* p[] = {pts[0]};
const int nps[] = { 4 };
Scalar color(255, 255, 255);
fillPoly(Image,p,nps,1,color);
}
void DrawRandomColorText(Mat& Image,char text[])
{
RNG rng;
Scalar color;
Point p_text(Image.cols / 4, Image.rows/2);
namedWindow(WindowName, WINDOW_AUTOSIZE);
while(1)
{
color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
putText(Image, text, p_text,CV_FONT_BLACK,2.0,color,2);
imshow(WindowName,Image);
waitKey(200);
}
}