tipo di dati di errori per le immagini in ingresso del cv2.calcOpticalFlowPyrLK

Sto correndo opencv 2.4.1 utilizzando python bindings e sto avendo difficoltà calcolo del flusso ottico.

In particolare, nella sezione di codice:

#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)

Restituisce l’errore:

<unknown> is not a numpy array

Così poi cerco di convertire le immagini di array numpy:

prev_img=prev_saturation_thresh_img
next_img=saturation_thresh_img  

Ora ho un nuovo errore:

<unknown> data type = 17 is not supported

In un ultimo disperato sforzo, ho convertire le immagini in cvmat (iplimage) prima di convertirlo in un array numpy, solo per vedere cosa succede

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

Così ora mi sono bloccato. Di seguito è il codice nella sua interezza per riferimento

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

OriginaleL’autore Chris | 2012-07-25

One Reply
  1. 6

    OpenCV può essere molto schizzinosi riguardo i dati in formati che accetta. Il codice riportato di seguito l’estratto funziona per me:

    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)

    Nota [:,:] quando la conversione da immagini di array numpy, ho trovato che sono necessari.

    Spero che questo possa risolvere il tuo problema.

    OriginaleL’autore hjweide

Lascia un commento