(5)OpenCV笔记——绘制基本图形

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


#define WINDOW_NAME1 "【绘制图1】"        //为窗口标题定义的宏 

#define WINDOW_WIDTH 600//定义窗口大小的宏


//-----------------------------------【DrawLine( )函数】--------------------------
//      描述:自定义的绘制函数,实现了线的绘制
//---------------------------------------------------------------------------------

void DrawLine(Mat img, Point start, Point end)
{
    int thickness = 2;
    int lineType = 8;
    line(img,
        start,
        end,
        Scalar(0, 0, 255),
        thickness,
        lineType);
}

//-----------------------------------【DrawFilledCircle( )函数】---------------------------
//      描述:自定义的绘制函数,实现了实心圆的绘制
//-----------------------------------------------------------------------------------------
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 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);
}



int main(void)
{
    // 创建空白的Mat图像
    Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

    //【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));


    //【1.3】绘制矩形
    rectangle(atomImage,
        Point(50, 50),    //左上角点坐标
        Point(100, 100),   //右下角点坐标
        Scalar(0, 255, 255),
        -1,
        8);

    DrawLine(atomImage, Point(40,40), Point(120,120));

    imshow(WINDOW_NAME1, atomImage);
    waitKey(0);
    return(0);
}

结果:

原文链接:加载失败,请重新获取