Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple dense optical flow program calcOpticalFlowFarneback() openCV 3.2 cpp

I can't run this simple code. I'm trying to calculate the optical flow between 2 pictures.

Please check the attached pictures

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


    }

Error Picture 1

Error Picture 2

like image 847
サルバドル Avatar asked May 16 '26 19:05

サルバドル


1 Answers

The assertion is due to img1 and img2 having different sizes. In the first image that you gave it can be clearly seen that img1 has 762 rows and img2 has 768 rows.
Try using cv::resize to make img2 of the same size as img1. Add the following line before cv::calcOpticalFlowFarneback().
cv::resize(img2, img2, img1.size());

like image 61
Lakshya Kejriwal Avatar answered May 19 '26 07:05

Lakshya Kejriwal