在MacOS上不运行,但在Ubuntu上运行良好

yjghlzjz  于 2022-10-05  发布在  Mac
关注(0)|答案(2)|浏览(249)

我正在运行一个我从网站上得到的代码,代码在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上的一张图片,其中的轨迹条没有移动:

这一切为什么要发生?请帮帮忙。谢谢。

1yjd4xko

1yjd4xko1#

如果您使用以下选项更新您的CV2,则已修复此问题:

pip install opencv-python-rolling
k3bvogb1

k3bvogb12#

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

相关问题