【问题标题】:OpenCV - Python trackbars not working on macOS, but works fine in UbuntuOpenCV - Python trackbars 不能在 macOS 上运行,但在 Ubuntu 上运行良好
【发布时间】:2017-11-04 03:25:26
【问题描述】:

我正在运行从网站获得的代码,该代码在 Ubuntu 上运行完全正常,但轨迹栏在 macOS 上没有移动:

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()

这是我的 macbook 中轨迹栏不移动的图像:

为什么会这样? 请帮忙。谢谢。

【问题讨论】:

  • 我找到了解决方案...我没有使用 k=cv2.waitKey(1) & 0xFF,而是使用了 k=cv2.waitKey(500)。
  • 但它看起来仍然与 ubuntu 的 trackbar 不同。为什么?
  • 感谢您的提问和解答!
  • 请发表您的评论作为答案。 cmets 部分用于回答问题。

标签: python macos python-3.x opencv ubuntu


【解决方案1】:

if/else 语句应该在 while 循环内


import cv2
import numpy as np

def nothing(x):
   pass

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

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

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

    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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多