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

simple dense optical flow program calcOpticalFlowFarneback() openCV 3.2 cpp

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

请查看附件图片

OpenCV Error: Assertion failed (prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyrScale_ < 1) in cv::`anonymous-namespace'::FarnebackOpticalFlowImpl::calc, file C:\Users\krato\Desktop\OpenCV\opencv-master\modules\video\src\optflowgf.cpp, line 1114

#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);


    }

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

感谢肖申克

代码的最终版本是:

#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