GUI features of OPENCV: drawing function

Let's first understand what GUI features are; let's learn the information from Baidu entries:

Graphical User Interface (GUI, also known as Graphical User Interface) refers to a computer operating user interface displayed in a graphical manner. 

A graphical user interface is an interface display format for human- computer communication that allows users to use input devices such as a mouse to manipulate on- screen icons or menu options to select commands, invoke files, launch programs, or perform some other everyday tasks. Graphical user interfaces have many advantages over character interfaces where routine tasks are accomplished by entering text or character commands through the keyboard. Graphical user interfaces consist of windows, drop-down menus, dialog boxes and their corresponding control mechanisms, which are standardized in various modern applications, that is, the same operation is always done in the same way. In a graphical user interface, the user What you see and operate are all graphic objects, and the technology of computer graphics is applied. 

In this way, in the past computer display interface, it is all information display, that is to say, the displayed interface is not as graphical as it is now, which reduces the burden of work for the creator, but increases the use of understanding of it. Therefore, in order to make the computer interface more user-friendly, it is necessary for programmers to learn GUI features. 

 Click here to see more content: Baidu entry: GUI features


target:

  •         Learn Opencv to draw different geometric shapes
  •         function of plotting function
  •         Common parameters

draw geometry

(1) Line drawing

To draw a line, we need to pass in the starting and ending coordinates of the line to the cv2.line() function. Before that, you also need to create a black background with numpy.

import numpy as np
import cv2

img = np.zeros((450,450,3),np.uint8)
cv2.line(img,(0,0),(450,450),(255,0,0),3)

cv2.imshow('imgline',img)
cv2.waitKey(0)

(2) Draw a rectangle

We need to provide the upper left and lower right corners to the cv2.rectangle() function.

cv2.rectangle(img,(126,0),(400,126),(0,255,0),3)

(3) Draw a circle

 To draw a circle, you need to provide its center coordinates and radius. We will draw a circle inside the rectangle drawn above.

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

(4) Draw an ellipse

To draw an ellipse, one parameter is its center coordinate (x, y), and the other parameter is the length of the major and minor axes. angle is the angle by which the ellipse is rotated counterclockwise. startAngle and endAngle represent the start and end of the elliptical arc measured counterclockwise from the main axis

cv2.ellipse(img,(250,250),(120,50),0,0,180,255,-1)

(5) Draw polygons

First, the coordinates of the vertices are required, and these points are formed into an array of shape Rows*1*2, where Rows is the number of vertices, and its type should be int32. Here, we draw a small yellow polygon with four 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))

If the third parameter is False, you will get a polyline connecting all the points instead of a closed shape. 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.

(6) Add text to the image

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

The parameters are text, lower left corner coordinates, font type, font size, color, thickness, and line type.

Full code:

import numpy as np
import cv2
#############创建背景##################
img = np.zeros((450,450,3),np.uint8)
#############对角线条##################
cv2.line(img,(0,0),(450,450),(255,0,0),3)
#############画矩形框##################
cv2.rectangle(img,(126,0),(400,126),(0,255,0),3)
#############画实心圆##################
cv2.circle(img,(263,63), 63, (0,0,255), -1)
#############画椭圆形##################
cv2.ellipse(img,(250,250),(120,50),0,0,180,255,-1)
#############画多边形##################
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))
#############放置文本##################
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,420), font, 3,(255,255,255),2,cv2.LINE_AA)

cv2.imshow('imgline',img)
cv2.waitKey(0)

Actual renderings:

 

Guess you like

Origin blog.csdn.net/m0_62919535/article/details/127151652