如何创建不调用任何函数的 Trackbars? OpenCV 3.1 与 Python 2.7
How to create Trackbars which do not call any function? OpenCV 3.1 with Python 2.7
我正在尝试对图像进行阈值处理。我使用 cv2.createTrackbar
函数作为-
cv2.createTrackbar('High H','image',0,179, None)
.
现在最后一部分是我遇到的麻烦。在我的代码中,我使用 highH = cv2.getTrackbarPos('High H','image')
获取我的轨迹栏值并在 cv2.inRange
函数中使用它。所以很明显我不需要调用函数作为函数的最后一个参数。现在的问题是我似乎无法输入函数。我尝试删除最后一部分,但出现错误 -
cv2.createTrackbar only works with 5 arguements. Only 4 given.
嗯,好吧,看来我不能跳过一部分了。
接下来我尝试了回调但什么也没有。我收到这个错误:-
When used nothing:- NameError: name 'nothing' is not defined
When used callback:- NameError: name 'callback' is not defined
好的,过了一会儿我尝试使用 None。收到此错误:-
TypeError: on_change must be callable
那么如何在不调用函数的情况下使用cv2.createTrackbar
函数呢?
谢谢!
为什么不按预期创建一个简单的函数?
简单的解决方案是定义一个 returns 轨迹栏位置的简单函数。它会在用户移动轨迹栏时被调用,但什么也不会发生。
import cv2
def f(x): return x
win = cv2.namedWindow("MyImage")
tb = cv2.createTrackbar("MyTrackbar","MyImage",0,179,f)
#assume you have some cv2 image already loaded
cv2.imshow("MyImage", img)
您还可以使用匿名 lambda 函数进行回调,如下所示:
import cv2
win = cv2.namedWindow("MyImage")
tb = cv2.createTrackbar("MyTrackbar","MyImage",0,179,lambda x:x)
#assume you have some cv2 image already loaded
cv2.imshow("MyImage", img)
def f():
pass
cv2.createTrackbar('thing', 'other thing', 0, 179, f)
这也行。
我正在尝试对图像进行阈值处理。我使用 cv2.createTrackbar
函数作为-
cv2.createTrackbar('High H','image',0,179, None)
.
现在最后一部分是我遇到的麻烦。在我的代码中,我使用 highH = cv2.getTrackbarPos('High H','image')
获取我的轨迹栏值并在 cv2.inRange
函数中使用它。所以很明显我不需要调用函数作为函数的最后一个参数。现在的问题是我似乎无法输入函数。我尝试删除最后一部分,但出现错误 -
cv2.createTrackbar only works with 5 arguements. Only 4 given.
嗯,好吧,看来我不能跳过一部分了。
接下来我尝试了回调但什么也没有。我收到这个错误:-
When used nothing:- NameError: name 'nothing' is not defined
When used callback:- NameError: name 'callback' is not defined
好的,过了一会儿我尝试使用 None。收到此错误:-
TypeError: on_change must be callable
那么如何在不调用函数的情况下使用cv2.createTrackbar
函数呢?
谢谢!
为什么不按预期创建一个简单的函数?
简单的解决方案是定义一个 returns 轨迹栏位置的简单函数。它会在用户移动轨迹栏时被调用,但什么也不会发生。
import cv2
def f(x): return x
win = cv2.namedWindow("MyImage")
tb = cv2.createTrackbar("MyTrackbar","MyImage",0,179,f)
#assume you have some cv2 image already loaded
cv2.imshow("MyImage", img)
您还可以使用匿名 lambda 函数进行回调,如下所示:
import cv2
win = cv2.namedWindow("MyImage")
tb = cv2.createTrackbar("MyTrackbar","MyImage",0,179,lambda x:x)
#assume you have some cv2 image already loaded
cv2.imshow("MyImage", img)
def f():
pass
cv2.createTrackbar('thing', 'other thing', 0, 179, f)
这也行。