Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8393123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:42:04+00:00 2026-06-09T19:42:04+00:00

I’m running opencv 2.4.1 using python bindings and am having difficulty calculating the optical

  • 0

I’m running opencv 2.4.1 using python bindings and am having difficulty calculating the optical flow.

Specifically this section of code:

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

Returns the error:

<unknown> is not a numpy array

So then I try to convert the images to numpy arrays:

prev_img=prev_saturation_thresh_img
next_img=saturation_thresh_img  

Now I have a new error:

<unknown> data type = 17 is not supported

In a last-ditch effort I convert the images to cvmat (from iplimage) before converting it to a numpy array, just to see what happens

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

So now I’m stuck. Below is the code in it’s entirety for reference

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:42:06+00:00Added an answer on June 9, 2026 at 7:42 pm

    OpenCV can be very picky about the data formats it accepts. The following code extract works for 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)
    

    Note the [:,:] when converting from images to numpy arrays, I have found that they are required.

    I hope that this may solve your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.