Reputation: 1
When I run this code :
def nothing(x):
pass
cv.createTrackbar(‘R’, ‘image’, 0,255,nothing)
I get this error: Using ‘value’ pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.
I couldn’t find what to do.
Upvotes: -1
Views: 8191
Reputation: 143097
You didn't show minimal working code which makes problem so I can only guess.
You have to create window "image"
before you use createTrackbar()
cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, function)
Minimal working example:
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()
EDIT:
More complex example with three trackbars which use the same function but with different index.
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()
Image Lenna from Wikipedia
EDIT:
Your problem is that you use two names for window - img
and image
- but you should use the same name in namedWindow()
, createTrackbar()
, getTrackbarPos()
, imshow()
BTW: If you create trackbar with '0 : OFF \n1 : ON'
then you have to use it aslo to get value s = cv.getTrackbarPos(switch, 'image')
EDIT:
It seems you have the same code as in demo in documentation and demo
uses "image"
in all commands. demo
also uses 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()
The same with code in nothing
so it is executed only when you change value in any trackbar.
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()
Upvotes: 2