OpenCV: Adding a Trackbar to our applications!

摘自https://docs.opencv.org/4.2.0/da/d6a/tutorial_trackbar.html

Well, it is time to use some fancy GUI tools. OpenCV provides some GUI utilities (highgui module) for you. An example of this is a Trackbar.

For cv.createTrackbar() function, first argument is the trackbar name, second one is the window name to which it is attached, third argument is the default value, fourth one is the maximum value and fifth one is the callback function which is executed every time trackbar value changes. The callback function always has a default argument which is the trackbar position. 

import cv2 as cv

def onchange(alpha):
    alpha = alpha/100.0
    img = cv.addWeighted(img1, alpha, img2, 1.0-alpha, 0.0)
    cv.imshow('Image Transfer', img)

img1 = cv.imread('sakura.jpg',cv.IMREAD_COLOR)
img2 = cv.imread('channel_error.jpg', cv.IMREAD_COLOR)

cv.namedWindow('Image Transfer', cv.WINDOW_AUTOSIZE)
cv.createTrackbar('alpha', 'Image Transfer', 0, 100, onchange)
    
onchange(0)

cv.waitKey()

猜你喜欢

转载自blog.csdn.net/Airfrozen/article/details/104459654