简体   繁体   English

轨迹栏回调python openCV

[英]Trackbar callback python openCV

I have been trying to have a simple window with a trackbar setting the threshold for the binarisation.我一直在尝试创建一个带有轨迹栏的简单窗口,用于设置二值化的阈值。
I have seen several example using the getTrackbarPos method of the trackbar in a while loop, but not many using a callback function, which I would like to use (or at least understand why getTrackbarPos is better)我在 while 循环中看到了几个使用 trackbar 的getTrackbarPos方法的例子,但使用回调函数的例子并不多,我想使用它(或者至少理解为什么getTrackbarPos更好)
I actually see the thresholded image when I move the slider, but it is almost instantaneously replaced by the original image.当我移动滑块时,我实际上看到了阈值图像,但它几乎立即被原始图像取代。 I also tried using a global ImageBin in the callback function but it does not help.我还尝试在回调函数中使用global ImageBin ,但没有帮助。 Someone has a suggestion or can reroute me to some similar issue ?有人有建议或可以将我重新路由到一些类似的问题? Thanks谢谢

import cv2

# callback function
def Update(value):
    print value
    ret,ImageBin = cv2.threshold(Image,value,255,cv2.THRESH_BINARY)
    cv2.imshow('Fenetre',ImageBin)

# window
cv2.namedWindow('Fenetre',cv2.WINDOW_GUI_NORMAL)

# Trackbar 
Slider = cv2.createTrackbar('Threshold','Fenetre',0,255,Update)

# Open image
Home = r'C:\Users\Laurent Thomas\Documents\DataSet\170922110941_BISCHOFF_DORSAL_2ndGO'
Image = cv2.imread(Home + '\WE00001---A001--PO01--LO001--CO6--SL001--PX32500--PW0040--IN0020--TM245--X014262--Y011163--Z216816--T1373979007.tif',0)

# Initialise first view as the normal image
ImageBin = Image[:]


while(1):
    cv2.imshow('Fenetre',ImageBin)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break


cv2.destroyAllWindows()

Modify the Update function as follows:修改Update函数如下:

# callback function
def Update(value):
    global ImageBin
    print (value)
    ret,ImageBin = cv2.threshold(Image,value,255,cv2.THRESH_BINARY)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM