OpenCV的calcOpticalFlowPyrLK抛出异常

OpenCV的calcOpticalFlowPyrLK抛出异常

本文介绍了OpenCV的calcOpticalFlowPyrLK抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用OpenCV形成一个小光流示例。一切工作除了函数调用calcOpticalFlowPyrLK,它打印以下失败的断言在控制台窗口:

我要解析的视频被分成300个图像,标记为caml00000.jpeg,caml00001.jpeg,...,caml00299.jpeg。这是我写的代码:

  #include< cv.h> 
#includeopencv2 / highgui / highgui.hpp

使用命名空间cv;
using namespace std;

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

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

string winname =测试窗口;

vector< Mat> imgVec(numFrames);

auto itrImg = begin(imgVec);
auto itrEnd = end(imgVec);
vector< Point2f>特征:
vector< Point2f>特征:

namedWindow(winname,CV_WINDOW_AUTOSIZE);
int fileNum = 0;
while(itrImg!= itrEnd){
Mat& imgRef = * itrImg; //从向量迭代器获取此帧的Mat

//计算文件的名称;
sprintf(buff,fileFormat,fileNum);
string fileName = buff;
// string fileName =kitty.jpg; //尝试使用静态图片
cout<< fileName<< endl

Mat cImage = imread(fileName,CV_LOAD_IMAGE_GRAYSCALE);
cImage.convertTo(imgRef,CV_8U); //也试过CV_8UC1
featuresPrevious = std :: move(featuresCurrent);
goodFeaturesToTrack(imgRef,featuresCurrent,30,0.01,30); //计算用于下一次迭代的特征
if(!imgRef.data){//这从不执行,所以读取文件没有问题
cout< 文件I / O问题! << endl
getchar();
return 1;
}

if(fileNum> 0){
Mat& lastImgRef = *(itrImg-1); //获取最后一帧的图像
vector< Point2f> featuresNextPos;
矢量< char>特征:
vector< int>呃;
calcOpticalFlowPyrLK(lastImgRef,imgRef,featuresPrevious,featuresNextPos,featuresFound,err); //问题行
//绘制连接上一个位置和当前位置的线
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); //不完美,但它会做

++ itrImg;
++ fileNum;
}

waitKey(0);
return 0;
}

我读到的关于这个异常的唯一原因是垫子是不同的格式,但我已经尝试读取静态图像(见上面关于kitty.jpg的代码),我仍然得到相同的失败的断言。任何想法?

解决方案

更改行 vector< char> featuresFound; 矢量< uchar> featuresFound; 向量< int>错误 Mat err;



我不能解释为什么,如何做。



编辑:
正如@Sluki在注释中说 - err矢量必须存储在浮点精度std :: vector或cv :: Mat。


I have been trying to form a small optical flow example with OpenCV for a while now. Everything works except the function call calcOpticalFlowPyrLK, which prints the following failed assertion in the console window:

The video that I'm parsing is separated into 300 images, labelled as "caml00000.jpeg", "caml00001.jpeg", ..., "caml00299.jpeg". Here is the code I wrote:

#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;
}

The only thing I've read about this exception is that it is caused when Mats are in different formats, however I've tried reading a static image (see code above regarding "kitty.jpg") and I still get the same failed assertion. Any ideas?

解决方案

Change line vector<char> featuresFound; to vector<uchar> featuresFound; and vector<int> err; to Mat err;

I can't explain why, but that's how it has to be done.

Edit:As @Sluki said in the comments - err vector has to be stored in floating point precision std::vector or cv::Mat.

这篇关于OpenCV的calcOpticalFlowPyrLK抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:20