使用fast(Opencv)提取特征点后对对象进行分段

时间:2017-02-20 20:50:37

标签: computer-vision

我有这个代码,我设法使用快速提取功能,然后使用Lucas kanade方法跟踪这些点,但现在我想分割并在视频中的每个对象周围绘制一个边界框。任何想法将不胜感激。提前致谢。这是代码。

#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"

using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
    bool addRemovePt = false;
    VideoCapture cap;
    TermCriteria termcrit(TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03);
    Size subPixWinSize(10, 10), winSize(31, 31);
    cap.open(0);
    if (!cap.isOpened())
        return -1;
    namedWindow("Video", 1);
    float scalingFactor = 0.75;
    Mat gray, prevGray, image, frame;
    bool init = true;
    vector<Point2f> Points[2];
    int count = 0;
    while (true) {
        cap >> frame; // capture frame from webcam
        if (frame.empty())
            break;
        resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER_AREA);
        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY); // change to grayscale
        if (init) {
            vector<KeyPoint> keypoints_1;
            Ptr<FastFeatureDetector> fastDetector = FastFeatureDetector::create(20);
            fastDetector->detect(gray, keypoints_1); // detect interest points using fast every 30 frames
            KeyPoint::convert(keypoints_1, Points[1]);
            init = false;
        }
        else if (!Points[0].empty()) {
            vector<uchar> status;
            vector<float> err;

            if (prevGray.empty()) {
                gray.copyTo(prevGray);
                cout << "once" << endl;
            }
            // Lucas-kannade optical flow
            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 (!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);
        }

        imshow("Video", image);
        count++;
        if (count == 15) {
            count = 0;
            init = true;
        }
        if (waitKey(30) >= 0)
            break;
        swap(Points[1], Points[0]);
        swap(prevGray, gray);
    }
    cap.release();
    return 0;
}

This is what i have

This is what i want

0 个答案:

没有答案