简体   繁体   English

OpenCV 轨迹栏,如何在轨迹栏窗口上显示最大值、最小值和当前值

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

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:哪个不显示值: 在此处输入图片说明

无法指示与 pos=0 不同的最小值,但由于轨迹栏没有刻度标签,有时将第一个刻度重新解释为 1 (pos+1) 很有用

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.我做了一个解决方法,我使用cv.putText方法将 BGR cv.putText值放在图像窗口上。

Steps I did for R value (will be same for B and G values):我为R值所做的步骤(对于BG值将相同):

  1. Create a function update_R_value .创建一个函数update_R_value Pass this function to createTrackBar method of cv2 .通过这个功能, createTrackBar的方法cv2

  2. Put text on the image that can show the value for R .在图像上放置可以显示R值的文本。 The initial value will be 0 (zero).初始值将为 0(零)。 Let's say we put it at (x, y) position.假设我们把它放在 (x, y) 位置。

  3. Then in update_R_value , I changed this text value every time the trackbar position gets changed.然后在update_R_value ,每次update_R_value位置更改时,我都会更改此文本值。

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.在 OpenCV 中,当我们在图像上放置文本时,它只是更改该特定位置的像素值以显示文本。 It involved keeping track of the previous BGR value since cv2.putText does not keep an account for previously changed pixel values.它涉及跟踪先前的 BGR 值,因为cv2.putText不记录先前更改的像素值。

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:为蓝色和绿色轨迹栏执行此操作后的完整输出如下所示:

显示 BGR 值

You can also check out the complete code here .您还可以在此处查看完整代码。

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM