Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

opencvでオプティカルフローメモ。

$
0
0

明日使うのでメモ。

#include "cv.h"#include "highgui.h"#include <iostream>#include <vector>#ifdef _DEBUG#pragma comment(lib, "opencv_imgproc2410d.lib")#pragma comment(lib, "opencv_core2410d.lib")#pragma comment(lib, "opencv_highgui2410d.lib")#pragma comment(lib, "opencv_video2410d.lib")//Release Mode#else#pragma comment(lib, "opencv_imgproc2410.lib")#pragma comment(lib, "opencv_core2410.lib")#pragma comment(lib, "opencv_highgui2410.lib")#pragma comment(lib, "opencv_video2410.lib")#endifint main(int argc, char** argv)
{
    cv::VideoCapture cap(0);

    constint cycle = 1000;

    cv::Mat prevFrame;
    cv::Size frameSize = prevFrame.size();
    cap >> prevFrame;

    cv::waitKey(cycle);

    while (1) {
        cv::Mat frame;
        cap >> frame;

        cv::Mat prevFrameGray;
        cv::Mat currFrameGray;

        cv::cvtColor(prevFrame, prevFrameGray, CV_RGB2GRAY);
        cv::cvtColor(frame, currFrameGray, CV_RGB2GRAY);

        // 特徴点抽出
        std::vector<cv::Point2f> prevCorners;
        std::vector<cv::Point2f> currCorners;

        cv::goodFeaturesToTrack(prevFrameGray, prevCorners, 20, 0.05, 5.0);
        cv::goodFeaturesToTrack(currFrameGray, currCorners, 20, 0.05, 5.0);
        cv::cornerSubPix(prevFrameGray, prevCorners, cv::Size(21, 21), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 30, 0.01));
        cv::cornerSubPix(currFrameGray, currCorners, cv::Size(21, 21), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 30, 0.01));

        std::vector<uchar> featuresFound;
        std::vector<float> featuresErrors;

        cv::calcOpticalFlowPyrLK(
            prevFrameGray,
            currFrameGray,
            prevCorners,
            currCorners,
            featuresFound,
            featuresErrors);

        for (int i = 0; i < featuresFound.size(); i++) {
            cv::Point p1 = cv::Point((int) prevCorners[i].x, (int) prevCorners[i].y);
            cv::Point p2 = cv::Point((int) currCorners[i].x, (int) currCorners[i].y);
            cv::line(frame, p1, p2, cv::Scalar(0, 0, 255), 2);
        }

        cv::imshow("preview", frame);
        prevFrame = frame;
        if (cv::waitKey(cycle) == 27) { break; }
    }
    return0;
}

Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>