[C++ OpenCV] Draw geometric figures and text on images

Table of contents

1. Draw a straight line

2. Draw a circle

3. Draw an ellipse

The first overloading method

The second overloading method

4. Draw a square

5. Draw polygons

6. Text generation

7. Practical operation


1. Draw a straight line

void cv::line   (   InputOutputArray    img,
                    Point   pt1,
                    Point   pt2,
                    const Scalar &      color,
                    int     thickness = 1,
                    int     lineType = LINE_8,
                    int     shift = 0 
                )   
  • img: The image to draw the line segment.

  • pt1: The starting point of the line segment.

  • pt2: The end point of the line segment.

  • color: The color of the line segment, defined through a Scalar object.

  • thickness: The width of the line.

  • lineType: The type of line segment. The possible values ​​are 8, 4, and CV_AA, which represent 8-adjacent connection lines, 4-adjacency connection lines, and anti-aliased connection lines respectively. The default value is 8 adjacencies. In order to obtain better results, you can choose CV_AA (using Gaussian filtering).

  • shift: the number of decimal places for coordinate points.

The effect of setting the line width to 1 and the line width to 2 is as follows:

 

2. Draw a circle

void cv::circle     (   InputOutputArray    img,
                        Point   center,
                        int     radius,
                        const Scalar &      color,
                        int     thickness = 1,
                        int     lineType = LINE_8,
                        int     shift = 0 
                    )              

cv.circle(  img, center, radius, color[, thickness[, lineType[, shift]]]    ) ->    img     //python
  • img input and output image after drawing the shape

  • center circle center coordinates

  • radius radius of circle

  • color color of line

  • If thickness is a positive number, it represents the thickness of the lines that make up the circle. A negative number such as -1 indicates whether the circle is filled.

  • line_type The type of line, the default is 8.

  • shift The number of decimal places for the circle center coordinate point and radius value

  • The effects of solid circles and hollow circles are as follows:

 

3. Draw an ellipse

The first overloading method

void cv::ellipse(       InputOutputArray    img,
                        Point               center,
                        Size                axes,
                        double              angle,
                        double              startAngle,
                        double              endAngle,
                        const Scalar &      color,
                        int                 thickness = 1,
                        int                 lineType = LINE_8,
                        int                 shift = 0 
                )   

Python:
    cv.ellipse( img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]] ) ->    img
    cv.ellipse( img, box, color[, thickness[, lineType]]    ) ->    img
  • Size: the major axis and minor axis of the ellipse, such as Size(100,50) represents the major axis 100 and the minor axis 50

  • angle: rotation angle

  • startAngle and endAngle represent the starting and ending angles respectively

The picture below is a comparison between drawing 0~100 degrees and drawing a complete ellipse.

 

In the OpenCV documentation, in order to illustrate the specific definition of angular direction, definition of axial direction, etc., the following schematic diagram is also given:

对于第一种情况,椭圆的axes.width > axes.height;、
注意对照示意图,axes.width表示的是椭圆的主轴,即FIRST AXIS,axes.height表示的是椭圆的副轴,即SECOND AXIS,主轴的方向沿图像坐标轴X轴顺时针偏移的方向即为angle;
以主轴axes.width为半径虚拟一个圆,以主轴为基准,顺时针方向偏移start angle角度,得到起始弧段点,由这个点向椭圆主轴做投影,在椭圆弧上的投影点作为椭圆弧段的真正起始点;椭圆弧段的真正结束点也是采取相同的方式获得;
沿顺时针方向,将椭圆弧段的起始点和结束点之间的椭圆弧段绘制出来,即为图中的深蓝色弧,为该模式下绘制出的椭圆弧段;
注意在这种绘制模式下,axes.width和awes.height均表示的是椭圆的半轴长度,这和下一种模式会有区别;
对于第二种情况,即椭圆的axes.width < axes.height时,仍将axes.width所在的轴作为主轴,和第一种情况类似,也仍是以axes.width作为半径虚拟一个圆,各种偏移角、起始角、结束角均与第一种情况类似。

The second overloading method

void cv::ellipse    (   InputOutputArray    img,
                        const RotatedRect &     box,
                        const Scalar &      color,
                        int     thickness = 1,
                        int     lineType = LINE_8 
                    )       
Python:
    cv.ellipse( img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]] ) ->    img
    cv.ellipse( img, box, color[, thickness[, lineType]]    ) ->    img

This method combines all the shape parameters of the ellipse into a RotatedRect object, that is, box. RotatedRect has three attributes: angle center size . It is easy to understand this data structure: center represents the center point of the rectangle; angle represents the angle of clockwise deflection of the rectangle; size represents the width and height of the rectangle respectively. Note that in RotatedRect, size.width also exists as the main axis. like:

RotateREct(Point2f(100.0,200.0),size(100,50),Scalar(255,255,255))

4. Draw a square

