使用Lucas-Kanade的光流

时间:2015-08-13 16:27:28

标签: c++ opencv tracking

目前,我正在使用Vibe来存储轮廓数据,并且在计算每个轮廓的质心之后,启动Lucas-Kanade跟踪器。启动工作正常,但问题在于病房之后的跟踪。如果我手动初始化点,那么跟踪器工作正常。但是,如果我通过轮廓质心(fbseparat)向量初始化它,它就不会跟踪。我附上了Lucas-Kanade代码。

for (int m = 1; m <= frameNumber; m++)
{
    //featuresCurrent.clear();
    cap >> frame;
    if (frame.empty())
        break;

    if (!(fbseparat[m][0].x == 0) && !(fbseparat[m][0].y == 0)) {
        addRemovePt = true;
    }


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



    //if( needToInit )
    //{
    // automatic initialization
    goodFeaturesToTrack(gray, featuresCurrent, MAX_COUNT, ..)
        cornerSubPix(gray, featuresCurrent, subPixWinSize, termcrit)

        addRemovePt = false;
    //}  
    if (!featuresPrevious.empty())
    {
        buildOpticalFlowPyramid(gray, pyramid, winSize, 500, bool...)
            vector<uchar> status;
        vector<float> err;
        if (prevGray.empty())
            gray.copyTo(prevGray);
        calcOpticalFlowPyrLK(prevGray, gray, featuresPrevious,

            featuresCurrent, status, err, winSize, 3, termcrit, 0, 0.001);
        size_t i, k;

        cout << "Point 1 initial size:" << featuresCurrent.size() << "\n";
        for (i = k = 0; i < featuresCurrent.size(); i++)
        {
            if (addRemovePt)
            {
                if (norm(fbseparat[m][i] - featuresCurrent[i]) <= 5)
                {
                    addRemovePt = false;
                    continue;
                }
            }

            if (!status[i])
                continue;

            featuresCurrent[k++] = featuresCurrent[i];

            circle(image, featuresCurrent[i], 3, Scalar(0, 255, 0), -1, 8);
        }

        featuresCurrent.resize(k);

    }


    cout << "Frame Number:" << m;
    cout << "Point 1 size: " << featuresCurrent.size() << "\n";
    cout << "Point 0 size: " << featuresPrevious.size() << "\n";

    if (addRemovePt && featuresCurrent.size() < (size_t)MAX_COUNT)
    {
        for (int j = 0; j <= countoursinframe[m]; j++)
        {
            vector<Point2f> tmp;

            tmp.push_back(fbseparat[m][j]);
            cornerSubPix(gray, tmp, winSize, Size(-1, -1), termcrit);
            featuresCurrent.push_back(tmp[0]);
            addRemovePt = false;

        }

    }

    //needToInit = false;
    imshow("LK Win", image);

    char c = (char)waitKey(10);
    if (c == 27)
        break;
    switch (c)
    {
        // case 'r':
        //     //needToInit = true;
        //     break;
    case 'c':
        featuresPrevious.clear();
        featuresCurrent.clear();
        break;
    }
}

0 个答案:

没有答案