本文主要涉及的opencv函数包括:
- 用于绘制
直线
的line函数;- 用于绘制
椭圆
的ellipse函数;- 用于绘制
矩形
的rectangle函数;- 用于绘制
圆
的circle函数;(可以作为粗的点)- 用于绘制填充的
多边形
的fillPoly函数。- 画点:直接对Mat进行元素颜色修改即可。
1. DrawEllipse函数的写法
自定义的绘制函数,实现了绘制了
不同角度、相同尺度的椭圆
。函数中DrawElipse调用了OpenCV中的ellipse函数,将椭圆画到图像img上。
void DrawEllipse(Mat img, double angle)
{
int thickness = 2;
int lineType = 8;
ellipse( img,//将椭圆画到img图像上
Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2),//椭圆中心点
Size(WINDOW_WIDTH/4, WINDOW_WIDTH/16),//长和宽,被矩形包围
angle,//椭圆旋转角度
0,//扩展弧度从0度到360度
360,
Scalar(255, 129, 0),//图形颜色为蓝色
thickness,//线宽
lineType);//线型
}
2. DrawFilledCircle()函数的写法
DrawFilledCircle()调用了OpenCV中的circle函数,将圆画到图像img上。
void DrawFilledCircle( Mat img, Point center )
{
int thickness = -1;
int lineType = 8;
circle( img,
center,//圆心由点center定义
WINDOW_WIDTH/32,//圆的半径
Scalar( 0, 0, 255 ),//圆的颜色
thickness,//线粗
lineType );
}
3. DrawPolygon()函数的写法
DrawPolygon()调用了OpenCV的fillPoly函数,将
多边形
画到图像img上。
void DrawPolygon( Mat img )
{
int lineType = 8;
//创建一些点
Point rookPoints[1][20];
rookPoints[0][0] = Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 );
rookPoints[0][1] = Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 );
rookPoints[0][2] = Point( 3*WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16 );
rookPoints[0][3] = Point( 11*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
rookPoints[0][4] = Point( 19*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8 );
rookPoints[0][5] = Point( 3*WINDOW_WIDTH/4, 3*WINDOW_WIDTH/8 );
rookPoints[0][6] = Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH/8 );
rookPoints[0][7] = Point( 26*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][8] = Point( 26*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][9] = Point( 22*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][10] = Point( 22*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][11] = Point( 18*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][12] = Point( 18*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][13] = Point( 14*WINDOW_WIDTH/40, WINDOW_WIDTH/4 );
rookPoints[0][14] = Point( 14*WINDOW_WIDTH/40, WINDOW_WIDTH/8 );
rookPoints[0][15] = Point( WINDOW_WIDTH/4, WINDOW_WIDTH/8 );
rookPoints[0][16] = Point( WINDOW_WIDTH/4, 3*WINDOW_WIDTH/8 );
rookPoints[0][17] = Point( 13*WINDOW_WIDTH/32, 3*WINDOW_WIDTH/8 );
rookPoints[0][18] = Point( 5*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
rookPoints[0][19] = Point( WINDOW_WIDTH/4, 13*WINDOW_WIDTH/16 );
const Point* ppt[1] = { rookPoints[0] };
int npt[] = { 20 };
fillPoly( img,
ppt, //多边形的顶点集
npt, //多边形定点数目
1, //多边形数量
Scalar( 255, 255, 255 ), //多边形的颜色
lineType );
}
4. DrawLine()函数的写法
DrawLine()调用了OpenCV中的line函数,用于在图像img上画一条直线段。
void DrawLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = 8;
line( img,
start, //线段始点
end, //线段终点
Scalar( 0, 0, 0 ), //黑色线条
thickness, //线粗
lineType );
}
5. 绘制矩形函数:rectangle
rectangle( rookImage,
Point( 0, 7*WINDOW_WIDTH/8 ),//左上角
Point( WINDOW_WIDTH, WINDOW_WIDTH),//右下角
Scalar( 0, 255, 255 ),
-1,
8 );
6. 整体代码示例
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
#include <opencv2/imgproc/imgproc.hpp>
//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------
#define WINDOW_NAME1 "【绘制图1】" //为窗口标题定义的宏
#define WINDOW_NAME2 "【绘制图2】" //为窗口标题定义的宏
#define WINDOW_WIDTH 600//定义窗口大小的宏
//--------------------------------【全局函数声明部分】-------------------------------------
// 描述:全局函数声明
//-----------------------------------------------------------------------------------------------
void DrawEllipse( Mat img, double angle );//绘制椭圆
void DrawFilledCircle( Mat img, Point center );//绘制圆
void DrawPolygon( Mat img );//绘制多边形
void DrawLine( Mat img, Point start, Point end );//绘制线段
//---------------------------------------【main( )函数】--------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main( void )
{
// 创建空白的Mat图像
Mat atomImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );
Mat rookImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );
// ---------------------<1>绘制化学中的原子示例图------------------------
//【1.1】先绘制出椭圆
DrawEllipse( atomImage, 90 );
DrawEllipse( atomImage, 0 );
DrawEllipse( atomImage, 45 );
DrawEllipse( atomImage, -45 );
//【1.2】再绘制圆心
DrawFilledCircle( atomImage, Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2) );
// ----------------------------<2>绘制组合图-----------------------------
//【2.1】先绘制出多边形
DrawPolygon( rookImage );
// 【2.2】绘制矩形
rectangle( rookImage,
Point( 0, 7*WINDOW_WIDTH/8 ),//左上角
Point( WINDOW_WIDTH, WINDOW_WIDTH),//右下角
Scalar( 0, 255, 255 ),
-1,
8 );
// 【2.3】绘制一些线段
DrawLine( rookImage, Point( 0, 15*WINDOW_WIDTH/16 ), Point( WINDOW_WIDTH, 15*WINDOW_WIDTH/16 ) );
DrawLine( rookImage, Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/4, WINDOW_WIDTH ) );
DrawLine( rookImage, Point( WINDOW_WIDTH/2, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/2, WINDOW_WIDTH ) );
DrawLine( rookImage, Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH ) );
// ---------------------------<3>显示绘制出的图像------------------------
imshow( WINDOW_NAME1, atomImage );
moveWindow( WINDOW_NAME1, 0, 200 );//窗口显示的位置,让两幅图像对齐
imshow( WINDOW_NAME2, rookImage );
moveWindow( WINDOW_NAME2, WINDOW_WIDTH, 200 );
waitKey( 0 );
return(0);
}
输出如下: