Ask Your Question
0

How to store the output "point2f point[0]" for future use in lkdemo file of opencv?

asked 2015-11-28 07:10:13 -0600

In lkdemo file of opencv, the points with x,y co-ordinates) were saved in "Point2f point" and on each iteration or in every part of the loop, points[0] is updated with x and y coordinates. I want to store points of each iteration in a memory block or at one place so that I can use these points later. How can I do that? I tried it by coping the points to a float 2D array in this way:

in every part of loop, copy all points to the first column of a 2D array then, in next part of loop, copy all points to the second column of a 2D array then, . . . . go up to last frame in this way.

but the problem is that point2f point can't be copied to a float 2D array or in other words, its not allowed in opencv.

Any ideas?

edit retag flag offensive close merge delete

Comments

3

since there will be different number of points in each iteration, you can't use a 2d Mat for this (which requires equal length for each row)

rather use a vector<vector<Point2f>> allpoints , and allpoints.push_back(points[0]); to store them

berak gravatar imageberak ( 2015-11-28 07:56:12 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-11-28 11:13:03 -0600

LBerger gravatar image

updated 2015-11-28 11:25:39 -0600

like @berak said and using lkdemo.cpp and using @LorenaGdL answer it works and using this video

int main(int argc, char* argv[])
{
    VideoCapture cap;

    string filename1 = "baby_mp4.mp4";  

    cap.open(filename1);

    if (!cap.isOpened())
    {
        cout << "Could not initialize capturing for camera...\n";
        return 0;
    }
    cout<< "\nCAP_PROP_POS_AVI_RATIO "<< cap.get(CAP_PROP_POS_AVI_RATIO);
    cout<< "\nCAP_PROP_POS_FRAMES "<< cap.get(CAP_PROP_POS_FRAMES);
    cout<< "\nCAP_PROP_FPS "<< cap.get(CAP_PROP_FPS);
int fourcc = cap.get(CAP_PROP_FOURCC);

string fourcc_str = format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
cout << "\n--->CAP_PROP_FOURCC: " << fourcc_str << endl;

    cout<< "\nCAP_PROP_FRAME_COUNT "<< cap.get(CAP_PROP_FRAME_COUNT);
    cout<< "\nCAP_PROP_FORMAT "<< cap.get(CAP_PROP_FORMAT);
    cout<< "\nCAP_PROP_MODE "<< cap.get(CAP_PROP_MODE);
    cout<< "\nCAP_PROP_FORMAT "<< cap.get(CAP_PROP_FORMAT);
    cout<< "\nCAP_PROP_POS_MSEC "<< cap.get(CAP_PROP_POS_MSEC);

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

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


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

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

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);
        pointsFrame.push_back(points[1]);
        needToInit=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;

            if( !status[i] )
                continue;

            points[1][k++] = points[1][i];
            circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            putText(image,format("%d",i),points[1][i],1,1,Vec3b(0,0,0));
        }
        points[1].resize(k);
         pointsFrame.push_back(points[1]);
   }


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

    char c = (char)waitKey(10);
    if( c == 27 )
        break;

    std::swap(points[1], points[0]);
    cv::swap(prevGray, gray);
}
cout << "\nFrame : " << pointsFrame.size();
for (int i=0;i<pointsFrame.size();i++)
    cout <<"\nFrame "<<i<<"--> points :"<< pointsFrame[i].size();
for (int i=0;i<pointsFrame.size();i++)
    cout <<"\nFrame "<<i<<"--> points 143 :"<< pointsFrame[i][143];
return 0;

}
edit flag offensive delete link more

Comments

add following lines next to "size_t i, k;"

        for (i = k = 0; i < points[1].size(); i++)
        {
Ayesha Siddique gravatar imageAyesha Siddique ( 2015-11-30 13:27:43 -0600 )edit

@LBerger can you eplain a little here. http://answers.opencv.org/question/77804/how-do-i-smooth-feature-trajectories-of-lkdemo/

Ayesha Siddique gravatar imageAyesha Siddique ( 2015-11-30 14:34:23 -0600 )edit

@LBerger and @berak How can I copy the values in points[1] to the columns of a new matrix?

Ayesha Siddique gravatar imageAyesha Siddique ( 2016-07-28 02:58:24 -0600 )edit

@berak...????????

Ayesha Siddique gravatar imageAyesha Siddique ( 2016-07-29 05:44:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-11-28 07:10:13 -0600

Seen: 818 times

Last updated: Nov 28 '15