Repository: opencv
Issue: Memory corruption in computing optical flow
Labels: bug,category: video,incomplete

##### System information (version)
- OpenCV => 3.1.0
- Operating System / Platform => Ubuntu 16.04
- Compiler => cmake (3.14.0) , g++ (5.4.0), C++14

##### Detailed description

Looks like there is a problem with `cv::calcOpticalFlowPyrLK`. I checked and made sure that the inputs (prev image, next image, prev pts, next pts) are in fact valid.
This is in fact the line that invokes the above function.

```c++
  const double win_size = 30.0;
  const int klt_max_iter = 30;
  const double klt_eps = 0.001;  
  vector<uchar> status;
  vector<float> error;
  cv::TermCriteria termcrit(cv::TermCriteria::COUNT+cv::TermCriteria::EPS,
                            klt_max_iter, klt_eps);

  cv::calcOpticalFlowPyrLK(img_ref, img_cur,
                           points_ref, points_cur,
                           status, error,
                           cv::Size2i(win_size, win_size),
                           4, termcrit, cv::OPTFLOW_USE_INITIAL_FLOW);
```
The exact backtrace from gdb is

```bash
(gdb) bt
#0  0x00007ffff6112428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff611402a in __GI_abort () at abort.c:89
#2  0x00007ffff61547ea in __libc_message (do_abort=do_abort@entry=2, fmt=fmt@entry=0x7ffff626ded8 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007ffff615d37a in malloc_printerr (ar_ptr=<optimized out>, ptr=<optimized out>, str=0x7ffff626e030 "free(): invalid next size (normal)", action=3) at malloc.c:5006
#4  _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) at malloc.c:3867
#5  0x00007ffff616153c in __GI___libc_free (mem=<optimized out>) at malloc.c:2968
#6  0x00007ffff4d33faa in void cv::pyrDown_<cv::FixPtCast<unsigned char, 8>, cv::PyrDownVec_32s8u>(cv::Mat const&, cv::Mat&, int) () from /usr/local/lib/libopencv_imgproc.so.3.1
#7  0x00007ffff4d3bbd3 in cv::pyrDown(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int> const&, int) () from /usr/local/lib/libopencv_imgproc.so.3.1
#8  0x00007ffff5426d72 in cv::buildOpticalFlowPyramid(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, int, bool, int, int, bool) () from /usr/local/lib/libopencv_video.so.3.1
#9  0x00007ffff542b2b6 in cv::calcOpticalFlowPyrLK(cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputOutputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::Size_<int>, int, cv::Ter
mCriteria, int, double) () from /usr/local/lib/libopencv_video.so.3.1
#10 0x00007ffff7b9717c in svo::initialization::trackKlt(boost::shared_ptr<svo::Frame>, boost::shared_ptr<svo::Frame>, std::vector<cv::Point_<float>, std::allocator<cv::Point_<float> > >&, std::vector<cv::Point_<float>, std::allocat
or<cv::Point_<float> > >&, std::vector<Eigen::Matrix<double, 3, 1, 0, 3, 1>, std::allocator<Eigen::Matrix<double, 3, 1, 0, 3, 1> > >&, std::vector<Eigen::Matrix<double, 3, 1, 0, 3, 1>, std::allocator<Eigen::Matrix<double, 3, 1, 0, 
3, 1> > >&, std::vector<double, std::allocator<double> >&) () from /home/kv/slam/svo/svvo/lib/libsvo.so
#11 0x00007ffff7b9801d in svo::initialization::KltHomographyInit::addSecondFrame(boost::shared_ptr<svo::Frame>) () from /home/kv/slam/svo/svvo/lib/libsvo.so
#12 0x00007ffff7b67849 in svo::FrameHandlerMono::processSecondFrame() () from /home/kv/slam/svo/svvo/lib/libsvo.so
#13 0x00007ffff7b69d63 in svo::FrameHandlerMono::addImage(cv::Mat const&, double) () from /home/kv/slam/svo/svvo/lib/libsvo.so
#14 0x0000000000408709 in svo::BenchmarkNode::runFromFolder() ()
#15 0x0000000000406a85 in main ()
(gdb) 
```

Also, to note:
This problem arises when I try to compute optical flow at the 3rd time step i.e, between `frame 0` and `frame 2`. Since there was very little disparity between `frame 0` and `frame 1`, I had to compute optical flow for `frame 0` and `frame 2` to ensure there is enough disparity.

Also, here is the [pastebin link](https://pastebin.com/92B00TkB) for a more detailed backtrace.