5. Drawing functions in OpenCV

Source: Internet
Author: User

1. Target

A. Learn to draw different geometries using OpenCV

b, you will learn these functions: Cv2.line (), Cv2.circle (), Cv2.rectangle (), Cv2.ellipse (), Cv2.puttext () and so on.

2. Code

All of these drawing functions above require the following parameters to be set:

IMG: The image you want to draw the graphic.

Color: The colors of the shape. In RGB, for example, you need to pass in a tuple, for example: (255,0,0) represents blue. You only need to pass in grayscale values for grayscale graphs.

Thickness: The thickness of the line. If a closed shape is set to-1, the shape is filled. The default value is 1.

Linetype: Type of line, 8 connection, anti-aliasing, etc. The default is 8 connections. Cv2. LINE_AA is anti-aliasing, so it looks very smooth.

1. Draw the line

To draw a line, you just need to tell the function the start and end of the line. Below we will draw a blue line from the top left to the lower right corner.

2. Draw a rectangle

To draw a rectangle, you need to tell the coordinates of the upper-left and lower-right vertices of the function. This time we'll have a green rectangle in the upper-right corner of the image.

3. Draw a Circle

To draw a circle, you only need to specify the center point coordinate and radius size of the circle. We draw a circle in the rectangle above.

4. Draw Ellipse

The ellipse is more complicated, we need to enter several parameters. One parameter is the position coordinate of the center point. The next parameter is the length of the long axis and the short axis. The angle at which the ellipse rotates counterclockwise. Elliptical Arc Modeling
The angle at which the clockwise direction starts and the end angle, if 0 is 360, is the entire ellipse. See Cv2.ellipse () for more information. The following example draws a half ellipse at the center of the image

5. Draw polygons

To draw a polygon, you need to point the coordinates of each vertex. Use the coordinates of these points to construct an array of size equal to the number of rows x1x2, which is the number of points. The data type of this array must be int32.
Here, draw a yellow polygon with four vertices.

6. Add text to the picture

To draw text on a picture, you need to set the following parameters:
? The text you want to draw
? The position you want to draw
? Font type (find supported fonts by viewing Cv2.puttext () documents)
? Size of font
? General attributes of text such as color, thickness, type of line, and so on. In order to better see a little recommended to use
Linetype=cv2. Line_aa.
Draws a white OpenCV on the image.

5. Drawing functions in OpenCV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.