//方法一
void cv::rectangle  (   InputOutputArray    img,
                        Point   pt1,
                        Point   pt2,
                        const Scalar &      color,
                        int     thickness = 1,
                        int     lineType = LINE_8,
                        int     shift = 0 
                    )       
Python:
    cv.rectangle(   img, pt1, pt2, color[, thickness[, lineType[, shift]]]  ) ->    img
    cv.rectangle(   img, rec, color[, thickness[, lineType[, shift]]]   ) ->    img

//方法二
void cv::rectangle  (   InputOutputArray    img,
                        Rect    rec,
                        const Scalar &      color,
                        int     thickness = 1,
                        int     lineType = LINE_8,
                        int     shift = 0 
                    )       
Python:
    cv.rectangle(   img, pt1, pt2, color[, thickness[, lineType[, shift]]]  ) ->    img
    cv.rectangle(   img, rec, color[, thickness[, lineType[, shift]]]   ) ->    img
  • pt1 The vertices of the rectangle.

  • pt2 The vertices of the rectangle opposite pt1. It means that pt1 and pt2 are diagonal vertices

  • rec is a set of two vertices, such as: Rect(100, 100, 150, 200)

  •  

5. Draw polygons

void cv::fillPoly   (   InputOutputArray    img,
                    const Point **      pts,
                    const int *     npts,
                    int     ncontours,
                    const Scalar &      color,
                    int     lineType = LINE_8,
                    int     shift = 0,
                    Point   offset = Point() 
                )       
Python:
    cv.fillPoly(    img, pts, color[, lineType[, shift[, offset]]]  ) ->    img
  • pts: Polygon vertex array, which can store arrays of multiple polygon vertex coordinates. Note that its data type is const Point **, and the address of the array is stored in the array.

  • npts: the number of vertices of each polygon

  • ncontours: The number of polygons to draw. The above three parameters are required to be in one-to-one correspondence.

  • offset: offset of all vertices

The results are drawn as follows:

 

Point root_points[1][6];
    root_points[0][0] = Point(215, 220);
    root_points[0][1] = Point(460, 225);
    root_points[0][2] = Point(466, 450);
    root_points[0][3] = Point(235, 465);
    root_points[0][4] = Point(260, 390);
    root_points[0][5] = Point(96, 310);
    const Point* pp[] = { root_points[0]};
    int nps[] = { 6 };
    fillPoly(img, pp, nps,1, Scalar(255, 255, 255), 8);

6. Text generation

void cv::putText    (   InputOutputArray    img,
                        const String &      text,
                        Point   org,
                        int     fontFace,
                        double      fontScale,
                        Scalar      color,
                        int     thickness = 1,
                        int     lineType = LINE_8,
                        bool    bottomLeftOrigin = false 
                    )       
Python:
    cv.putText( img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]] ) ->    img
  • Point org: The coordinate of the lower left corner of the first character

  • int fontFace: font type, FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, etc.

  • double fontScale, font size

  • Scalar color, font color, color is represented by Scalar().

  • lineType, line type, we use the default value 8.

  • bottomLeftOrigin: The position of the origin of the image data. The default is the upper left corner. If the parameter is changed to true, the origin is the lower left corner.

7. Practical operation

#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
using namespace std;
using namespace cv;
int main()
{
    Mat img = Mat::zeros(512, 512, CV_8UC3);
    Point p1(10, 100);
    Point p2(50,50);
    //绘制直线
    line(img, p1, p2, Scalar(255, 255, 255), 2, LINE_AA,0);
    circle(img, Point(100, 100), 50, Scalar(255, 255, 255), 1, LINE_AA, 0);
    //绘制实心圆
    circle(img, Point(210, 100), 50, Scalar(255, 255, 255), -1, LINE_AA, 0);
    //绘制椭圆
    ellipse(img, Point(250, 250), Size(70, 50), 0, 0, 100, Scalar(255, 255, 255), 1, LINE_4, 0);
    ellipse(img, Point(400, 250), Size(70, 50), 0, 0, 360, Scalar(255, 255, 255), 1, LINE_4, 0);
    //绘制方形
    rectangle(img, Point(100, 300), Point(200, 500), Scalar(255, 255, 255));
    rectangle(img, Rect(100, 100, 150, 200), Scalar(255, 255, 255));
    //绘制多边形
    Point root_points[1][6];
    root_points[0][0] = Point(215, 220);
    root_points[0][1] = Point(460, 225);
    root_points[0][2] = Point(466, 450);
    root_points[0][3] = Point(235, 465);
    root_points[0][4] = Point(260, 390);
    root_points[0][5] = Point(96, 310);
    const Point* pp[] = { root_points[0]};
    int nps[] = { 6 };
    fillPoly(img, pp, nps,1, Scalar(20, 215, 135), 8);
    //生成文字
    putText(img, "dagengen", Point(250, 250), 2, 1, Scalar(155, 155, 155));
        waitKey(1);
        return 0;
}

The generated result graph is as follows:

 

Guess you like

Origin blog.csdn.net/dagengen12138/article/details/131293820