简单的密集光流程序 calcOpticalFlowFarneback() openCV 3.2 cpp

Posted

技术标签:

【中文标题】简单的密集光流程序 calcOpticalFlowFarneback() openCV 3.2 cpp【英文标题】:simple dense optical flow program calcOpticalFlowFarneback() openCV 3.2 cpp 【发布时间】:2017-04-18 22:03:48 【问题描述】:

我无法运行这个简单的代码。我正在尝试计算 2 张图片之间的光流。

请查看附件图片

OpenCV 错误:断言失败 (prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyrScale_

#include <Windows.h>
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2/video/tracking.hpp"
#include <vector>
#include <stdio.h>
#include <Windows.h>    
#include <iostream>
    using namespace std;
    using namespace cv;

    // Display the results of the matches
    //
    int main(int argc, char* argv[])
    
        cv::Mat img1 = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
        cv::Mat img2 = cv::imread(argv[2], cv::IMREAD_GRAYSCALE);
        cv::Mat res;

        cv::calcOpticalFlowFarneback(img1,img2,img1,.4,1,12,2,8,1.2, 0);

        cv:imshow("cat", res);
        cv::waitKey(0);


    

【问题讨论】:

【参考方案1】:

断言是由于img1img2 具有不同的大小。在您提供的第一张图片中,可以清楚地看到 img1 有 762 行,img2 有 768 行。 尝试使用cv::resize 使img2 的大小与img1 相同。在cv::calcOpticalFlowFarneback()之前添加以下行。cv::resize(img2, img2, img1.size());

【讨论】:

【参考方案2】:

感谢肖申克

最终版本的代码是:

#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2/video/tracking.hpp"
#include <vector>
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
using namespace cv;

// Display the results of the matches
//
int main(int argc, char* argv[])

    cv::Mat res, img1, img2, img2Original, img2OriginalC;
    cv::VideoWriter writer;

    cv::VideoCapture cap;
    cap.open(std::string(argv[1]));
    //cv::cap.open(0);


    cv::namedWindow("cat", cv::WINDOW_AUTOSIZE);

    cap >> img1;
    cvtColor(img1, img1, COLOR_BGR2GRAY);
    double fps = cap.get(cv::CAP_PROP_FPS);
    cv::Size tamano((int)cap.get(cv::CAP_PROP_FRAME_WIDTH), (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT));
    writer.open("mouse.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, tamano);

    for (;;) 
        cap >> img2;
        if (img2.empty()) break;

        img2.copyTo(img2OriginalC);
        cvtColor(img2, img2, COLOR_BGR2GRAY);
        img2.copyTo(img2Original);
        cv::calcOpticalFlowFarneback(img1, img2, res, .4, 1, 12, 2, 8, 1.2, 0);
        for (int y = 0; y < img2.rows; y += 5) 
            for (int x = 0; x < img2.cols; x += 5)
            
                // get the flow from y, x position * 3 for better visibility
                const Point2f flowatxy = res.at<Point2f>(y, x) * 1;
                // draw line at flow direction
                line(img2OriginalC, Point(x, y), Point(cvRound(x + flowatxy.x), cvRound(y + flowatxy.y)), Scalar(255, 0, 0));
                // draw initial point
                circle(img2OriginalC, Point(x, y), 1, Scalar(0, 0, 0), -1);
            
        
        img2Original.copyTo(img1);
        imshow("cat", img2OriginalC);
        //writer << img2OriginalC;    
        if (cv::waitKey(1) == 27) break;
    
    cap.release();
    return 0;

显示实际代码的视频: https://www.youtube.com/watch?v=rfuP-z2OR8I

【讨论】:

以上是关于简单的密集光流程序 calcOpticalFlowFarneback() openCV 3.2 cpp的主要内容,如果未能解决你的问题,请参考以下文章

Lucas Kanade 密集光流

两帧之间的密集光流

有没有办法在opencv c ++中将密集光流转换为稀疏光流?

Brox 密集光流跟踪器错误“EFilterIncompatibleSize”

如何在openCV python中将背景减法与密集光流跟踪相结合

JavaCV 中的密集光流 (DualTVL1)