// 創建一個滑動條並將其附加到指定的窗口
int cv::createTrackbar(const String & trackbarname, // 滑動條名稱
const String & winname, // 窗口名稱
int * value, // 滑動條初始位置
int count, // 滑動條最大值
TrackbarCallback onChange = 0, // 回調函數
void * userdata = 0)
// 獲取特定滑動條的當前滑動塊位置
int cv::createTrackbar(const String & trackbarname, // 滑動條名稱
const String & winname) // 窗口名稱
線性疊加兩張圖像:
import cv2
max_alpha = 100
window_name = 'Linear blend'
src1 = cv2.imread('1.jpg')
src2 = cv2.imread('2.jpg')
# 回調函數
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)
改變圖像對比度和亮度:
import cv2
import numpy as np
# 單個回調函數無法同時獲取兩個Trackbar的滑動塊位置,定義一個返回pass的回調函數即可
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:
# 使用getTrackbarPos函數分別獲取對比度和亮度值
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