OpenCV calcOpticalFlowPyrLK выдает исключение

Я уже некоторое время пытаюсь сформировать небольшой пример оптического потока с OpenCV. Все работает, кроме вызова функции calcOpticalFlowPyrLK, которая печатает следующее неудачное утверждение в окне консоли:

Ошибка OpenCV: утверждение не выполнено (mytype == typ0 || (CV_MAT_CN (mytype) == CV_MAT_CV (type0) && ((1 << type0) & fixedDepthMask)! = 0)) в неизвестной функции, файл …… \ src \ opencv \ modules \ core \ src \ matrix.cpp, строка 1421

Видео, которое я анализирую, разделено на 300 изображений, помеченных как «caml00000.jpeg», «caml00001.jpeg», …, «caml00299.jpeg». Вот код, который я написал:

#include <cv.h>
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;

int main( int argc, char** argv ){

char buff[100];
int numFrames=300;
char fileFormat[]="images/caml%05d.jpeg";

string winname="Test Window";

vector<Mat> imgVec(numFrames);

auto itrImg=begin(imgVec);
auto itrEnd=end(imgVec);
vector<Point2f> featuresPrevious;
vector<Point2f> featuresCurrent;

namedWindow( winname, CV_WINDOW_AUTOSIZE );
int fileNum=0;
while(itrImg!=itrEnd){
Mat& imgRef=*itrImg; //get this frame's Mat from the vector iterator

//Calculate the name of the file;
sprintf(buff,fileFormat,fileNum);
string fileName=buff;
//string fileName="kitty.jpg"; //attempted using a static picture as well
cout << fileName << endl;

Mat cImage=imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
cImage.convertTo(imgRef, CV_8U); //also tried CV_8UC1
featuresPrevious=std::move(featuresCurrent);
goodFeaturesToTrack(imgRef,featuresCurrent,30, 0.01, 30); //calculate the features for use in next iteration
if(!imgRef.data){ //this never executes, so there isn't a problem reading the files
cout << "File I/O Problem!" << endl;
getchar();
return 1;
}

if(fileNum>0){
Mat& lastImgRef=*(itrImg-1); //get the last frame's image
vector<Point2f> featuresNextPos;
vector<char> featuresFound;
vector<int> err;
calcOpticalFlowPyrLK(lastImgRef,imgRef,featuresPrevious,featuresNextPos,featuresFound,err); //problem line
//Draw lines connecting previous position and current position
for(size_t i=0; i<featuresNextPos.size(); i++){
if(featuresFound[i]){
line(imgRef,featuresPrevious[i],featuresNextPos[i],Scalar(0,0,255));
}
}
}

imshow(winname, imgRef);

waitKey(1000/60); //not perfect, but it'll do

++itrImg;
++fileNum;
}

waitKey(0);
return 0;
}

Единственное, что я прочитал об этом исключении, это то, что оно вызывается, когда Маты находятся в разных форматах, однако я попытался прочитать статическое изображение (см. Код выше относительно «kitty.jpg»), и я все еще получаю то же самое ошибочное утверждение , Есть идеи?

3

Решение

Изменить строку vector<char> featuresFound; в vector<uchar> featuresFound; а также vector<int> err; в Mat err;

Я не могу объяснить почему, но это так.

Редактировать:
Как сказал @Sluki в комментариях — вектор ошибки должен храниться в точности с плавающей точкой std :: vector или cv :: Mat.

8

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector