您的位置:首页 > 运维架构

OpenCV - 基本图形函数的使用

2015-10-24 20:49 369 查看
在OpenCV的图像绘制中,我们会用到各种绘制函数,基本的函数包括绘制直线的line函数,绘制椭圆的ellipse函数,绘制矩形的rectangle函数,绘制圆的circle函数以及用于绘制填充的多边形的fillPoly函数。下面可以通过下面用函数绘制的两个组个图的一段代码来详细了解DrawEllipse() 函数、DrawFilledCircle() 函数、DrawPolygon()
函数以及DrawLine() 函数的用法。

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;

#define WinDow_1 "【绘制组合图1】"
#define WinDow_2 "【绘制组合图2】"
#define WinDow_width 600

void DrawLine(Mat img, Point start, Point end);//绘制线段函数
void DrawFilledCircle(Mat img, Point center);//绘制实心圆函数
void DrawEllipse(Mat img, double angle);//绘制椭圆函数
void DrawPolygon(Mat img);//绘制多边形函数

int main()
{

// 初始化Mat图像
Mat atomImage = Mat::zeros(WinDow_width, WinDow_width, CV_8UC3);
Mat rookImage = Mat::zeros(WinDow_width, WinDow_width, CV_8UC3);

//制出4个分别旋转0、45、90、-45度的椭圆
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);

//绘制圆形
DrawFilledCircle(atomImage, Point(WinDow_width / 2, WinDow_width / 2));

imshow(WinDow_1, atomImage);//显示出化学原子图像
moveWindow(WinDow_1, 0, 200);

//绘制出多边形
DrawPolygon(rookImage);

//绘制矩形
rectangle(rookImage,
Point(0, 7 * WinDow_width / 8),
Point(WinDow_width, WinDow_width),
Scalar(0, 255, 255),
-1,
8);

//绘制线段
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));

imshow(WinDow_2, rookImage);//显示出组合图形
moveWindow(WinDow_2, WinDow_width, 200);

waitKey(0);
return(0);
}

void DrawEllipse(Mat img, double angle)
{
int thickness = 2;//定义线宽
int lineType = 8;//定义线型

ellipse(img,
Point(WinDow_width / 2, WinDow_width / 2),//椭圆中心坐标
Size(WinDow_width / 4, WinDow_width / 16),//椭圆尺寸
angle,//旋转角度
0,
360,
Scalar(255, 129, 0),//用Scalar函数表示颜色-蓝色
thickness,
lineType);
}

void DrawFilledCircle(Mat img, Point center)
{
int thickness = -1;
int lineType = 8;

circle(img,
center,//圆心坐标
WinDow_width / 32,//圆的半径
Scalar(0, 0, 255),//红色
thickness,
lineType);
}

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);
}

void DrawLine(Mat img, Point start, Point end)
{
int thickness = 2;
int lineType = 8;
line(img,
start,//起点坐标
end,//终点坐标
Scalar(0, 0, 0),//黑色
thickness,
lineType);
}


用上述函数所绘制的组合图形如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: