0

在 python 2.7.12 中使用 opencv3.1 作为 cv2。我现在遇到的问题是,尽管我遵循多组指令,这些指令似乎都使用与我自己相同的设置或至少非常相似的设置。我主要看这两个例子:openCV.org和CodeGenerater的 Blogspot 教程。我没有忘记制作回调函数或使用cv2.getTrackbarPos. 我觉得我执行此操作的特定顺序或图像显示循环一定有问题。这是我所拥有的,它使用初始轨迹栏阈值显示图像,但不使用轨迹栏回调更新图像:

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()
4

2 回答 2

0

答案真的很简单。在查看我编写的一些旧代码后,我意识到我需要将等待键从 0 更改为 1:

if cv2.waitKey(0) & 0xFF == ord('q'):
    break

变成了

if cv2.waitKey(1) & 0xFF == ord('q'):
    break

我没有看到的是我忘记了 Camel Case cv2.destroyAllWindows,这让我认为显示循环仍在运行,而实际上并非如此。

于 2016-09-30T17:46:40.077 回答
0
#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循环

于 2019-11-11T01:55:13.723 回答