【问题标题】:OpenCV's calcOpticalFlowPyrLK throws exceptionOpenCV 的 calcOpticalFlowPyrLK 抛出异常
【发布时间】:2013-03-15 10:12:41
【问题描述】:

一段时间以来,我一直在尝试使用 OpenCV 构建一个小的光流示例。除了函数调用 calcOpticalFlowPyrLK 之外,一切正常,它在控制台窗口中打印以下失败的断言:

OpenCV 错误:断言失败 (mytype == typ0 || (CV_MAT_CN(mytype) == CV_MAT_CV(type0) && ((1

我正在解析的视频分为 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;
}

我读到的关于这个异常的唯一信息是它是在 Mats 采用不同格式时引起的,但是我尝试读取静态图像(参见上面关于“kitty.jpg”的代码),我仍然得到同样失败的断言。有什么想法吗?

【问题讨论】:

    标签: c++ opencv assertions opticalflow


    【解决方案1】:

    vector&lt;char&gt; featuresFound; 改为vector&lt;uchar&gt; featuresFound;vector&lt;int&gt; err; 改为Mat err;

    我无法解释原因,但必须这样做。

    编辑: 正如@Sluki 在 cmets 中所说 - 错误向量必须存储在浮点精度 std::vector 或 cv::Mat 中。

    【讨论】:

    • "向量 错误;"到“犯错;”和 "vector featuresFound;"到“vector featuresFound;”工作!非常感谢。
    • 错误是用浮点精度计算的,所以你不能使用 int 来存储它,但是使用 vector 和 Mat 一样有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 2013-05-24
    • 2013-06-18
    • 2020-11-18
    相关资源
    最近更新 更多