SoFunction
Updated on 2024-11-21

Python OpenCV Using Sliders to Tune Function Arguments

introductory

When observing the effect of a function in OpenCV with different parameters, I used to change the parameters once and run it once, which is troublesome and inefficient. In order to observe the processing effect of parameter change more conveniently, you can use the slider to change the parameter.

Specific ideas

  1. Use () to create sliders with as many parameters as you want
  2. Define callback functions for each parameter
  3. Displaying images in callback functions

Note that the name of the window in the slider should be the same as the name of the window in which the image is displayed.

code implementation

import cv2

d = 0
color = 0
space = 0
def change_d(x):
  d = x
  blur = (img,d,color,space)
  ("myImg",blur)

def change_color(x):
  color = x
  blur = (img,d,color,space)
  ("myImg",blur)

def change_space(x):
  space = x
  blur = (img,d,color,space)
  ("myImg",blur)

img = ('')
('myImg')
('d','myImg',1,500,change_d)
('color','myImg',1,500,change_color)
('space','myImg',1,500,change_space)

while(1):
  k = (1)&0xFF
  if k==27:
    break
  d = ('d','myImg')
  color = ('color','myImg')
  space = ('space','myImg')


()

Effective demonstration

This is the entire content of this article.