opencv3光流法的使用

#include <iostream>  
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/ml/ml.hpp>


using namespace cv;
using namespace std;


void duan_OpticalFlow(Mat &frame, Mat & result);
bool addNewPoints();
bool acceptTrackedPoint(int i);


Mat curgray;    // 当前图片
Mat pregray;    // 预测图片
vector<Point2f> point[2];   // point0为特征点的原来位置,point1为特征点的新位置
vector<Point2f> initPoint;  // 初始化跟踪点的位置
vector<Point2f> features;   // 检测的特征
int maxCount = 500;         // 检测的最大特征数
double qLevel = 0.01;   // 特征检测的等级
double minDist = 10.0;  // 两特征点之间的最小距离
vector<uchar> status;   // 跟踪特征的状态,特征的流发现为1,否则为0
vector<float> err;

int main()
{

    Mat matSrc;
    Mat matRst;

    VideoCapture cap(0);
    //int totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);

    // perform the tracking process
    printf("Start the tracking process, press ESC to quit.\n");
    while(cap.isOpened()) {
        // get frame from the video
        cap >> matSrc;
        if (!matSrc.empty())
        {
            duan_OpticalFlow(matSrc, matRst);

        }
        else
        {
            cout << "Error : Get picture is empty!" << endl;
        }
        if (waitKey(1) == 27) break;
    }


    waitKey(0);

    return 0;

}



void duan_OpticalFlow(Mat &frame, Mat & result)
{
    cvtColor(frame, curgray, CV_BGR2GRAY);
    frame.copyTo(result);

    if (addNewPoints())
    {
        goodFeaturesToTrack(curgray, features, maxCount, qLevel, minDist);
        point[0].insert(point[0].end(), features.begin(), features.end());
        initPoint.insert(initPoint.end(), features.begin(), features.end());
    }


    if (pregray.empty())
    {
        curgray.copyTo(pregray);
    }

    calcOpticalFlowPyrLK(pregray, curgray, point[0], point[1], status, err);


    int k = 0;
    for (size_t i = 0; i<point[1].size(); i++)
    {
        if (acceptTrackedPoint(i))
        {
            initPoint[k] = initPoint[i];
            point[1][k++] = point[1][i];
        }
    }


    point[1].resize(k);
    initPoint.resize(k);

    for (size_t i = 0; i<point[1].size(); i++)
    {
        line(result, initPoint[i], point[1][i], Scalar(0, 0, 255));
        circle(result, point[1][i], 3, Scalar(0, 255, 0), -1);
    }


    swap(point[1], point[0]);
    swap(pregray, curgray);


    imshow("Optical Flow Demo", result);
    //waitKey(50);
}


bool addNewPoints()
{
    return point[0].size() <= 10;
}


bool acceptTrackedPoint(int i)
{
    return status[i] && ((abs(point[0][i].x - point[1][i].x) + abs(point[0][i].y - point[1][i].y)) > 2);//检测是否为移动点
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值