OpenCV 的 calcOpticalFlowPyrLK 抛出异常
OpenCV's calcOpticalFlowPyrLK throws exception
我一直在尝试用OpenCV形成一个小的光流例子一段时间了。除了函数调用calcOpticalFlowPyrLK之外,一切都正常,它在控制台窗口中打印以下失败的断言:
OpenCV错误:Assertion failed (mytype == typ0 || (CV_MAT_CN(mytype) == CV_MAT_CV(type0) &&((1 & lt; & lt;type0),fixedDepthMask) != 0))在未知函数中,文件......srcopencvmodulescoresrcmatrix.cpp,第1421行
我正在解析的视频被分成300个图像,标记为"caml00001.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;
}
我所读到的关于这个异常的唯一一件事是,当Mats以不同的格式引起的,但是我试着读取静态图像(参见上面关于"kitty.jpg"的代码),我仍然得到相同的失败断言。什么好主意吗?
将vector<char> featuresFound;
改为vector<uchar> featuresFound;
, vector<int> err;
改为Mat err;
我不能解释为什么,但这是必须要做的。
编辑:正如@Sluki在评论中所说,err vector必须存储在浮点精度std::vector或cv::Mat中。
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- 函数如何通知用户它基于函数原型抛出异常?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- VisualStudios 会抛出异常,而代码块不会 [C++]
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- ZMQ::send() 抛出异常并终止 QNX 进程.为什么以及如何从中恢复?
- new(std::nothrow) int[n] 抛出异常
- 为什么g_object_set抛出异常(vcruntime140.dll)?
- 你能防止 std::regex 在无效表达式上抛出异常吗?
- C++子线程抛出异常
- 我们应该在抛出异常之前取消分配内存吗
- 有没有一种方法可以让OpenCLC++绑定为所有错误抛出异常
- 为什么访问模板化变体作为返回值抛出异常
- 如何实现,错误分配中止而不是抛出异常
- std::p riority_queue::p op什么时候会抛出异常
- 在0x5914F3BE抛出异常(基于.dll)
- std::vector 默认构造函数可以抛出异常吗?
- 挂钩创建文件抛出异常:读取访问冲突
- getaddrinfo 抛出异常
- 如果文件为c ,如何抛出异常