Content from Opencv-python tutorials own translation finishing
Target:
Use the scroll bar to bind a window
Learn the following functions Cv2.gettrackbarpos (), Cv2.createtrackbar ()
Sample code:
Here is a simple example to create a realistic user-specified color. The user uses three scroll bars to specify the B,g,r value, and the user-selected color is displayed in the window. The user drags the scroll bar to the color change in the window. The initial default color is set to black.
For the Cv2.gettrackbarpos () function, the first argument is the name of the scroll bar, the second argument is the associated window name, the third parameter is the default, the fourth is the maximum, and the fifth is the callback function that executes all the changed values. The callback function always has a default parameter, which is the position of the scroll bar. In this example, the function doesn't do anything, simply skip it.
Scroll bar Another important application is to use as a button or a switch. By default, there is no button function in the OpenCV. So users need to use the scroll bar to achieve such a function. In this example, we create a switch that controls whether the application's color change function is turned on and if the switch function is turned off, the screen is always black.
Import cv2
import NumPy as NP
def nothing (x):
Pass
# Create a black background window
img = Np.zeros ((300,512,3), np.uint8)
Cv2.namedwindow (' image ')
# Create scroll bars that change colors
cv2.createtrackbar (' R ', ' image ', 0,255,nothing)
Cv2.createtrackbar (' G ', ' image ', 0,255,nothing)
cv2.createtrackbar (' B ', ' image ', 0,255,nothing)
# switch switches for creating control functions
= ' 0:off \n1:on '
cv2.createtrackbar (switch, ' image ', 0,1,nothing) while
(1):
Cv2.imshow (' Image ', img)
k = Cv2.waitkey (1) & 0xFF
If k =: Break
# get four scroll bar position
r = Cv2.gettrackbarpos (' R ', ' image ')
g = Cv2.gettrackbarpos (' g ', ' image ')
B = Cv2.gettrackbarpos (' B ', ' image ')
s = cv2.gettrackbarpos (switch, ' image ')
if s = = 0:
img[:] = 0
Else:
img[:] = [b,g,r]# Assign the color value in the scroll bar to the picture
cv2.destroyallwindows ()
Practice:
Create a paint program that adjusts the color and the radius of the brush. Use the scroll bar adjustment.
Import cv2
import numpy as NP
# Mouse callback function
def nothing (x):
pass
def draw_circle (event, X,y,flags,param):
s=cv2.gettrackbarpos (' size ', ' image ')
b=cv2.gettrackbarpos (' B ', ' image ')
r= Cv2.gettrackbarpos (' R ', ' image ')
g=cv2.gettrackbarpos (' g ', ' image ')
if event = = Cv2. EVENT_LBUTTONDBLCLK:
cv2.circle (IMG, (x,y), S, (B,g,r), -1)
# Create A black image, a window and bind the function to Window
img = Np.zeros ((512,512,3), np.uint8)
Cv2.namedwindow (' image ')
cv2.createtrackbar (' R ', ' Image ', 0,255,nothing '
cv2.createtrackbar (' G ', ' image ', 0,255,nothing)
cv2.createtrackbar (' B ', ' image ', 0,255,nothing)
cv2.createtrackbar (' size ', ' image ', 1,255,nothing)
cv2.setmousecallback (' image ', draw_ Circle) while
(1):
cv2.imshow (' Image ', img)
if cv2.waitkey & 0xFF =:
break Cv2.destroyallwindows ()
Double-click a circle