Opencv study notes 3: drawing lines, circles, boxes, ellipses, text

Opencv study notes 3: drawing lines, circles, boxes, ellipses, text


Target

1 Draw different shapes using opencv
2 We will learn to use these functions: cv2.line(), cv2.circle() , cv2.rectangle(), cv2.ellipse(), cv2.putText() etc.


Parameter explanation

In all the above functions, you will see some common arguments as given below:

img : The image where you want to draw the shapes
color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
thickness : Thickness of the line or circle etc. If -1 is passed for closed figures like circles, it will fill the shape. default thickness = 1
lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves.

Drawing Line

To draw a line ,
you need to pass starting and ending coordinates of line. We will create a black image and draw a blue line on it from top-left to bottom- right corners.

import numpy as np
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
img = cv2.line(img,(0,0),(511,511),(255,0,0),5)

Drawing Rectangle

The coordinates of the upper left and lower right corners need to be provided, this time we draw a green rectangle.

img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

Drawing Circle

To draw a circle we need to know the center and radius

img = cv2.circle(img,(447,63), 63, (0,0,255), -1)

Drawing Ellipse

To draw the ellipse, we need to pass several arguments. One argument is the center location (x,y). Next argument is axes lengths (major axis length, minor axis length). angle is the angle of rotation of ellipse in anti-clockwise direction. startAngle and endAngle denotes the starting and ending of ellipse arc measured in clockwise direction from major axis. i.e. giving values 0 and 360 gives the full ellipse. For more details, check the documentation of cv2.ellipse(). Below example draws a half ellipse at the center of the image.

img = cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

Drawing Polygon

In order to draw a polygon, first you need the vertex coordinates of the polygon.

pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,[pts],True,(0,255,255))

If third argument is False, you will get a polylines joining all the points, not a closed shape.

Note

cv2.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is more better and faster way to draw a group of lines than calling cv2.line() for each line.

Add text to pictures

To put texts in images, you need specify following things.
Text data that you want to write
Position coordinates of where you want put it (i.e. bottom-left corner where data starts).
Font type (Check cv2.putText() docs for supported fonts)
Font Scale (specifies the size of font)
regular things like color, thickness, lineType etc. For better look, lineType = cv2.LINE_AA is recommended.
We will write OpenCV on our image in white color.

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

Drawing result display

So it is time to see the final result of our drawing. As you studied in previous articles, display the image to see it.
write picture description here

Drawing Functions in OpenCV

Additional Resources

The angles used in ellipse function is not our circular angles. For more details, visit this discussion.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324926607&siteId=291194637