2012-07-25 5 views
4

저는 파이썬 바인딩을 사용하여 opencv 2.4.1을 실행 중이며 광 흐름을 계산하는 데 어려움이 있습니다. 코드의cv2.calcOpticalFlowPyrLK의 입력 이미지에 대한 데이터 유형 오류

특히이 섹션 : 내가 가지고있는 지금

prev_img=prev_saturation_thresh_img 
next_img=saturation_thresh_img 

: 나는 배열을 NumPy와하는 이미지를 변환하려고 그럼

<unknown> is not a numpy array 

:

#calculate the opticalflow 
if prev_saturation_thresh_img==None: 
    prev_saturation_thresh_img=saturation_img 
if i >=0: 
    prev_img=prev_saturation_thresh_img 
    next_img=saturation_thresh_img 
    p1, st, err = cv2.calcOpticalFlowPyrLK(prev_img,next_img,tracks_np,**lk_params) 

오류를 반환 새로운 오류 :

나는 NumPy와 배열로 변환하기 전에 (iplimage에서) cvmat로 이미지를 변환 최후의 필사적 노력의 일환으로

7,397,198,838,799,, 바로 그래서 지금이 붙어있어

error: ..\..\..\OpenCV-2.4.1\modules\video\src\lkpyramid.cpp:607: error: (-215) nextPtsMat.checkVector(2, CV_32F, true) == npoints 

을 어떻게 볼 수 있습니다. 아래 코드는 참조 용으로 전체입니다.

import cv 
import cv2 
import numpy as np 

class Target: 
    def __init__(self): 
     self.capture = cv.CaptureFromFile("raw_gait_cropped.avi") 

    def run(self): 
     #initiate font 
     font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8) 

     #instantiate images 
     img_size=cv.GetSize(cv.QueryFrame(self.capture)) 
     hsv_img=cv.CreateImage(img_size,8,3) 
     saturation_img=cv.CreateImage(img_size,8,1) 
     saturation_thresh_img=cv.CreateImage(img_size,8,1) 
     prev_saturation_thresh_img=None 

     #create params for GoodFeaturesToTrack and calcOpticalFlowPyrLK 
     gftt_params = dict(cornerCount=11, 
          qualityLevel=0.2, 
          minDistance=5, 
          mask=None, 
          useHarris=True 
          ) 

     lk_params = dict( winSize = (15, 15), 
          maxLevel = 2, 
          criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03), 
          flags = cv2.OPTFLOW_USE_INITIAL_FLOW, 
          minEigThreshold=1 
          )  
     tracks=[] 
     writer=cv.CreateVideoWriter("angle_tracking.avi",cv.CV_FOURCC('M','J','P','G'),30,cv.GetSize(hsv_img),1) 

     i=0 
     while True: 
      #grab a frame from the video capture 
      img=cv.QueryFrame(self.capture) 

      #break the loop when the video is over 
      if img == None: 
       break 

      #convert the image to HSV 
      cv.CvtColor(img,hsv_img,cv.CV_BGR2HSV) 

      #Get Saturation channel 
      cv.MixChannels([hsv_img],[saturation_img],[(1,0)]) 

      #Apply threshold to saturation channel 
      cv.InRangeS(saturation_img,145,255,saturation_thresh_img) 

      #locate initial features to track 
      if i==0: 
       eig_image=temp_image = cv.CreateMat(img.height, img.width, cv.CV_32FC1) 
       for (x,y) in cv.GoodFeaturesToTrack(saturation_thresh_img, eig_image, temp_image, **gftt_params): 
        tracks.append([(x,y)]) 
        cv.Circle(saturation_thresh_img,(int(x),int(y)),5,(255,255,255),-1,cv.CV_AA,0) 
       tracks_np=np.float32(tracks).reshape(-1,2) 
       print tracks 

      #calculate the opticalflow 
      if prev_saturation_thresh_img==None: 
       prev_saturation_thresh_img=saturation_img 
      if i >=0: 
      prev_img=prev_saturation_thresh_img 
       next_img=saturation_thresh_img 
       p1, st, err = cv2.calcOpticalFlowPyrLK(prev_img,next_img,tracks_np,**lk_params) 
       prev_saturation_thresh_img=saturation_img 
      i=i+1 
      print i 
      #display frames to users 
      cv.ShowImage("Raw Video",img) 
      cv.ShowImage("Saturation Channel",saturation_img) 
      cv.ShowImage("Saturation Thresholded",saturation_thresh_img) 

      # Listen for ESC or ENTER key 
      c = cv.WaitKey(7) % 0x100 
      if c == 27 or c == 10: 
       break 
     #close all windows once video is done 
     cv.DestroyAllWindows() 



if __name__=="__main__": 
    t = Target() 
    t.run() 

답변

6

OpenCV는 받아 들일 수있는 데이터 형식에 대해 매우 까다로울 수 있습니다. 다음 코드 추출물은 나를 위해 작동 :

prev = cv.LoadImage('images/'+file_list[0]) 
prev = np.asarray(prev[:,:]) 
prev_gs = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY) 

current = cv.LoadImage('images/'+file) 
current = np.asarray(current[:,:]) 
current_gs = cv2.cvtColor(current, cv2.COLOR_BGR2GRAY) 

features, status, track_error = cv2.calcOpticalFlowPyrLK(prev_gs, current_gs, good_features, None,  
**lk_params) 

참고가 [: :] 배열을 NumPy와하는 이미지에서 변환 할 때, 나는 그들이 필요한 것으로 나타났습니다.

이 방법으로 문제가 해결되기를 바랍니다.