0

我正在尝试使用光流,但没有绘制光流线,而只是点,有什么问题?这是该项目的源代码。通过调试器查看。GDB 总是显示p0.x = p1.x and p0.y = p1.y. 但为什么 ?对不起,我的英语不好。

#include "opencv/cv.h"
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/opencv.hpp>

#include <iostream>
#include <stdio.h>

std::vector<cv::Point2f> corners;
std::vector<cv::Point2f> corners_b;
double qualityLevel = 0.01;
double minDistance = 10;
int blockSize = 3;
bool useHarrisDetector = false;
double k = 0.04;
int maxCorners = 200;
int maxTrackbar = 100;

void MotionDetection(cv::Mat frame1, cv::Mat frame2)
{
    cv::Mat prev, next;
    cvtColor(frame1, prev, CV_BGR2GRAY); 
    cvtColor(frame2, next, CV_BGR2GRAY); 
    goodFeaturesToTrack( prev, 
            corners,
            maxCorners,
            qualityLevel,
            minDistance,
            cv::Mat(),
            blockSize,
            useHarrisDetector,
            k );
    cornerSubPix(prev, 
            corners,
            cvSize( 10, 10 ) ,
            cvSize( -1, -1 ), 
            cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03 ) );
    std::vector<uchar> features_found;
    features_found.reserve(maxCorners);
    std::vector<float> feature_errors;
    feature_errors.reserve(maxCorners);
    calcOpticalFlowPyrLK(prev, next, corners, corners_b, features_found, 
            feature_errors, cvSize( 10, 10 ), 5, cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.3 ), 0);
    IplImage g = next;
    for( int i = 0; i < maxCorners; ++i )
    {
        CvPoint p0 = cvPoint( cvRound( corners[i].x ), cvRound( corners[i].y ) );
        CvPoint p1 = cvPoint( cvRound( corners_b[i].x ), cvRound( corners_b[i].y ) );
        cvLine( &g, p0, p1, CV_RGB(255,0,0), 3, CV_AA );
    }
    cv::Mat rs(&g);
    imshow( "result window", rs );  
    int key = cv::waitKey(5);
}

int main(int argc, char* argv[])
{
    cv::VideoCapture cap(0); 
    if(!cap.isOpened())              
    {
        std::cout<<"[!] Error: cant open camera!"<<std::endl;
        return -1;
    }
    cv::Mat edges;
    cv::namedWindow("result window", 1);
    cv::Mat frame, frame2;
    cap >> frame;
    while(1)
    {
        cap >> frame2;
        MotionDetection(frame, frame2);
    }
    return 0;
}
4

1 回答 1

0

在你的主要功能框架是克隆框架2。我想,那个帽子>> frame2; frame2.copyTo( 框架 ); 而不是 cap >> 框架;就这样

于 2013-05-29T10:59:31.793 回答