2014-03-30 150 views
-1

我的程序從網絡攝像頭獲取輸入並實時輸出高斯金字塔。該程序運行正常,但是當我退出(通過按一個鍵來觸發waitKey()),我得到一個錯誤:OpenCV:調試斷言失敗(pHead-> nBlockUse)

Debug Assertion Failed! 
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)) 
Line 52: dbgdel.cpp 

我懷疑這是關係到我使用創造了高斯的buildPyramid()功能金字塔。輸出需要一個Mat數組。輸出的地墊數量取決於水平的數量,所以輸出需要是一個指針。我不知道問題是初始化變量還是最終沒有被刪除。我也可能完全不瞭解這個原因。

我正在數組的數組與此:

std::vector<cv::Mat> GPyr; 

,我這個做高斯金字塔:

buildPyramid(imgMatNew, GPyr, levels, BORDER_DEFAULT); 

什麼原因造成的錯誤任何想法?

全部來源:

#include "stdafx.h" 
#include <iostream> 
#include <stdio.h> 

#include "opencv2/core/core.hpp" 
#include "opencv2/flann/miniflann.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/photo/photo.hpp" 
#include "opencv2/video/video.hpp" 
#include "opencv2/features2d/features2d.hpp" 
#include "opencv2/objdetect/objdetect.hpp" 
#include "opencv2/calib3d/calib3d.hpp" 
#include "opencv2/ml/ml.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/contrib/contrib.hpp" 
#include "opencv2/core/core_c.h" 
#include "opencv2/highgui/highgui_c.h" 
#include "opencv2/imgproc/imgproc_c.h" 
#include "opencv2\objdetect\objdetect.hpp" 

using namespace cv; 
using namespace std; 

int main() 
{ 

    CvCapture* capture = 0; 

    // imgMatNew, imgMatOut were used to grab the current frame 
    Mat frame, frameCopy, image, imgMatNew, imgMatOut; 
    std::vector<cv::Mat> GPyr; 

    int levels = 4; 

    capture = cvCaptureFromCAM(CV_CAP_ANY); //0=default, -1=any camera, 1..99=your camera 
    if (!capture) 
    { 
     cout << "No camera detected" << endl; 
    } 

    //cvNamedWindow("result", CV_WINDOW_AUTOSIZE); 
    namedWindow("GPyrOut", WINDOW_AUTOSIZE); 
    namedWindow("imageNew", WINDOW_AUTOSIZE); 

    if (capture) 
    { 
     cout << "In capture ..." << endl; 
     for (;;) 
     { 
      // capture frame from video camera 
      IplImage* iplImg = cvQueryFrame(capture); 
      frame = iplImg; 

      // convert ilpImg into Mat format for easy processing 
      imgMatNew = cvarrToMat(iplImg, 1); 


      // Start Image Processing Here 
      buildPyramid(imgMatNew, GPyr, levels, BORDER_DEFAULT); 

      // Show Window 
      imshow("GPyrOut", GPyr[levels]); //show G Pyr, at a certain level, mex index = levels 
      imshow("imageNew", imgMatNew); //show window 

      if (waitKey(10) >= 0) 
       break; 
     } 
     // waitKey(0); 
    } 

    cvReleaseCapture(&capture); 

    return 0; 
} 

回答

0

所以,有什麼錯在這裏的兩件事情。

a)您必須不是使用opencv過時的c-api,混合使用c和C++調用是通向地獄的直通道路。 b)C++開始索引爲0,最後一個有效索引爲size-1,所以對於4個級別,級別[4]超出界限。請在這種情況下運行調試版本以獲得適當的例外!

這裏的更正後的代碼:

Mat frame, frameCopy, image, imgMatNew, imgMatOut; 
std::vector<cv::Mat> GPyr; 

int levels = 4; 

VideoCapture capture(0); 

if (!capture.isOpened()) 
{ 
    cout << "No camera detected" << endl; 
    return -1; 
} 

//cvNamedWindow("result", CV_WINDOW_AUTOSIZE); 
namedWindow("GPyrOut", WINDOW_AUTOSIZE); 
namedWindow("imageNew", WINDOW_AUTOSIZE); 

cout << "In capture ..." << endl; 
for (;;) 
{ 
    // capture frame from video camera 
    capture.read(frame); 

    // Start Image Processing Here 
    buildPyramid(frame, GPyr, levels, BORDER_DEFAULT); 

    // Show Window 
    imshow("GPyrOut", GPyr[levels-1]); //show last level 
    imshow("imageNew", frame); //show window 

    if (waitKey(10) >= 0) 
     break; 
}