1. 程式人生 > >【OpenCV筆記】光流法之金字塔Lucas-Kanade

【OpenCV筆記】光流法之金字塔Lucas-Kanade

本文參考連結:https://blog.csdn.net/zy122121cs/article/details/44955353 參考論文:”Pyramidal Implementation of the Lucas Kanade Feature TrackerDescription of the algorithm”一、金字塔光流法介紹

光流金字塔即對影象進行分層處理,一般來說不算原始影象(最底層)的話分為四層就能滿足需求,按照論文中的話說就是超過4層在大多數情況下沒有意義。如果原始影象的大小為640x480,那麼分為4層的大小分別為320x240,160x120,80x60,40x30。 如下圖所示:  

金字塔分層

接下來對金字塔光流法的過程進行簡單描述,期間不會出現任何數學公式,對公式有興趣的小夥伴可以直接搜尋查閱參考文獻的論文。 首先展示一張圖:

金字塔光流的過程

我們對視訊中點的跟蹤實際上是對相鄰兩幀的影象進行處理,設影象I和J為相鄰兩幀的影象,我們希望在影象J中找到u0的對應點v,那麼首先對兩幅影象進行分層,假設如上圖分為3層,如此可以分別計算得到u1、u2、u3。 對於金字塔我們從最高層開始進行處理, u3在影象J中的對應初始點為v31(v31和u3是相等的,圖畫的不太準),然後通過某種計算符合相應的條件後,得到當前層最小誤差點v3n(n表示經過n次計算)和相應的光流。然後利用計算得到的光流能夠在影象J中找到點v21作為第二層的初始點,以此類推進行和第3層一樣的迭代計算最終能夠獲得包含各層光流分量的總光流,就能得到最終的對應點v0r。注:1.某種計算具體見論文。        2.相應條件包含兩種,一是達到設定的迭代次數上限,二是計算結果符合精確度閾值。這在opencv的函式中有體現。

       3.論文中能夠得到一些引數設定資訊,迭代次數一般設定為5次即可(但是opencv中預設為30次),金字塔層數≤4,搜尋窗大小為奇數x奇數。

二、OpenCV金字塔光流函式介紹 函式宣告如下:

CV_EXPORTS_W void calcOpticalFlowPyrLK( InputArray prevImg, InputArray nextImg,
                                        InputArray prevPts, InputOutputArray nextPts,
                                        OutputArray status, OutputArray err,
                                        Size winSize = Size(21,21), int maxLevel = 3,
                                        TermCriteria criteria =                   TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
                                        int flags = 0, double minEigThreshold = 1e-4 );
函式引數介紹
InputArray prevImg 前一幅影象
InputArray nextImg 後一幅影象
InputArray prevPts vector,前一幅影象中想要跟蹤的點集
InputOutputArray nextPts vector,後一幅影象中計算得到的對應點集
OutputArray status vector,記錄狀態,如果對應點的光流被搜尋到則將對應點置1
OutputArray err vector,記錄每個特徵點的誤差,如果光流沒有被計算出來,不會有誤差
Size winSize = Size(21,21) 搜尋窗的大小,如前所述為奇數x奇數
int maxLevel = 3 金字塔的層數
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01) 迭代停止條件,預設設定為30次迭代或者閾值0.01
int flags = 0 預設值為0,表示不設定此標記。有如下標記可以選擇 OPTFLOW_USE_INITIAL_FLOW     = 4, OPTFLOW_LK_GET_MIN_EIGENVALS = 8, OPTFLOW_FARNEBACK_GAUSSIAN   = 256
double minEigThreshold = 1e-4 作為閾值可以過濾掉一些不好的特徵點以提升效能

三、官方例程  

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/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 )
{
    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;

    help();
    cv::CommandLineParser parser(argc, argv, "{@input|0|}");
    string input = parser.get<string>("@input");

    if( input.size() == 1 && isdigit(input[0]) )
        cap.open(input[0] - '0');
    else
        cap.open(input);

    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 = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[0].clear();
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        }

        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
    }

    return 0;
}