我正在修改计算光流的算法。 我想将输入视频(我正在使用文件作为输入)细分为一个理想平方的子视频,以便分别计算光流并将最终结果合并为一个输出。
我已经写了一部分代码,将我的原始黑白帧分成9个子帧,一切正常。在对所有9个子帧进行光流计算迭代并合并结果之前,我想尝试仅在单个子帧(例如左上角)上计算光流。代码可以编译,但是运行时会出现此错误
OpenCV Error: Assertion failed (count >= 0) in cornerSubPix, file
/build/opencv-L2vuMj/opencv-
3.2.0+dfsg/modules/imgproc/src/cornersubpix.cpp, line 58
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-L2vuMj/opencv-
3.2.0+dfsg/modules/imgproc/src/cornersubpix.cpp:58: error: (-215) count
>= 0 in function cornerSubPix
这是代码的核心部分
Mat gray, prevGray, image, frame;
vector<Point2f> points[2];
for(;;)
{
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(image);
cvtColor(image, gray, COLOR_BGR2GRAY);
Mat * cells = subdivide(gray);
Mat * prevCells = subdivide(prevGray);
if( needToInit )
{
// automatic initialization
goodFeaturesToTrack(cells[0], points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
cornerSubPix(cells[0], points[1], subPixWinSize, Size(-1,-1), termcrit);
addRemovePt = false;
needToInit=false;
}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
if(prevCells[0].empty())
cells[0].copyTo(prevCells[0]);
calcOpticalFlowPyrLK(prevCells[0], cells[0], points[0], points[1], status, err, winSize,
3, termcrit, 0, 0.001);
size_t i, k;
for( i = k = 0; i < points[1].size(); i++ )
{
if( addRemovePt )
{
if( norm(point - points[1][i]) <= 5 )
{
addRemovePt = false;
continue;
}
}
if( !status[i] )
continue;
points[1][k++] = points[1][i];
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
}
points[1].resize(k);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
vector<Point2f> tmp;
tmp.push_back(point);
cornerSubPix( cells[0], tmp, winSize, Size(-1,-1), termcrit);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
needToInit = false;
imshow("LK Demo", image);
char c = (char)waitKey(10);
std::swap(points[1], points[0]);
cv::swap(prevCells[0], cells[0]);
}
return 0;
subdivide(Mat input)
是返回Mat数组的函数。
在之前的代码中,如果我写的是gray
而不是cells[0]
和prevGray
而不是prevCells[0]
,那么一切正常。
我用作基础的代码是opencv (link to the page)提供的一个演示
我唯一添加的就是subdivide
函数,并且我从代码中删除了所有允许不需要的选项(例如夜间模式)的部分。
我该怎么解决?