cv2 getTrackbarPos无法正常工作 [英] cv2 getTrackbarPos not working
问题描述
在python 2.7.12中使用opencv3.1作为cv2.我现在遇到的问题是,尽管我遵循多组指令,但这些指令似乎都使用与我本人相同的设置,或者至少使用非常相似的设置.我主要通过以下两个示例进行操作: CodeGenerater的Blogspot教程.我没有忘记做一个回调函数或使用cv2.getTrackbarPos
.我觉得我执行的特定顺序或图像显示循环肯定有问题.这就是我所拥有的,它会使用初始跟踪栏阈值来显示图像,但不会使用跟踪栏回调来更新图像:
Working with opencv3.1 as cv2 in python 2.7.12. The problem I'm having right now is that, though I am following multiple sets of instructions that all seem to use the same setup as myself or at least a very similar one. I am mainly going by these two examples: openCV.org and CodeGenerater's Blogspot tutorial. I did not forget to make a callback function or to use cv2.getTrackbarPos
. I feel there must be something wrong with the specific order I do it in or the image display loop. Here is what I have, It diaplays the image with the initial trackbar threshold, but does not update the image with the trackbar callback:
import cv2
#write simple callback function to pass trackbar position as *arg
def callback(*arg):
pass
#create display window for image
cv2.namedWindow('frame')
#read in image
img = cv2.imread(r'/home/Usr/Documents/Aerial-Images/images_with_targets/Flight_4/target_10.jpg',0)
#instantiate trackbar that goes in our named window and uses callback function
cv2.createTrackbar('thresh2','frame',5,15,callback)
#initialize thresholds
thresh1=11
thresh2=5
#loop really just runs until the escape key causes a break
while(True):
#sets threshold 2 to trackbar position
thresh2=cv2.getTrackbarPos('thresh2','frame')
#apply laplacian filter to ehance edge gradients
th = cv2.Laplacian(img,cv2.CV_8UC1)
#binarize image with adaptive threshold
th = cv2.adaptiveThreshold(th,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,thresh1,thresh2)
#show filtered image
cv2.imshow('frame',th)
#waits for escape key then breaks out of loop
if cv2.waitKey(0) & 0xFF == ord('q'):
break
#close our display window
cv2.destroyallwindows()
推荐答案
#read in image
img = cv2.imread(r'/home/Usr/Documents/Aerial-Images/images_with_targets/Flight_4/target_10.jpg',0)
#instantiate trackbar that goes in our named window and uses callback function
cv2.createTrackbar('thresh2','frame',5,15,callback)
#initialize thresholds
thresh1=11
thresh2=5
这应该属于while循环
This should fall within the while loop
这篇关于cv2 getTrackbarPos无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!