cv2.calcOpticalFlowPyrLK的输入图像的数据类型错误

时间:2012-07-25 03:14:47

标签: python opencv opticalflow

我正在使用python绑定运行opencv 2.4.1,并且很难计算光流。

特别是这部分代码:

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

返回错误:

<unknown> is not a numpy array

然后我尝试将图像转换为numpy数组:

prev_img=prev_saturation_thresh_img
next_img=saturation_thresh_img  

现在我遇到了一个新错误:

<unknown> data type = 17 is not supported

在最后的努力中,我将图像转换为cvmat(来自iplimage),然后将其转换为numpy数组,只是为了看看会发生什么

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

1 个答案:

答案 0 :(得分: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数组时,我发现它们是必需的。

我希望这可以解决您的问题。