[OpenCV-Python] - Graphical User Interface & Window Control & Drawing Basic Graphics & Responding to Mouse Events & Using the Tracking Bar

Table of contents

Foreword:

1. Window control

2. Draw straight lines, rectangles, ellipses, text arrows, etc.

3. Respond to mouse events

4. Use the Trackbar

Summarize:


Foreword:

The functions of OpenCV's Graphical User Interface (GUI) mainly include reading, writing and displaying images, reading, writing and displaying videos, window control (creation, size, closing), drawing, responding to mouse events, and using track bars, etc. .

1. Window control

create cv2.namedWindow(winName, flags) . Attribute constant flags have NORMAL: resizable, unlimited; AUTOSIZE: default value, cannot be resized, and the size is determined by the image; FULLSCREEN: the window is displayed in full screen; GUI_EXPANDED: the window can display the status bar and toolbar; FREERATIO: as many as possible Display pictures without scale limitation; KEEPRATIO: The window is determined by the scale of the image.

Close cv2.destroyAllWindows() means to close all windows, and cv2.destroyWindow(winName) means to close the window with the specified name.

Resize cv2.resizeWindow(winName,size) , size is a two-tuple such as (240, 320).

2. Draw straight lines, rectangles, ellipses, text arrows, etc.

Draw polygon polylines(img,pts,isclosed,color,thickness). pts is the coordinates of each point and is usually placed in an array. When isclosed is True, the connection is closed, otherwise it is not closed.

Draw text putText(img, text, org, fontFace, fontScale, color, thickness). text is the text, org is the position of the lower left corner of the text, fontFace is the font type, and fontScale is the font size.

import cv2
import numpy as np
img = np.zeros((480,640,3),np.uint8)+255#创建纯白背景用于画图
cv2.line(img,(40,50),(80,100),(255,0,0),4)
cv2.rectangle(img,(80,100),(380,380),(0,255,0),3)#rectangle(img,左上角坐标,右下角坐标,颜色,粗细)
cv2.circle(img,(320,240),50,(0,0,255),3,16)#circle(img,圆心坐标,半径,颜色,线宽,精细度)后两个参数可选填
cv2.ellipse(img,(320,240),(100,50),0,0,360,(0,0,255),5,16)#椭圆ellipse(img,中心点,长宽的一半,角度,从哪个角度开始,从哪个角度结束)
pts=np.array([[10,20],[20,30],[30,15],[25,60],[50,80]],np.int32)
cv2.polylines(img,[pts],True,(255,0,0),3)#随便给的点可能四不像
cv2.putText(img,'Hello opencv',(20,100),cv2.FONT_HERSHEY_COMPLEX,2,(0,0,255))
cv2.imshow('draw',img)
cv2.waitKey()
cv2.destroyAllWindows()

    

In addition to the parameters in the above code example, they generally have two parameters at the end: lineType line type and shift parameter. The default value of lineType is cv2.Line_8 generally does not need to be set; the numerical precision of shift coordinates generally does not need to be set. lineType includes FILLED: filling, LINE_4: 4 connecting lines, LINE_8: 8 connecting lines, LINE_AA: anti-aliased lines, smoother.

3. Respond to mouse events

OpenCV can call the mouse callback function to complete event processing when the user triggers a mouse event. The mouse callback function is a custom function, and the basic format is:

def mouseCallback(event,x,y,flags,param):
    ...

mouseCallback is a custom name; event is the mouse event object passed to the function when calling; x and y are the coordinates (x, y) of the mouse pointer in the window when the mouse event is triggered; flags is the mouse drag when the mouse event is triggered Or keyboard key operation, the parameters are press the left mouse button, release the left mouse button, double-click the left button, press ctrl, etc.; param is other data passed to the callback function.

The cv2.setMouseCallback() function is used to bind the mouse callback function to the image window, the format is as follows:

cv2.setMouseCallback(wname,mouseCallback)

wname is the name of the window, which is the name of the callback function.

img=np.zeros((200,320,3),np.uint8)+255
def draw(event,x,y,flag,param):
    if event==cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),20,(255,0,0),-1)#双击鼠标左键开始画圆,坐标x,y就是鼠标点击的位置
    elif event==cv2.EVENT_RBUTTONDBLCLK:
        cv2.rectangle(img,(x,y),(x+20,y+20),(0,0,255),-1)#双击鼠标右键开始画矩形

cv2.namedWindow('drawing')
cv2.setMouseCallback('drawing',draw)
while True:
    cv2.imshow('drawing',img)
    k=cv2.waitKey(1)
    if k==27:#按esc结束
        break
cv2.destroyAllWindows()

4. Use the Trackbar

Trackbar (Trackbar) is an interactive tool provided by OpenCV for the image window, which can obtain values ​​within a specific range through the slider position of the trackbar.

cv2.createTrackbar(trackbarname, wname, value, count, onChange, userdata) is used to create a trackbar. value is the initial position of the slider in the track bar; count is the maximum value of the track bar, and the minimum value is 0; onChange is the name of the callback function called when the position of the track bar slider changes; userdata is other optional data passed to the callback function .

The cv2.getTrackbarPos(trackbarname,wname) function is used to return the current value of the trackbar.

img=np.zeros((120,400,3),np.uint8)#创建黑色图像
def doChange(x):
    b=cv2.getTrackbarPos('B','window')
    g=cv2.getTrackbarPos('G','window')
    r=cv2.getTrackbarPos('R','window')
    img[:]=[b,g,r]#改变图像
cv2.namedWindow('window')
cv2.createTrackbar('B','window',0,255,doChange)
cv2.createTrackbar('G','window',0,255,doChange)
cv2.createTrackbar('R','window',0,255,doChange)
while True:
    cv2.imshow('window',img)
    k = cv2.waitKey(1)
    if k==27:#按esc结束
         break
cv2.destroyAllWin

 Drag the color below the bar will change, similar to the PS palette! !

Summarize:

As a beginner, there may be many places that have not been fully summarized or are wrong. After further in-depth study, I will continue to come back and delete them. Friends are welcome to correct me! Learn image transformations next time !

Guess you like

Origin blog.csdn.net/weixin_51658186/article/details/130339070