简体   繁体   中英

OpenCV: Debug Assertion Fail (pHead->nBlockUse)

My program is getting an input from the webcam and outputting the Gaussian Pyramid in real time. The program runs fine, but when I exit (by pressing a key to trigger the waitKey()), I get an error:

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

I suspect this is related to the buildPyramid() function I am using to create the Gaussian Pyramid. The output requires an Array of Mat. The number of mats that are output depends on the number of levels, so the output needs to be a pointer. I don't know if the problem is with initializing the variable or if it doesn't get deleted at the end. I could also just be completely off about the cause.

I am making the Array of Arrays with this:

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

and I am making the Gaussian Pyramid with this:

buildPyramid(imgMatNew, GPyr, levels, BORDER_DEFAULT);

Any ideas for what is causing the error?

Full Source:

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

so, there's 2 things wrong here.

a) you must not use opencv's outdated c-api, mixing c and c++ calls is the straight road to hell.

b) c++ starts indexing at 0, and the last valid index is size-1, so for 4 levels, levels[4] is out of bounds. please run a debug build to get proper exceptions in this case !

here's the corrected code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM