1. 目标
在本文中将会完成以下目标:
- 使用 OpenCV 函数line()画线
- 使用 OpenCV 函数ellipse() 绘制椭圆
- 使用 OpenCV 函数rectangle()绘制矩形
- 使用 OpenCV 函数circle()画圆
- 使用 OpenCV 函数fillPoly()绘制填充多边形
2. 理论
在本文中,将介经常使用的两种数据结构:cv::Point和cv::Scalar:
2.1 Point
它表示一个二维点,表示图像坐标是x和y. 可以将其定义为:
Point pt;
pt.x = 10;
pt.y = 8;
或者
Point pt = Point(10, 8);
2.2 Scalar
-
表示一个 4 元素向量(BGRA A是透明通道)。Scalar 类型在 OpenCV 中经常用于保存像素值。
-
在本文中,将经常使用它来表示 BGR 颜色值(3 个参数)。如果不使用最后一个参数,则无需定义它。
-
看一个例子,传入自定义参数定义一个颜色:
Scalar( a, b, c )
将定义 BGR 颜色,例如:Blue = a , Green = b和Red = c
3. 代码
- 此代码位于 OpenCV 示例文件夹中。当然也可以从这里获取
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#define w 400
using namespace cv;
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );
int main( void ){
char atom_window[] = "Drawing 1: Atom";
char rook_window[] = "Drawing 2: Rook";
Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 );
MyFilledCircle( atom_image, Point( w/2, w/2) );
MyPolygon( rook_image );
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
FILLED,
LINE_8 );
MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
imshow( atom_window, atom_image );
moveWindow( atom_window, 0, 200 );
imshow( rook_window, rook_image );
moveWindow( rook_window, w, 200 );
waitKey( 0 );
return(0);
}
void MyEllipse( Mat img, double angle )
{
int thickness = 2;
int lineType = 8;
ellipse( img,
Point( w/2, w/2 ),
Size( w/4, w/16 ),
angle,
0,
360,
Scalar( 255, 0, 0 ),
thickness,
lineType );
}
void MyFilledCircle( Mat img, Point center )
{
circle( img,
center,
w/32,
Scalar( 0, 0, 255 ),
FILLED,
LINE_8 );
}
void MyPolygon( Mat img )
{
int lineType = LINE_8;
Point rook_points[1][20];
rook_points[0][0] = Point( w/4, 7*w/8 );
rook_points[0][1] = Point( 3*w/4, 7*w/8 );
rook_points[0][2] = Point( 3*w/4, 13*w/16 );
rook_points[0][3] = Point( 11*w/16, 13*w/16 );
rook_points[0][4] = Point( 19*w/32, 3*w/8 );
rook_points[0][5] = Point( 3*w/4, 3*w/8 );
rook_points[0][6] = Point( 3*w/4, w/8 );
rook_points[0][7] = Point( 26*w/40, w/8 );
rook_points[0][8] = Point( 26*w/40, w/4 );
rook_points[0][9] = Point( 22*w/40, w/4 );
rook_points[0][10] = Point( 22*w/40, w/8 );
rook_points[0][11] = Point( 18*w/40, w/8 );
rook_points[0][12] = Point( 18*w/40, w/4 );
rook_points[0][13] = Point( 14*w/40, w/4 );
rook_points[0][14] = Point( 14*w/40, w/8 );
rook_points[0][15] = Point( w/4, w/8 );
rook_points[0][16] = Point( w/4, 3*w/8 );
rook_points[0][17] = Point( 13*w/32, 3*w/8 );
rook_points[0][18] = Point( 5*w/16, 13*w/16 );
rook_points[0][19] = Point( w/4, 13*w/16 );
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
fillPoly( img,
ppt,
npt,
1,
Scalar( 255, 255, 255 ),
lineType );
}
void MyLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = LINE_8;
line( img,
start,
end,
Scalar( 0, 0, 0 ),
thickness,
lineType );
}
4. 代码解释
由于我们计划绘制两个示例(一个atom和一个 rook),所以必须创建两个图像和两个窗口来显示它们。
char atom_window[] = "Drawing 1: Atom";
char rook_window[] = "Drawing 2: Rook";
Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
创建了绘制不同几何形状的函数。例如,为了绘制atom,使用 MyEllipse 和MyFilledCircle:
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 );
MyFilledCircle( atom_image, Point( w/2, w/2) );
为了绘制rook,使用了MyLine、rectangle和MyPolygon:
MyPolygon( rook_image );
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
FILLED,
LINE_8 );
MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
下边看一下每个函数的内部实现:
4.1 MyLine
void MyLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = LINE_8;
line( img,
start,
end,
Scalar( 0, 0, 0 ),
thickness,
lineType );
}
-
MyLine只是调用函数line() ,它执行以下操作:
- 从 start Point 到 end Point画一条线条
- 该线条在图像img中显示
- 线条颜色由 (0, 0, 0) 定义,它是对应于黑色的 RGB 值
- 线条粗细设置为粗(在本例中为 2)
- 该线是 8-connected(lineType = 8)
4.2 MyEllipse
void MyEllipse( Mat img, double angle )
{
int thickness = 2;
int lineType = 8;
ellipse( img,
Point( w/2, w/2 ),
Size( w/4, w/16 ),
angle,
0,
360,
Scalar( 255, 0, 0 ),
thickness,
lineType );
}
-
从上面的代码中,可以看到函数ellipse() 绘制了一个椭圆,参数说明:
- 椭圆在图像img中显示
- 椭圆中心位于点 (w/2, w/2) 并被包围在一个大小为 **(w/4, w/16)**的盒子中
- 椭圆是旋转的角度度数
- 椭圆延伸0到360度之间的弧
- 图形的颜色为 ( 255, 0, 0 ) ,表示 BGR 值中的蓝色。
- 椭圆的粗细为 2。
4.3 MyFilledCircle
void MyFilledCircle( Mat img, Point center )
{
circle( img,
center,
w/32,
Scalar( 0, 0, 255 ),
FILLED,
LINE_8 );
}
-
与 ellipse 函数类似,circle参数如下:
- 显示圆的图像 ( img )
- 圆的中心表示为点中心
- 圆的半径:w/32
- 圆圈的颜色: ( 0, 0, 255 ) 在 BGR中表示红色
- 由于粗细= -1,圆形将被填充。
4.4 MyPolygon
void MyPolygon( Mat img )
{
int lineType = LINE_8;
Point rook_points[1][20];
rook_points[0][0] = Point( w/4, 7*w/8 );
rook_points[0][1] = Point( 3*w/4, 7*w/8 );
rook_points[0][2] = Point( 3*w/4, 13*w/16 );
rook_points[0][3] = Point( 11*w/16, 13*w/16 );
rook_points[0][4] = Point( 19*w/32, 3*w/8 );
rook_points[0][5] = Point( 3*w/4, 3*w/8 );
rook_points[0][6] = Point( 3*w/4, w/8 );
rook_points[0][7] = Point( 26*w/40, w/8 );
rook_points[0][8] = Point( 26*w/40, w/4 );
rook_points[0][9] = Point( 22*w/40, w/4 );
rook_points[0][10] = Point( 22*w/40, w/8 );
rook_points[0][11] = Point( 18*w/40, w/8 );
rook_points[0][12] = Point( 18*w/40, w/4 );
rook_points[0][13] = Point( 14*w/40, w/4 );
rook_points[0][14] = Point( 14*w/40, w/8 );
rook_points[0][15] = Point( w/4, w/8 );
rook_points[0][16] = Point( w/4, 3*w/8 );
rook_points[0][17] = Point( 13*w/32, 3*w/8 );
rook_points[0][18] = Point( 5*w/16, 13*w/16 );
rook_points[0][19] = Point( w/4, 13*w/16 );
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
fillPoly( img,
ppt,
npt,
1,
Scalar( 255, 255, 255 ),
lineType );
}
-
绘制填充多边形,使用函数fillPoly() 。参数如下:
- 多边形将在img上绘制
- 多边形的顶点是ppt中的点集定义
- 多边形的颜色由 (255, 255, 255) 定义,这是白色的BGR值
4.5 rectangle
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
FILLED,
LINE_8 );
-
cv::rectangle函数。参数如下:
- 矩形将在rook_image上绘制
- 矩形的两个相对顶点由
(0, 7*w/8)和 (w, w)定义 - 矩形的颜色由 (0, 255, 255) 给出,它是黄色的 BGR 值
- 粗细值由FILLED (-1) 给出,矩形将被填充。
5.结果
编译和运行你的程序应该会给你这样的结果: