I'm trying to create a trackbar for contours but when I run the code I get this error:
TypeError: thresh_callback() takes exactly 3 arguments (1 given)
the code:
def thresh_callback(thresh,blur,img):
edges = cv.Canny(blur,thresh,thresh*2)
drawing = np.zeros(img.shape,np.uint8) # Image to draw the contours
contours,hierarchy = cv.findContours(edges,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
for cnt in contours:
color = np.random.randint(0,255,(3)).tolist() # Select a random color
cv.drawContours(drawing,[cnt],0,color,2)
cv.imshow('output',drawing)
cv.imshow('input',img)
def Pics():
vc = cv.VideoCapture(2)
retVal, frame = vc.read();
while True :
if frame is not None:
imgray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
blur = cv.GaussianBlur(imgray,(5,5),0)
thresh = 100
max_thresh = 255
cv.createTrackbar('canny thresh:','input',thresh,max_thresh,thresh_callback)
thresh_callback(thresh,blur,frame)
rval, frame = vc.read()
if cv.waitKey() & 0xFF == 27:
break
cv1.DestroyAllWindows()
您正在将函数thresh_callback
作为回调传递给cv.createTrackbar(
。看起来该方法期望单参数函数调用某个事件。
You're passing thresh_callback
into cv.createTrackbar
, and it is somewhere in there that your function is getting called with only one argument. I assume that you still want to use the blur
and frame
that you determine in your code, so trying using functools.partial
to set those for you:
import functools
...
partialed_callback = functools.partial(thresh_callback, blur=blur, img=frame)
cv.createTrackbar('canny thresh:','input',thresh,max_thresh,partialed_callback)
This creates a version of your function for which blur
and frame
are already set, so your thresh_callback
function will be called with the frame
and blur
defined in the loop and the thresh
provided from within createTrackbar
.
Also, you probably don't want to be calling thresh_callback(thresh,blur,frame)
in the line after calling cv.createTrackbar
, as that will mean it gets called twice and always with thresh=100
the second time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.