Ask Your Question
0

Lucas Kanade feature tracker

asked 2015-11-17 03:59:06 -0600

updated 2015-11-19 08:49:10 -0600

I've built a new project using the file "lktdemo.cpp" provided in OpenCV folder. It builds successfully but when I run it, it uses the camera of my laptop. The camera of my laptop is ON but 'm unable to record the video as I don't understand its recording. How do I force it to read the video file? At what point, the changes should be made?

when I change the code in the following way, it displays a message that "could not initialize capturing"

Code:

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x, (float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    help();

    VideoCapture cap;
    TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
    Size subPixWinSize(10,10), winSize(31,31);

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    string filename = "seanpenn.avi";

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        //cap.open(argc == 2 ? argv[1][0] - '0' : 0);
        cap.open(filename);
    else if( argc == 2 )
        //cap.open(argv[1]);
        cap.open(filename);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onMouse, 0 );

    Mat gray, prevGray, image, frame;
    vector<Point2f> points[2];

    for(;;)
    {
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<Point2f> tmp;
            tmp.push_back(point);
            cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image);

        char c ...
(more)
edit retag flag offensive close merge delete

Comments

Have you tried to give as parameter the path to your video file?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-17 04:03:59 -0600 )edit
1

usually it's just VideoCapture cap("/path/to/video/file") instead of VideoCapture cap(0)

berak gravatar imageberak ( 2015-11-17 04:19:28 -0600 )edit
1

@berak the code on master is implemented so if you do not specify parameters it uses cap(0); the parameter may be the id of the camera or the path of a video file

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-17 04:50:36 -0600 )edit
2

^^ ah, sorry, ok, did not look at the actual sample. so, ./lkdemo videofile

berak gravatar imageberak ( 2015-11-17 04:57:28 -0600 )edit

How do I use ./lkdemo videofile? I comment the following lines:

if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

and then I used

string filename = "C:/Users/user/Downloads/seanpenn.avi";
    VideoCapture cap(filename);

at line number: 42 and 43. By doing so, I got the message "Could not initialize capturing..." in the command window.

Ayesha Siddique gravatar imageAyesha Siddique ( 2015-11-17 09:16:27 -0600 )edit

Strange... uncomment the lines and delete yours and use ./lkdemo "C:/Users/user/Downloads/seanpenn.avi" (on linux) or lkdemo.exe "C:/Users/user/Downloads/seanpenn.avi" (on Windows). Another way you can use cap.open(filename)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-17 10:14:15 -0600 )edit

I un-cmment all that added lines and in the original file, I added the following lines at line no. 54

string filename = "seanpenn.avi";
    cap.open(filename);

but the problem of that message is still there. I'm very very new to opencv and I don't know that how do I use following lines in my code? `lkdemo.exe "C:/Users/user/Downloads/seanpenn.avi

Ayesha Siddique gravatar imageAyesha Siddique ( 2015-11-17 12:14:44 -0600 )edit

Ok, how do you run the code? Are you using Visual Studio on Windows, or maybe Eclipse or KDevelop on Linux? Anyway, you shall add in the command arguments the path to the video you want and run the original code

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-18 02:22:04 -0600 )edit

I'm using visual studio on windows. Please find edits in the question.

Ayesha Siddique gravatar imageAyesha Siddique ( 2015-11-19 08:32:31 -0600 )edit
1

see, there's a lot of ways to start a program and supply a cmdline arg.

maybe make a link to your exe on the desktop, and then drag a video file onto that ?

berak gravatar imageberak ( 2015-11-19 09:08:41 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-11-22 14:45:30 -0600

Under the guidance of @LBerger, the query is solved as:

Use opencv_ffmpeg300.dll or copy it to your project.exe directory (also provided in LBerger's second last comment),set "needToInit" to true in the code, do necessary changes to the code for reading the file and then build & run your project.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-11-17 03:59:06 -0600

Seen: 1,007 times

Last updated: Nov 22 '15