#include "stdafx.h" #include <opencv.hpp> using namespace cv; #define WINDOW_WIDTH 600 void DrawEllipse(Mat img, double angle);//绘制椭圆 void DrawCircle(Mat img,Point center); //绘制圆 void DrawPolygon(Mat img);//绘制多边形 void DrawLine(Mat img,Point start,Point end); //绘制直线 int _tmain(int argc, _TCHAR* argv[]) { //创建空白的Mat图像 Mat image = Mat::zeros(WINDOW_WIDTH,WINDOW_WIDTH,CV_8UC3); DrawEllipse(image,90); DrawEllipse(image,0); DrawEllipse(image,45); DrawEllipse(image,-45); DrawCircle(image,Point(WINDOW_WIDTH/2,WINDOW_WIDTH/2)); DrawPolygon(image); DrawLine(image,Point(0,0),Point(WINDOW_WIDTH,WINDOW_WIDTH)); imshow("img",image); 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), //椭圆颜色 thickness, //椭圆线宽 lineType //椭圆线型 ); } void DrawCircle(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 points[1][13]; points[0][0] = Point(WINDOW_WIDTH/4,7*WINDOW_WIDTH/8); points[0][1] = Point(3*WINDOW_WIDTH/4,7*WINDOW_WIDTH/8); points[0][2] = Point(3*WINDOW_WIDTH/4,13*WINDOW_WIDTH/8); points[0][3] = Point(4*WINDOW_WIDTH/4,7*WINDOW_WIDTH/8); points[0][4] = Point(5*WINDOW_WIDTH/4,6*WINDOW_WIDTH/8); points[0][5] = Point(6*WINDOW_WIDTH/4,5*WINDOW_WIDTH/8); points[0][6] = Point(7*WINDOW_WIDTH/4,4*WINDOW_WIDTH/8); points[0][7] = Point(8*WINDOW_WIDTH/4,3*WINDOW_WIDTH/8); points[0][8] = Point(9*WINDOW_WIDTH/4,2*WINDOW_WIDTH/8); points[0][9] = Point(10*WINDOW_WIDTH/4,1*WINDOW_WIDTH/8); points[0][10] = Point(11*WINDOW_WIDTH/4,3*WINDOW_WIDTH/8); points[0][11] = Point(12*WINDOW_WIDTH/4,4*WINDOW_WIDTH/8); points[0][12] = Point(13*WINDOW_WIDTH/4,5*WINDOW_WIDTH/8); const Point* ppt[1] = {points[0]}; int npt[] = {13}; fillPoly(img, //要绘制的图像 ppt, //多边形的定点集 npt, //定点数目 1, //绘制多边形数量 Scalar(255,125,25), //多边形颜色 lineType //线型 ); } void DrawLine(Mat img,Point start,Point end) { int thinkness = 2; int lineType = 8; line(img, //要绘制的图像 start, //起点 end, //终点 Scalar(25,111,222), //颜色 thinkness, //线宽 lineType //线型 ); }