OpenCV:调试断言失败(pHead-> nBlockUse)

时间:2014-03-30 17:18:28

标签: c++ opencv gaussian

我的程序是从网络摄像头获取输入并实时输出高斯金字塔。该程序运行正常,但当我退出(通过按一个键来触发waitKey())时,我收到一个错误:

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

1 个答案:

答案 0 :(得分:0)

所以,这里有两件事情不对。

a)你必须使用opencv过时的c-api,混合c和c ++调用是通往地狱的直接道路。

b)c ++开始索引为0,最后一个有效索引为size-1,因此对于4个级别,level [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;
}