Trackbars无法使用python

时间:2014-05-20 23:24:33

标签: python opencv

我正在尝试使用一组轨迹栏控制视频捕获的阈值,这里捕获的每个帧的HSV是代码:

import numpy as np
import cv2 
h_Min=0
h_Max=256
s_Min=0
s_Max=256
v_Min=0
v_Max=256
Frame_Width=640
Frame_Hight=480
Max_DetObj=50
Min_ObjArea=40*40
Max_ObjArea=Frame_Width*Frame_Hight/1.5
def nothing(x):
    pass

def Trackbars():
    cv2.namedWindow('Trackbars',cv2.WINDOW_AUTOSIZE)
    cv2.createTrackbar('H_Min','Trackbars',h_Min,h_Max,nothing)
    cv2.createTrackbar('H_Max','Trackbars',h_Max,h_Max,nothing)
    cv2.createTrackbar('S_Min','Trackbars',s_Min,s_Max,nothing)
    cv2.createTrackbar('S_Max','Trackbars',s_Max,s_Max,nothing)
    cv2.createTrackbar('V_Min','Trackbars',v_Min,v_Max,nothing)
    cv2.createTrackbar('V_Max','Trackbars',v_Max,v_Max,nothing)
def Pics():
calibMod=True
if(calibMod):
    Trackbars()
vc = cv2.VideoCapture(2)
retVal, frame = vc.read();

while True :
    if frame is not None:
        hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
        if (calibMod==True):
            #ret,threshold=cv2.threshold(hsv,0,255,0)
            hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
            threshold=cv2.inRange(hsv,(h_Min,s_Min,v_Min),(h_Max,s_Max,v_Max))
            morphop(threshold)
            cv2.imshow('threshold image',threshold)
            trackObj(threshold,hsv,frame)
        cv2.imshow('camera',frame)
        #cv2.imshow('threshold',threshold)
        cv2.imshow('hsv',hsv)
   rval, frame = vc.read()
    if cv2.waitKey(1) & 0xFF == 27:
        break
cv.DestroyAllWindows()

问题在于没有一个轨迹栏正在工作,当我改变它的值时,门限窗口上没有任何变化 我该怎么办?

1 个答案:

答案 0 :(得分:0)

你要么提供一个有效的回调函数(你的没什么()呢,好吧,..不多......)

或明确查询该值:

 if (calibMod==True):
     #ret,threshold=cv2.threshold(hsv,0,255,0)

     hue_min = cv2.getTrackbarPos('H_Min','Trackbars')
     hue_max = cv2.getTrackbarPos('H_Max','Trackbars')
     sat_min = cv2.getTrackbarPos('S_Min','Trackbars')
     sat_max = cv2.getTrackbarPos('S_Max','Trackbars')
     val_min = cv2.getTrackbarPos('V_Min','Trackbars')
     val_max = cv2.getTrackbarPos('V_Max','Trackbars')

     hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
     threshold=cv2.inRange(hsv,(hue_min,sat_min,val_min),(hue_max,sat_max,val_max))
     ...