Drawing 04.OpenCV-GUI-

 

Opencv herein describes the mapping function: draw a line, circle, ellipse, polygon, and add text

Will learn these functions: cv2.line (), cv2.circle (), cv2.rectangle (), cv2.ellipse (), cv2.putText () and so on.

The code
above all these drawing functions need to set the following parameters:
  • img: the photograph of the image you want to draw graphics.
  • color: color shape. In RGB, for example, we need to pass a tuple, for example: (255,0,0) for blue. For grayscale need only pass the gray value.
  • thickness: thickness of the line. If set to a pattern close to -1, then the pattern will be filled. The default value is 1.
  • LINETYPE: line style, and 8 is connected, antialiasing. The default is 8. cv2.LINE_AA as anti-aliasing, this looks very smooth.

Draw lines
  to draw a line, you just need to tell the function to start and end point of this line. Below we will draw a blue line from the top left to the bottom right corner.

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
cv2.line(img,(0,0),(511,511),(255,0,0),5)

cv2.line () parameters are the name of the picture, the starting point coordinates, end point coordinates, color (255,0,0) on behalf of blue, and thickness.

Draw a rectangle  

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

Parameters are the name of the picture, the coordinates of the upper left, lower right coordinates, color (255,0,0) on behalf of blue, and thickness.

Circle
  to circle, then, you need to specify the center point coordinates and the radius of the circle size. We draw a circle on top of the rectangle.

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

Videos ellipse
  position coordinates of the center point, the next parameter is the length of the major and minor axes. Ellipse rotated counterclockwise angle. Speech clockwise elliptical arc starting and ending angles, with 360 if it is 0, that is, the entire ellipse. View cv2.ellipse () can get more information. The following example is plotted in the center of the image half ellipse.

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

 Polygon drawing
  polygon drawing, you need pointing coordinates of each vertex. Construction of these coordinate points with a size equal to the number of array rows, the number of rows is the number of points X1X2. This array of data types must be int32.
  Here draw a polygon having four yellow vertices.

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

 Here # reshape first parameter -1, indicating the length of this dimension is based on the dimensions of the latter calculated.
Note: If the third parameter is False, we get a polygon is not closed (both not connected).
Note: cv2.polylines () it can be used to draw a lot of lines. Just wanted to draw the line put on a list, the list passed to the function on it. Independently each line will be drawn. This compared with cv2.line () one by one to draw faster.

 Adding text on a picture
  to draw text on the picture, you need to set the following parameters:
  • You have to draw the text
  • You have to draw the location
  • font type (by looking at cv2.putText () documentation to find support fonts)
  • Fonts the size of the
  general properties • text, such as color, thickness, line type and so on. In order to look a little better recommended linetype = cv2.LINE_AA.
OpenCV drawing white on the image.

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

Warning: all drawing function return values ​​are None, can not be used img = cv2.line (img, (0,0), (511,511), (255,0,0), 5).

 

 

 

Guess you like

Origin blog.csdn.net/weixin_42572978/article/details/92614952