Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Trackbar, how to display max, min and current value on trackbar window

Tags:

python

opencv

How can I ask display the range of the bar (max, min) and the current pointing value on the trackbar window? Here is the example of using the trackbar.

# trackbar
import cv2
import numpy as np

def nothing(x):
    pass

img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('image')

# Create trackbars

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

# Create switch for on/off

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:   # 27 is Escape
        break
    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()

Which doesn't display the values: enter image description here

like image 382
J_yang Avatar asked Nov 22 '22 07:11

J_yang


1 Answers

I was faced with a similar situation for one of my college activity. I did a workaround where I used the cv.putText method to put values of the BGR trackbar on the image window.

Steps I did for R value (will be same for B and G values):

  1. Create a function update_R_value. Pass this function to createTrackBar method of cv2.

  2. Put text on the image that can show the value for R. The initial value will be 0 (zero). Let's say we put it at (x, y) position.

  3. Then in update_R_value, I changed this text value every time the trackbar position gets changed.

img = np.zeros((512,512,3), np.uint8)
bgr_track = {'B': 0, 'G': 0, 'R': 0}    # Initial values of BGR
    
# Values of trackbar as text on image for R trackbar.
font = cv2.FONT_HERSHEY_SIMPLEX
img = cv2.putText(img, "R: ", (10, 330), font, 0.5, (255,255,255), 1)
img = cv2.putText(img, "0", (30, 330), font, 0.5, (255,255,255), 1)
    
def update_R_value(x):
    global font, img, bgr_track
    img = cv2.putText(img, f"{bgr_track['R']}", (30, 330), font, 0.5, (0,0,0), 1)    # Fill previous text pixels with black
    img = cv2.putText(img, f"{x}", (30, 330), font, 0.5, (255,255,255), 1)   # Put new text with color white
    bgr_track['R'] = x

Note:

In OpenCV when we put text on an image, it is just changing the pixel values at that particular position to make the text display. It involved keeping track of the previous BGR value since cv2.putText does not keep an account for previously changed pixel values.

So, in order to update the new trackbar value, first I put a text with the previous value in colour black and then I put the updated value in colour white and also saving the new value in a dictionary to repeat the process.

The complete output after doing it for the Blue and Green trackbar was like this:

Display BGR values

You can also check out the complete code here.

Thanks.

like image 135
Siddharth Avatar answered Jun 09 '23 13:06

Siddharth