1.目的
(1)如何用 Point 在图像中定义 2D 点
(2)如何以及为何使用 Scalar
(3)用OpenCV的函数 line 绘 直线
(4)用OpenCV的函数 ellipse 绘 椭圆
(5)用OpenCV的函数 rectangle 绘 矩形
(6)用OpenCV的函数 circle 绘 圆
(7)用OpenCV的函数 fillPoly 绘 填充的多边形
2.Point与Scalar讲解
(1)Point
创建Point对象
Point pt;
pt.x = 1;
pt.y = 2;
Point pt(1,2);
Point pt = Point(1,2);
(2)Scalar
表示了具有四个元素的数组。
可以使用Scalar表示RGB颜色值:Scalar(a,b,c),那么定义的RGB颜色值为:blue:c,green:b,red:a
3.绘图函数介绍
(1)line
void MyLine(Mat& image, Point start,Point end){
if(!image.data){
cout << "MyLine! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
line参数解释:
image:绘制面板
start:起始点
end:结束点
Scalar(0,0,0):线颜色
thickness:线粗细
lineType:线类型
*/
line(image, start, end, Scalar(255,255,255), thickness, lineType);
}
(2)ellipse
void MyEllipse(Mat& image, double angle, int w){
if(!image.data){
cout << "MyLine! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
ellipse参数解释
image:绘制画板
Point:椭圆中心点
Size:绘制椭圆所在矩阵大小
angle:椭圆旋转角度
0-360:椭圆弧度拓展范围:0-360度
Scalar(255,255,255):画线颜色
thickness:线条粗细
lineType:线条类型
*/
ellipse(image, Point(w/2.0,w/2.0),Size(w/4.0,w/16.0), angle, 0, 360, Scalar(255,255,255), thickness, lineType);
}
(3)circle
void MyCircle(Mat& image, Point center, double radius){
if(!image.data){
cout << "MyCircle! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
circle参数解释
image:绘制画板
center:圆心
radius:圆半径
Scalar(255,255,255):画线颜色
thickness:线条粗细
lineType:线条类型
*/
circle(image, center, radius, Scalar(255,0,0), thickness, lineType);
}
(4)fillPoly
void MyPolygon(Mat& image, int w){
if(!image.data){
cout << "MyPolygon! Image error!!!" << endl;
return;
}
int lineType = 8;
/** 创建点 */
Point rook_points[1][20];
rook_points[0][0] = Point( w/4.0, 7*w/8.0 );
rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );
rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );
rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 );
rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 );
rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 );
rook_points[0][6] = Point( 3*w/4.0, w/8.0 );
rook_points[0][7] = Point( 26*w/40.0, w/8.0 );
rook_points[0][8] = Point( 26*w/40.0, w/4.0 );
rook_points[0][9] = Point( 22*w/40.0, w/4.0 );
rook_points[0][10] = Point( 22*w/40.0, w/8.0 );
rook_points[0][11] = Point( 18*w/40.0, w/8.0 );
rook_points[0][12] = Point( 18*w/40.0, w/4.0 );
rook_points[0][13] = Point( 14*w/40.0, w/4.0 );
rook_points[0][14] = Point( 14*w/40.0, w/8.0 );
rook_points[0][15] = Point( w/4.0, w/8.0 );
rook_points[0][16] = Point( w/4.0, 3*w/8.0 );
rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 );
rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 );
rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
/*fillPoly参数解释
image:绘制画板
ppt:多边形顶点集合
npt:多边形顶点数量
1:要绘制的多边形数量
Scalar(255,255,255):填充的颜色
lineType:线条类型
*/
fillPoly(image, ppt, npt, 1, Scalar(255,0,0), lineType);
}
(5)rectangle
void MyRectangle(Mat& image, Point leftUp, Point rightDown){
if(!image.data){
cout << "MyEllipse! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
rectangle参数解释
image:绘制画板
leftUp:矩形左上角点坐标
rightDown:矩形右下角点坐标
Scalar(255,255,255):画线颜色
thickness:线条粗细
lineType:线条类型
*/
rectangle(image, leftUp, rightDown, Scalar(255,255,255), thickness, lineType);
}
4.完整代码
#include<iostream>
using namespace std;
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
void MyLine(Mat& image, Point start,Point end){
if(!image.data){
cout << "MyLine! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
line参数解释:
image:绘制面板
start:起始点
end:结束点
Scalar(0,0,0):线颜色
thickness:线粗细
lineType:线类型
*/
line(image, start, end, Scalar(255,255,255), thickness, lineType);
}
void MyEllipse(Mat& image, double angle, int w){
if(!image.data){
cout << "MyLine! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
ellipse参数解释
image:绘制画板
Point:椭圆中心点
Size:绘制椭圆所在矩阵大小
angle:椭圆旋转角度
0-360:椭圆弧度拓展范围:0-360度
Scalar(255,255,255):画线颜色
thickness:线条粗细
lineType:线条类型
*/
ellipse(image, Point(w/2.0,w/2.0),Size(w/4.0,w/16.0), angle, 0, 360, Scalar(255,255,255), thickness, lineType);
}
void MyRectangle(Mat& image, Point leftUp, Point rightDown){
if(!image.data){
cout << "MyEllipse! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
rectangle参数解释
image:绘制画板
leftUp:矩形左上角点坐标
rightDown:矩形右下角点坐标
Scalar(255,255,255):画线颜色
thickness:线条粗细
lineType:线条类型
*/
rectangle(image, leftUp, rightDown, Scalar(255,255,255), thickness, lineType);
}
void MyCircle(Mat& image, Point center, double radius){
if(!image.data){
cout << "MyCircle! Image error!!!" << endl;
return;
}
int thickness = 2;
int lineType = 8;
/*
circle参数解释
image:绘制画板
center:圆心
radius:圆半径
Scalar(255,255,255):画线颜色
thickness:线条粗细
lineType:线条类型
*/
circle(image, center, radius, Scalar(255,0,0), thickness, lineType);
}
void MyPolygon(Mat& image, int w){
if(!image.data){
cout << "MyPolygon! Image error!!!" << endl;
return;
}
int lineType = 8;
/** 创建点 */
Point rook_points[1][20];
rook_points[0][0] = Point( w/4.0, 7*w/8.0 );
rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );
rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );
rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 );
rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 );
rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 );
rook_points[0][6] = Point( 3*w/4.0, w/8.0 );
rook_points[0][7] = Point( 26*w/40.0, w/8.0 );
rook_points[0][8] = Point( 26*w/40.0, w/4.0 );
rook_points[0][9] = Point( 22*w/40.0, w/4.0 );
rook_points[0][10] = Point( 22*w/40.0, w/8.0 );
rook_points[0][11] = Point( 18*w/40.0, w/8.0 );
rook_points[0][12] = Point( 18*w/40.0, w/4.0 );
rook_points[0][13] = Point( 14*w/40.0, w/4.0 );
rook_points[0][14] = Point( 14*w/40.0, w/8.0 );
rook_points[0][15] = Point( w/4.0, w/8.0 );
rook_points[0][16] = Point( w/4.0, 3*w/8.0 );
rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 );
rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 );
rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
/*fillPoly参数解释
image:绘制画板
ppt:多边形顶点集合
npt:多边形顶点数量
1:要绘制的多边形数量
Scalar(255,255,255):填充的颜色
lineType:线条类型
*/
fillPoly(image, ppt, npt, 1, Scalar(255,0,0), lineType);
}
int main(int argc, char** argv){
int imageX;
cout << "Input size of image:";
cin >> imageX;
Mat lineImage(imageX,imageX,CV_8UC3);
//绘制直线
/*Point start(0,0);
Point end(imageX,imageX);
MyLine(lineImage,start,end);*/
//绘制椭圆
/*double angle = 0.0;
MyEllipse(lineImage, angle, imageX);*/
//绘制矩形
/*Point leftUp = Point(imageX/2.0-10,imageX/2.0-10);
Point rightDown = Point(imageX/2.0+10, imageX/2.0+10);
MyRectangle(lineImage, leftUp, rightDown);*/
//绘制圆
/*Point center = Point(imageX/2.0, imageX/2.0);
double radius;
cout << "Input radius of circle:";
cin >> radius;
MyCircle(lineImage, center, radius);*/
//绘制多边形
MyPolygon(lineImage, imageX);
namedWindow("LineImage");
imshow("LineImage", lineImage);
waitKey(0);
return(0);
}