[OpenCV-Python] 8 Use the slider to make a palette

OpenCV-Python: II Gui features in OpenCV

8 Use the slider to make a palette

Goal
  • Learn to bind the slider to the OpenCV window
  • You will learn these functions: cv2.getTrackbarPos(), cv2.creatTrackbar(), etc.

8.1 Code example

Now let's create a simple program: set the color of the drawing board by adjusting the slider. We want to create a window to display the color, and three sliders to set the colors of B, G, and R. When we slide the scroll bar, the color of the window will also change accordingly. By default, the starting color of the window is black.
 cv2.getTrackbarPos() function:

The first parameter is the name of the slider

The second parameter is the name of the window where the slider is placed

The third parameter is the default position of the slider. The fourth parameter is the maximum value of the slider

The fifth function is a callback function, which will be called every time the slider slides. The callback function usually contains a default parameter, which is the position of the slider. In this case, this function doesn't need to do anything, we only need pass.

Another important application of the slider is to be used as a switch button. By default, OpenCV does not have button functions. So we use sliders instead. In our program, we want to create a switch button. Only when the switch button points to ON, the sliding of the slide bar is useful, otherwise the windows are all black.

import cv2
import numpy as np

def nothing(x):
    pass

# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)

# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)

while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

    # get current positions of four trackbars
    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]

cv2.destroyAllWindows()

img

Exercise

  1. Combining the knowledge in the previous section, create a drawing board, you can choose various colors of brushes to paint various graphics
    .
import cv2
import numpy as np
def nothing(x):
    pass
# 当鼠标按下时变为 True
drawing=False
# 如果 mode 为 true 绘制矩形。按下 'm' 变成绘制曲线。
mode=True
ix,iy=-1,-1
# 创建回调函数
def draw_circle(event,x,y,flags,param):
    r=cv2.getTrackbarPos('R','image')
    g=cv2.getTrackbarPos('G','image')
    b=cv2.getTrackbarPos('B','image')
    color=(b,g,r)
    global ix,iy,drawing,mode
    # 当按下左键是返回起始位置坐标
    if event==cv2.EVENT_LBUTTONDOWN:
        drawing=True
        ix,iy=x,y
    # 当鼠标左键按下并移动是绘制图形。 event 可以查看移动, flag 查看是否按下
    elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
        if drawing==True:
            if mode==True:
                cv2.rectangle(img,(ix,iy),(x,y),color,-1)
            else:
                # 绘制圆圈,小圆点连在一起就成了线, 3 代表了笔画的粗细
                cv2.circle(img,(x,y),3,color,-1)
                # 下面注释掉的代码是起始点为圆心,起点到终点为半径的
                # r=int(np.sqrt((x-ix)**2+(y-iy)**2))
                # cv2.circle(img,(x,y),r,(0,0,255),-1)
                # 当鼠标松开停止绘画。
    elif event==cv2.EVENT_LBUTTONUP:
        drawing==False
        # if mode==True:
            # cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
        # else:
            # cv2.circle(img,(x,y),5,(0,0,255),-1)
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.setMouseCallback('image',draw_circle)
while(1):
    cv2.imshow('image',img)
    
    k=cv2.waitKey(1)&0xFF
    if k==ord('m'):
        mode=not mode
    elif k==27:
        break

img
For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113405586