欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

OpenCV 基本图形绘制函数详解

程序员文章站 2022-04-12 11:59:52
用于绘制直线的line函数; 用于绘制椭圆的ellipse函数; 用于绘制矩形的rectangle函数; 用于绘制圆的circle函数; 用于绘制填充的多边形的fillpoly...
  • 用于绘制直线的line函数;
  • 用于绘制椭圆的ellipse函数;
  • 用于绘制矩形的rectangle函数;
  • 用于绘制圆的circle函数;
  • 用于绘制填充的多边形的fillpoly函数。
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

#define window_width 600 // 定义窗口大小的宏
#define window_name1 "原子图"        //为窗口标题定义的宏 
#define window_name2 "多边形图以及画线"        //为窗口标题定义的宏 

/***绘制椭圆***/
void drawellipse(mat img, double angle)
{
    int thickness = 2; //  线宽
    int linetype = 8; // 线性,8 代表联通线性

    ellipse(img,
        point(window_width / 2, window_width / 2),
        size(window_width / 4, window_width / 16),
        angle, // 角度,0~360 度
        0,
        360,
        scalar(255, 129, 0),
        thickness,
        linetype);
}

/*绘制实心圆*/
void drawfilledcircle(mat img, point center)
{
    int thickness = -1; //  线粗-1,代表实心
    int linetype = 8; // 线性,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);
}


int main(int argc, char ** argv)
{
    mat atomimage = mat::zeros(window_width, window_width, cv_8uc3);
    mat rookimage = mat::zeros(window_width, window_width, cv_8uc3);

    // 椭圆绘制
    drawellipse(atomimage, 0);
    drawellipse(atomimage, 90);
    drawellipse(atomimage, 45);
    drawellipse(atomimage, -45);
    // 绘制圆心
    drawfilledcircle(atomimage, point(window_width / 2, window_width / 2));


    // 绘制多边形
    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));

    // ---------------------------<3>显示绘制出的图像------------------------
    imshow(window_name1, atomimage);
    movewindow(window_name1, 0, 200);
    imshow(window_name2, rookimage);
    movewindow(window_name2, window_width, 200);

    waitkey(0);
    return(0);
}

运行:

OpenCV 基本图形绘制函数详解

参考:

《opencv3 编程入门》 毛星云 p104

到此这篇关于opencv 基本图形绘制函数的文章就介绍到这了,更多相关opencv 基本图形函数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!