您没有显示导致问题的最小工作代码,所以我只能猜测。
您必须先创建窗口"image",然后才能使用createTrackbar()
cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, function)
最小的工作示例:
import cv2
# --- functions ---
def function(value):
print(value)
new_img = img.copy()
new_img[:,:,2] = value
cv2.imshow('image', new_img)
# --- main ---
img = cv2.imread('lenna.png')
cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, function)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
编辑:
具有三个轨迹栏的更复杂的示例,它们使用相同的功能但具有不同的索引。
import cv2
# --- functions ---
def function(index, value):
percent = (value/100)
#print(index, value, percent)
img[:,:,index] = original_img[:,:,index] * percent
cv2.imshow('image', img)
# --- main ---
img = cv2.imread('lenna.png')
original_img = img.copy()
cv2.namedWindow('image')
cv2.createTrackbar('R (%)', 'image', 100, 100, lambda value:function(2, value))
cv2.createTrackbar('G (%)', 'image', 100, 100, lambda value:function(1, value))
cv2.createTrackbar('B (%)', 'image', 100, 100, lambda value:function(0, value))
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
来自维基百科的图片Lenna
编辑:
您的问题是您为窗口使用了两个名称 - img 和 image - 但您应该在 namedWindow()、createTrackbar()、getTrackbarPos()、imshow() 中使用相同的名称
顺便说一句:如果您使用'0 : OFF \n1 : ON' 创建轨迹栏,那么您还必须使用它来获得价值s = cv.getTrackbarPos(switch, 'image')
编辑:
您的代码似乎与文档中的demo 相同,demo 在所有命令中都使用"image"。 demo 也使用 cv.getTrackbarPos(switch, ...)
import cv2 as cv
import numpy as np
def nothing(x):
pass
img = np.array ((512,512,3), np.uint8)
cv.namedWindow("image")
cv.createTrackbar('R', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('B', 'image', 0, 255, nothing)
switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch, 'image', 1, 1, nothing)
while True:
cv.imshow('image', img)
key = cv.waitKey(1) & 0xFF
if key == 27:
break
r = cv.getTrackbarPos('R', 'image')
g = cv.getTrackbarPos('G', 'image')
b = cv.getTrackbarPos('B', 'image')
s = cv.getTrackbarPos(switch, 'image')
if s == 0:
img[:] = 0
# reset trackbars
#cv.setTrackbarPos('R', 'image', 0)
#cv.setTrackbarPos('G', 'image', 0)
#cv.setTrackbarPos('B', 'image', 0)
else:
img[:] = [b,g,r]
cv.destroyAllWindows()
与nothing 中的代码相同,因此仅当您更改任何轨迹栏中的值时才会执行。
import cv2 as cv
import numpy as np
# --- functions ---
def nothing(value):
r = cv.getTrackbarPos('R', 'image')
g = cv.getTrackbarPos('G', 'image')
b = cv.getTrackbarPos('B', 'image')
s = cv.getTrackbarPos(switch, 'image')
if s == 0:
img[:] = 0
# reset trackbars
#cv.setTrackbarPos('R', 'image', 0)
#cv.setTrackbarPos('G', 'image', 0)
#cv.setTrackbarPos('B', 'image', 0)
else:
img[:] = [b,g,r]
# --- main ---
img = np.array ((512,512,3), np.uint8)
cv.namedWindow("image")
cv.createTrackbar('R', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('B', 'image', 0, 255, nothing)
switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch, 'image', 1, 1, nothing)
while True:
cv.imshow('image', img)
key = cv.waitKey(1) & 0xFF
if key == 27:
break
cv.destroyAllWindows()