gpt4 book ai didi

python - cv2.createTrackbar 将 userdata 参数传递给回调

转载 作者:太空宇宙 更新时间:2023-11-03 21:34:19 24 4
gpt4 key购买 nike

我阅读了有关 cv2.createTrackbar 的文档.据说

onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.

但我不知道如何将用户数据传递到 Python 中的 onChange 回调中。

我定义了我的回调函数:

def callback(value,cur_img):
cv2.GaussianBlur(cur_img, (5, 5), value)

我得到了错误:

callback() takes exactly 2 arguments (1 given)

因为它只将 bar 值参数传递给回调。
但我真的需要 cv2.GaussianBlur 函数的 cur_img。如何将 cur_img 参数传递给回调?

最佳答案

您可以使用 lambda 表达式作为回调函数来创建函数闭包。在类中包装播放器时我遇到了同样的问题,并且想使用我的类实例的属性。下面是类方法的片段。

def play(self):
... other code...
cv2.namedWindow('main', cv2.WINDOW_NORMAL)
cv2.createTrackbar('trackbar', 'main', 0,
100,
lambda x: self.callback(x, self)
)
... other code...

def callback(tb_pos, self):
#tb_pos receives the trackbar position value
#self receives my class instance, but could be
#whatever you want

关于python - cv2.createTrackbar 将 userdata 参数传递给回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40680795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com