Ask Your Question
0

small question about createTrackbar() function

asked 2017-07-31 18:40:52 -0500

hamoda gravatar image

Hi everybody,

I want to understand one lines of this code

import cv2
import numpy as np

def nothing(x):
    pass

# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)

# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)

while(1):
    cv2.imshow('image',img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

    # get current positions of four trackbars
    r = cv2.getTrackbarPos('R','image')
    g = cv2.getTrackbarPos('G','image')
    b = cv2.getTrackbarPos('B','image')
    s = cv2.getTrackbarPos(switch,'image')

    if s == 0:
        img[:] = 0
    else:
        img[:] = [b,g,r]

cv2.destroyAllWindows()
  • The line is

cv2.createTrackbar('R','image',0,255,nothing)

why we pass a function that does nothing to cv2.createTrackbar() function?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-08-01 00:52:06 -0500

berak gravatar image

the callback function is a mandatory argument, you can neither skip it, or replace it with None, so there needs to be a "dummy" function for this purpose (well, at least a dummy function, ofc. you could try to do something useful in there).

it may look a bit weird, but you'll probably have to accept it as "the way it is".

edit flag offensive delete link more

Comments

yes it is really weird

hamoda gravatar imagehamoda ( 2017-08-01 01:30:22 -0500 )edit

but let us also repeat that trackbars in OpenCV are purely for debugging. UI should never be developed using OpenCV native code :) its not meant to be used like that.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-08-01 07:44:17 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-07-31 18:40:52 -0500

Seen: 235 times

Last updated: Aug 01 '17