Ask Your Question
0

'cv::Exception' in calcOpticalFlowFarneback

asked 2016-02-08 08:16:57 -0600

santhoshkelathodi gravatar image

updated 2016-02-10 09:50:29 -0600

berak gravatar image

There is an exception in running the below : When I debugged I found that the code is crashing in the function call calcOpticalFlowFarneback(). I have just run the sample code written below. Could anyone help me solve this issue.I am working in the Qt Creator programming environment -----------------------------------------exception----------------------------------------------------

Starting /home/santhosh/opencv-qt/OpticalFlowDense2/OpticalFlowDense2...
OpenCV Error: Assertion failed (prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyr_scale < 1) in calcOpticalFlowFarneback, file /home/santhosh/opencv/opencv-3.1.0-source/modules/video/src/optflowgf.cpp, line 1102
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/santhosh/opencv/opencv-3.1.0-source/modules/video/src/optflowgf.cpp:1102: error: (-215) prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyr_scale < 1 in function calcOpticalFlowFarneback

The program has unexpectedly finished.

----------------------------Code---------------------------------------------------------------------------

void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, const Scalar& color);

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Mat prev_im,nex_im,flow;

    prev_im=imread("image1.jpg",0);
    if (!prev_im.data)
    {
        QMessageBox msg;
        msg.setText("Could not Load image1");
        msg.exec();
    }
    //namedWindow("previm", cv::WINDOW_AUTOSIZE);
    imshow("previm",prev_im);


    nex_im=imread("image2.jpg",0);
    if (!nex_im.data)
    {
        QMessageBox msg;
        msg.setText("Could not Load image2");
        msg.exec();
    }
    //namedWindow("nextim", cv::WINDOW_AUTOSIZE);
    imshow("nextim",nex_im);


    calcOpticalFlowFarneback(prev_im,nex_im,flow,0.5, 1, 12, 2, 7, 1.5, 0);

    Mat cflow;
    cvtColor(prev_im, cflow, CV_GRAY2BGR);
    drawOptFlowMap(flow, cflow, 10, CV_RGB(0, 255, 0));


    imshow("OpticalFlowFarneback", cflow);


    waitKey(0);
}




void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, const Scalar& color)
{
 for(int y = 0; y < cflowmap.rows; y += step)
 {
      for(int x = 0; x < cflowmap.cols; x += step)
      {
       const Point2f& fxy = flow.at< Point2f>(y, x);
       line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
        color);
       circle(cflowmap, Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), 1, color, -1);

      }
 }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-02-10 09:29:09 -0600

santhoshkelathodi gravatar image

I have fixed this issue. It was found out that the resolution of the image1 and image2 were not matching and because of that this crash was happening. This intuition I got from the error message(prev0.size() == next0.size() && prev0.channels() == next0.channels() && prev0.channels() == 1 && pyr_scale < 1 in function calcOpticalFlowFarneback) and when I gave 2 images of same resolution it worked. Sorry for asking such a silly doubt. :( However I think it will be useful for some amateur opencv user.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-02-08 08:16:57 -0600

Seen: 3,184 times

Last updated: Feb 10 '16