OpenCV学习笔记 core库3.应用:绘制基本图形

学习内容

#define WINDOW_WIDHT VALUE //将VALUE改为想要的正整数值

Ellipse 椭圆

void DrawEllipse(Mat img, double angle)
{
	int thickness = 2;
	int lineType = 8;
	//调用OpenCV提供的ellipse()函数进行绘制
	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								//线型
	);
}

FilledCircle 实心圆

void DrawFilledCircle(Mat img, Point centre)
{
	int thickness = -1;		//线粗为-1,即为实心
	int lineType = 8;
	//调用OpenCV提供的circle()函数进行绘制
	circle( img,
		centre,				//圆心
		WINDOW_WIDTH/32,	//半径
		Scalar(0, 0, 255),	
		thickness,
		lineType			
	);
}

Polygon 多边形

void DrawPolygon(Mat img )
{
	int lineType = 8;
	Point rookPoints[1][20];	//顶点集
	//赋值过程省略
	const Point* ppt[1]={ rookPoints[0]};
	int npt[] = {20};
	
	fillPoly( img,
		ppt,		//多边形顶点集	
		npt,		//多边形顶点数目
		1,			//多边形数量
		Scalar(255,255,255),
		lineType);
}

Line 线

void DrawLine(Mat img, Point start, Point end )
{
	int thickness = 2;
	int lineType = 8;
	//调用OpenCV提供的line()函数进行绘制
	line( img, 
		start,				//起点
		end,				//终点
		Scalar(0, 0, 0),
		thickness,
		lineType
	);
}

实例

用之前所学的知识实现了个简单的基本图形绘图板…

#include <cstdlib>
#include <cmath>
#include "opencv2/opencv.hpp"

#define WINDOW_WIDTH 600
#define WIN_NAME "DRAW_TEST"
#define BAR_NAME "BAR_TEST"
#define SX ( sx + ex )
#define SY ( sy + ey)
#define DX (abs(ex-sx))
#define DY (abs(ey-sy))

using namespace cv;

void DrawEllipse(Mat img, double angle);
void DrawFilledCircle(Mat img, Point centre);
void DrawCircle(Mat img, Point centre);
void DrawPolygon(Mat img );
void DrawLine(Mat img, Point start, Point end );
void on_Change(int, void*);
void on_Mouse(int event, int x, int y, int flags, void* param);

char dShape[5][20]={"Ellipse","FilledCircle","Circle","Polygon_Undefined","Line"};
//enum dShape{Ellipse, FilledCircle, Circle, Polygon, Line}; 
int track_v,sx,sy,ex,ey;
bool drawing;
Mat tmpimg(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3, Scalar(0,0,0) );
Mat tmpshow = tmpimg.clone();

int main()
{
    namedWindow(WIN_NAME);
    imshow(WIN_NAME, tmpshow);
    createTrackbar(BAR_NAME, WIN_NAME, &track_v, 4, on_Change);
    setMouseCallback(WIN_NAME, on_Mouse);
    waitKey(0);
}
void on_Change(int, void*)
{
    system("CLS");
    printf("Draw Mode: %s\n",dShape[track_v]);
}
void on_Mouse(int event, int x, int y, int flags, void* param)
{
    switch(event)
    {
        case EVENT_LBUTTONDOWN:
        {
            drawing = 1;
            sx=x;sy=y;
            printf("sx = %d , sy = %d\n",sx,sy);
            break;
        }
        case EVENT_LBUTTONUP:
        {
            drawing = 0;
            printf("ex = %d , ey = %d\n",ex,ey);
            tmpimg = tmpshow.clone();
            break;
        }
        case EVENT_MOUSEMOVE:
        {
            if(drawing)
            {
                ex=x;ey=y;
                tmpshow = tmpimg.clone();
                switch(track_v)
                {
                    case 0:
                        DrawEllipse(tmpshow, 0);break;
                    case 1:
                        DrawFilledCircle(tmpshow, Point(SX/2, SY/2) );break;
                    case 2:
                        DrawCircle(tmpshow,Point(SX/2, SY/2 ) );break;
                    case 3:
                        break;
                    case 4:
                        DrawLine(tmpshow, Point(sx, sy), Point(ex, ey) );

                }
            }
        }
    }
    imshow(WIN_NAME,tmpshow);
}
void DrawEllipse(Mat img, double angle)
{
	int thickness = 2;
	int lineType = 8;
	//调用OpenCV提供的ellipse()函数进行绘制
	ellipse( img,								//画到的目标图像
		Point(SX/2, SY/2 ),	                    //中心点
		Size(DX, DY ),	                        //大小
		angle,									//旋转角度
		0,										//扩展的起始弧度
		360,									//扩展的末端弧度
		Scalar(255, 129, 0),					//颜色			
		thickness,								//线宽
		lineType								//线型
	);
}
void DrawFilledCircle(Mat img, Point centre)
{
	int thickness = -1;		//线粗为-1,即为实心
	int lineType = 8;
	//调用OpenCV提供的circle()函数进行绘制
	circle( img,
		centre,				//圆心
		max(DX/2,DY/2),     //半径
		Scalar(0, 0, 255),	
		thickness,
		lineType			
	);
}
void DrawCircle(Mat img, Point centre)
{
	int thickness = 2;		
	int lineType = 8;
	//调用OpenCV提供的circle()函数进行绘制
	circle( img,
		centre,				//圆心
		max(DX/2,DY/2),	    //半径
		Scalar(0, 255, 0),	
		thickness,
		lineType			
	);
}
/*void DrawPolygon(Mat img )
{
	int lineType = 8;
	Point rookPoints[1][20];	//顶点集
	//赋值过程省略
	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;
	//调用OpenCV提供的line()函数进行绘制
	line( img, 
		start,				//起点
		end,				//终点
		Scalar(255, 0, 0),
		thickness,
		lineType
	);
}
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页