2013-03-25 83 views
3

我一直在試圖用OpenCV形成一個小光流示例一段時間。一切正常,除了函數調用calcOpticalFlowPyrLK,打印以下斷言失敗在控制檯窗口:OpenCV的calcOpticalFlowPyrLK拋出異常

OpenCV Error: Assertion failed (mytype == typ0 || (CV_MAT_CN(mytype) == CV_MAT_CV(type0) && ((1 << type0) & fixedDepthMask) != 0)) in unknown function, file ......\src\opencv\modules\core\src\matrix.cpp, line 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」),我仍然得到同樣失敗的斷言。有任何想法嗎?

回答

8

行更改vector<char> featuresFound;vector<uchar> featuresFound;vector<int> err;Mat err;

我無法解釋爲什麼,但是這就是它有許多工作要做。

編輯: 由於@Sluki在評論中說 - err向量必須存儲在浮點精度std :: vector或cv :: Mat中。

+0

「vector err;」到「Mat err;」和「vector featuresFound;」以「矢量 featuresFound;」成功了!非常感謝你。 – Suedocode 2013-03-26 22:06:22

+3

錯誤是用浮點精度計算的,所以你不能使用int來存儲它,但使用矢量的作品以及Mat – sluki 2013-10-23 13:05:25