// Create a slider and attach it to the specified window
int cv::createTrackbar(const String & trackbarname, // Slider name
const String & winname, // Window name
int * value, // Initial position of sliding bar
int count, // Slider maximum
TrackbarCallback onChange = 0, // Callback function
void * userdata = 0)
// Get the current slider position of a specific slider
int cv::createTrackbar(const String & trackbarname, // Slider name
const String & winname) // Window name
Linearly superimpose two images :
import cv2
max_alpha = 100
window_name = 'Linear blend'
src1 = cv2.imread('1.jpg')
src2 = cv2.imread('2.jpg')
# Callback function
def update_alpha(x):
alpha = x / max_alpha
beta = 1.0 - alpha
dst = cv2.addWeighted(cv2.resize(src1, (330,186)), alpha, src2, beta, 0)
cv2.imshow(window_name, dst)
cv2.namedWindow(window_name)
cv2.createTrackbar('Alpha', window_name, 0, max_alpha, update_alpha)
cv2.waitKey(0)
Change the contrast and brightness of the image :
import cv2
import numpy as np
# A single callback function cannot get two at the same time Trackbar Sliding block position of , Define a return pass The callback function of
def nothing(x):
pass
window_name = 'contrast_brightness'
image = cv2.imread('2.jpg')
cv2.imshow('Original image', image)
cv2.namedWindow(window_name)
cv2.createTrackbar('contrast', window_name, 0, 300, nothing)
cv2.createTrackbar('brightness', window_name, 0, 255, nothing)
while True:
# Use getTrackbarPos Function to obtain the contrast and brightness values respectively
contrast = cv2.getTrackbarPos('contrast', window_name)
brightness = cv2.getTrackbarPos('brightness', window_name)
new_image = cv2.convertScaleAbs(image, contrast*0.01, brightness)
cv2.imshow(window_name, new_image)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
https://docs.opencv.org/4.x/d9/dc8/tutorial_py_trackbar.html