Opencv-python drawing basics

Introduction to related functions

1. Point

This data structure represents the 2D points specified by their image coordinates and It can be defined as.

Point pt;

pt.x = 10;

pt.y = 8;


Point pt = Point(10, 8);

2. Scalar

represents an array with 4 elements. Subtypes are used heavily in OpenCV to pass pixel values.

In this section, we will further use it to represent RGB color values (three parameters). If the fourth parameter is not used, it need not be defined.

Let's look at an example, if the following color parameter expression is given.

Scalar( a, b, c )

Then the RGB color values defined are: Red = c, Green = b and Blue= a

3. Rectangle

C++: void rectangle(Mat& img,Point pt1, Pointpt2, const Scalar&color, intthickness=1,intlineType=8, intshift=0)

C++: void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )


img - the object to draw the rectangle pt1 - A vertex of the rectangle with the upper left corner of . pt2 - the other vertex, the lower right corner of . rec - another way of determining the rectangle, giving the top left corner coordinates and the length and width color - specifies the color or brightness of the rectangle (grayscale image), scalar(255,0,255) can specify both. thickness - The thickness of the rectangular border. A negative value (like CV_FILLED) indicates that a filled rectangle is to be drawn lineType - The border line type. (8 (or 0) - 8-connected line (8-adjacent) connection line. 4 - 4-connected line (4-adjacent) connection line. CV_AA - antialiased line. )

shift - the number of decimal places in the coordinate point

4. Line

C++: void line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)


img - image. pt1 - Starting point of the line. pt2 - end of line. color - the color of the line. thickness - the width of the line. lineType - line type Type of the line:

8 (or omitted) - 8-connected line. 4 - 4-connected line. CV_AA - antialiased line. shift - the number of decimal places in the coordinates.

5. Circle

C++: void circle(Mat&img, Point center, intradius, const Scalar&color,intthickness=1, intlineType=8, intshift=0)


img - The rectangle to draw the circle. center - The coordinates of the center of the circle. radius - radius. color - the color of the round border, of type scalar thickness - Positive values indicate the width of the rounded border. A negative value means that a filled circle is drawn lineType - rounded border line type shift - the number of decimal places of the center coordinates and radius of the circle


C++: 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)

C++: void ellipse(Mat& img, constRotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)


img - The image where the ellipse is located. center - The center of the ellipse. axes - half the length of the main axis of the ellipse angle - the angle of rotation of the ellipse startAngle - the start angle of the elliptic arc endAngle - the end angle of the elliptical arc box - Information specifying the center and rotation angle of the ellipse, via RotatedRect or CvBox2D. This means that the ellipse is drawn on a rotating rectangle (the rectangle is not visible, just a box is specified) color - The color of the oval border. thickness - positive values represent the ellipse border width, negative values represent the filled ellipse lineType - line type shift - the number of decimal places of the ellipse center coordinates and axes

8. PolyLine

C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

Parameters: img - The image where the fold line is located. pts - pointer to the point of inflection in the fold. npts - Pointer to the number of inflection points of the fold. ncontours - The number of line segments. isClosed - If or not the fold is closed. color - The color of the fold line. thickness - the width of the fold. lineType - line type. shift - the number of decimal places of the vertex coordinate.

9. 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 )

Parameters: img - displays the image where the text is located. text - the text to be displayed. org - the lower left corner of the text in the image Coordinates. font - font structure. fontFace - font type, selectable fonts: FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, orFONT_HERSHEY_SCRIPT_COMPLEX, all the above types can be used with FONT_HERSHEY_ITALIC to produce italic effect. fontScale - the font size, multiply this value with the built-in size of the font to get the font size color - text color thickness - the thickness of the line for writing, similar to a 0.38 nib and a 0.5 nib lineType - Linear. bottomLeftOrigin - true, the image data origin is in the lower left corner. Otherwise, the image data origin is in the upper left corner.

Case Code

import numpy as np
import cv2

cv2.rectangle(img,(384,0),(510,128),(0,255,0),3),(447,63), 63, (0,0,255), -1)


pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))

cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)


running result

1、Rambling about concurrent programming the Actor model
2、nginx caching policy implementation scheme
3、How important a good drive is
4、Academician Chen Runsheng Chinese Academy of Sciences Challenges in Precision Medicine Data Processing
5、Heres everything you want to know about the Vault7 CIA leaked document

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送