gpt4 book ai didi

python - OpenCV - Python trackbars 不能在 macOS 上运行,但在 Ubuntu 上运行良好

转载 作者:行者123 更新时间:2023-12-02 16:30:08 24 4
gpt4 key购买 nike

我正在运行从网站获得的代码,该代码在 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 中轨迹栏不移动的图像:
This is the image from my macbook where the trackbars don't move
为什么会这样?
请帮忙。谢谢。

最佳答案

如果/否则 声明应在 内而环形


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

关于python - OpenCV - Python trackbars 不能在 macOS 上运行,但在 Ubuntu 上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44333631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com