当我运行这段代码时:
def nothing(x): pass cv.createTrackbar(‘R’, ‘image’, 0,255,nothing)
我得到这个错误:使用“value”指针不安全且已弃用。请使用NULL作为值指针。若要获取跟踪条值,请安装回调。我不知道该怎么办。
cbjzeqam1#
你没有显示最小的工作代码,这使得问题,所以我只能猜测。在使用createTrackbar()之前,必须创建窗口"image"
createTrackbar()
"image"
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')的值
img
image
namedWindow()
getTrackbarPos()
imshow()
'0 : OFF \n1 : ON'
s = cv.getTrackbarPos(switch, 'image')
您的代码似乎与文档中的演示代码相同,并且demo在所有命令中都使用"image"。demo还使用cv.getTrackbarPos(switch, ...)
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中的代码相同,因此仅当您更改任何跟踪条中的值时才会执行。
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()
1条答案
按热度按时间cbjzeqam1#
你没有显示最小的工作代码,这使得问题,所以我只能猜测。
在使用
createTrackbar()
之前,必须创建窗口"image"
最小工作示例:
更复杂的示例,其中三个跟踪条使用相同的函数,但索引不同。
图片Lenna来自维基百科
您的问题是窗口使用了两个名称-
img
和image
-但您应该在namedWindow()
、createTrackbar()
、getTrackbarPos()
和imshow()
中使用相同的名称顺便说一句:如果你用
'0 : OFF \n1 : ON'
创建跟踪条,那么你必须使用它来获得s = cv.getTrackbarPos(switch, 'image')
的值您的代码似乎与文档中的演示代码相同,并且
demo
在所有命令中都使用"image"
。demo
还使用cv.getTrackbarPos(switch, ...)
与
nothing
中的代码相同,因此仅当您更改任何跟踪条中的值时才会执行。