zoukankan      html  css  js  c++  java
  • opencv —— line、ellipse、rectangle、circle、fillPoly、putText 基本图形的绘制

    绘制线段:line 函数

    void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0);
    • img: 要绘制线段的图像。
    • pt1: 线段的起点。
    • pt2: 线段的终点。
    • color: 线段的颜色,通过一个 Scalar 对象定义。
    • thickness: 线条的宽度,默认值为 1。
    • lineType: 线段的类型。可以取值 8,4,和 LINE_AA, 分别代表8邻接连接线,4 邻接连接线和反锯齿连接线。默认值为 8 邻接。为了获得更好地效果可以选用 LINE_AA(采用了高斯滤波)。
    • shift: 坐标点小数点位数,默认值 0。
    // 绘制直线
    void DrawLine(Mat img, Point start, Point end){
        int thickness = 2;
        int lineType = 8;
        line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
    }

     

    绘制椭圆:ellipse 函数

    void ellipse(Mat& img, Point center, Size axes,double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1,int lineType=8, int shift=0);
    • img: 要绘制椭圆的图像。
    • center: 椭圆中心点坐标。
    • axes: 椭圆位于该 Size 决定的矩形内。(即定义长轴和短轴)。
    • angle: 椭圆的角度。
    • startAngle: 椭圆开始绘制时角度,顺时针旋转。
    • endAngle: 椭圆绘制结束时角度。(若绘制一个完整的椭圆,则startAngle=0, endAngle = 360)。
    • color: 椭圆的颜色。
    • thickness: 绘制椭圆线粗。负数表示全部填充。
    • lineType、shift:同上。
    void DrawEllipse(Mat img,Point center,Size rect_size, double angle) {
        int thickness = 2;
        int lineType = 8;
        ellipse(img, center, rect_size, angle, 0, 360, Scalar(255, 129, 0), thickness, lineType);
        imshow("img", img);
    }

    绘制矩形:rectangle 函数

    void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0);
    • pt1: 矩形的左上角坐标。
    • pt2: 矩阵的右下角坐标。
    • 其余同上。
    //绘制矩形
    rectangle(src, Point(0, 0),Point(width, width), Scalar(0, 255, 255), -1, 8);

     

    绘制圆:circle 函数

    void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0);
    • center: 圆心坐标。
    • radius: 半径。
    • 其余同上。
    //绘制实心圆
    void DrawFilledCircle(Mat img, Point center, int radius){
        int thickness = -1; //线粗
         int lineType = 8;
        circle(img, center, radius, Scalar(0, 0, 255),thickness, lineType); 
    }

     

    填充多边形:fillPoly 函数

    void fillPoly(Mat& img, const Point** pts,const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() );
    • pts: 多边形定点集。
    • npts: 多边形的顶点数目。
    • ncontours: 要绘制多边形的数量。
    • offset: 所有点轮廓的可选偏移。
    • 其余同上。
    //实现凹多边形绘制
    void DrawPolygon(Mat img){
        int lineType = 8;
    
        //创建一些点
         Point rootPoints[1][20];
        rootPoints[0][0] = Point(wwidth / 4, 7 * wwidth / 8);
        rootPoints[0][1] = Point(3 * wwidth / 4, 7 * wwidth / 8);
        rootPoints[0][2] = Point(3 * wwidth / 4, 13 * wwidth / 16);
        rootPoints[0][3] = Point(11 * wwidth / 16, 13 * wwidth / 16);
        rootPoints[0][4] = Point(19 * wwidth / 32, 3 * wwidth / 8);
        rootPoints[0][5] = Point(3 * wwidth / 4, 3 * wwidth / 8);
        rootPoints[0][6] = Point(3 * wwidth / 4, wwidth / 8);
        rootPoints[0][7] = Point(26 * wwidth / 40, wwidth / 8);
        rootPoints[0][8] = Point(26 * wwidth / 40, wwidth / 4);
        rootPoints[0][9] = Point(22 * wwidth / 40, wwidth / 4);
        rootPoints[0][10] = Point(22 * wwidth / 40, wwidth / 8);
        rootPoints[0][11] = Point(18 * wwidth / 40, wwidth / 8);
        rootPoints[0][12] = Point(18 * wwidth / 40, wwidth / 4);
        rootPoints[0][13] = Point(14 * wwidth / 40, wwidth / 4);
        rootPoints[0][14] = Point(14 * wwidth / 40, wwidth / 8);
        rootPoints[0][15] = Point(wwidth / 4, wwidth / 8);
        rootPoints[0][16] = Point(wwidth / 4, 3 * wwidth / 8);
        rootPoints[0][17] = Point(13 * wwidth / 32, 3 * wwidth / 8);
        rootPoints[0][18] = Point(5 * wwidth / 16, 13 * wwidth / 16);
        rootPoints[0][19] = Point(wwidth / 4, 13 * wwidth / 16);
        const Point* all_pts[1] = { rootPoints[0] };
        int pt_num[] = { 20 };
        fillPoly(img, all_pts, pt_num, 1, Scalar(255, 255, 255), lineType);
    }

     

    显示文字:putText 函数

    void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
    • img:显示文字所在图像.
    • text:待显示的文字.
    • org:文字在图像中的左下角坐标.
    • fontFace:字体类型,可选择字体:                                                                                     

    FONT_HERSHEY_SIMPLEX,FONT_HERSHEY_PLAIN,FONT_HERSHEY_DUPLEX

    FONT_HERSHEY_COMPLEX,FONT_HERSHEY_TRIPLEX,FONT_HERSHEY_COMPLEX_SMALL,

    FONT_HERSHEY_SCRIPT_SIMPLEX,FONT_HERSHEY_SCRIPT_COMPLEX

    以上所有类型都可以配合FONT_HERSHEY_ITALIC使用,产生斜体效果。

    • fontScale:字体大小,该值和字体内置大小相乘得到字体大小
    • color:文本颜色
    • thickness:写字的线的粗细
    • lineType:线型.
    • bottomLeftOrigin:false, 图像数据原点在左下角。否则, 图像数据原点在左上角.
     putText(Image, "OpenCv", Point(width / 2, width / 2), FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 1, 8, false);

     

    借鉴博客:https://www.cnblogs.com/ishero/p/11136315.html

  • 相关阅读:
    DFS,BFS算法
    浙江理工大学7月月赛
    矩阵快速幂
    数塔
    Bone Collector
    畅通工程
    敌兵布阵
    Tempter of the Bone
    Elevator
    Fibonacci Again
  • 原文地址:https://www.cnblogs.com/bjxqmy/p/12225743.html
Copyright © 2011-2022 走看看