程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[opencv - Python]: use createtrackbar to create a slider

編輯:Python

Catalog

  • 1. Correlation function
  • 2. Single slider
  • 3. Multiple sliding bars
  • 4. Reference material

1. Correlation function

  • createTrackbar
// 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)
  • getTrackbarPos
// Get the current slider position of a specific slider 
int cv::createTrackbar(const String & trackbarname, // Slider name 
const String & winname) // Window name 

2. Single slider

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)

3. Multiple sliding bars

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

4. Reference material

https://docs.opencv.org/4.x/d9/dc8/tutorial_py_trackbar.html


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved