When I create an image pyramid from each of the two images and try to perform an optical flow using the calcOpticalFlowFarneback method, I get the following error.
OpenCV(3.4.3) Error: Assertion failed (0 <= i && i < (int)v.size()) in cv::debug_build_guard::InputArray::getMat, file
D:\opencv343\sources\modules\core\src\matrix_wrap.cpp, line 88 OpenCV:
terminate handler is called! The last OpenCV error is: OpenCV(3.4.3)
Error: Assertion failed (0 <= i && i < (int)v.size()) in
cv::debug_build_guard::InputArray::getMat, file
D:\opencv343\sources\modules\core\src\matrix_wrap.cpp, line 88
Code
// The header file only includes iostream, vector, opencv2.hpp and uses std and cv namespaces
#include "header.h"
void tracking(const Mat& inOldImg, const Mat& inNewImg) {
const int maxLevel = 5;
vector<Mat> oldImgPyr;
buildOpticalFlowPyramid(inOldImg, oldImgPyr, Size(3, 3), maxLevel, false);
vector<Mat> newImgPyr;
buildOpticalFlowPyramid(inNewImg, newImgPyr, Size(3, 3), maxLevel, false);
Mat flow(inOldImg.size(), CV_32FC2);
calcOpticalFlowFarneback(oldImgPyr, newImgPyr, flow, 0.25, maxLevel, 15, 3, 5, 1.2, 0);
return;
}
int main() {
string oldImgPath = "old.bmp";
string newImgPath = "new.bmp";
Mat oldImg = imread(oldImgPath, 0);
Mat newImg = imread(newImgPath, 0);
tracking(oldImg, newImg);
return 0;
}
What I tried.
- Flow vectors were saved in flow when run with a single image instead of an image pyramid.
- The same error occurred when the scale parameter was set to 0.5.
- The same error occurred when 0 was specified as the level of the pyramid.
- I updated my OpenCV version to 3.4.13 and still had the same problem.
Environment
OpenCV: 3.4.3
VisualStudio: 2019
Update
Referring to the OpenCV reference, calcOpticalFlowFarneback requires an 8-bit single-channel input image, while calcOpticalFlowPyrLK requires either an 8-bit single-channel image or an image pyramid. Does this mean that calcOpticalFlowFarneback internally converts the image to a pyramid image?
Description of the input and output of calcOpticalFlowFarneback in the reference
prev first 8-bit single-channel input image.
next second input image of the same size and the same type as prev.
Description of the input and output of calcOpticalFlowPyrLK in the reference
prevImg first 8-bit input image or pyramid constructed by buildOpticalFlowPyramid.
nextImg second input image or pyramid of the same size and the same type as prevImg.