简体   繁体   中英

WHY I solved “Debug Assertion Failed OpenCv is_block_type_valid(header->_block_use)”

I have encounter this when I using findCountours() in OpenCV, Debug Assertion Failed I have google a lot but nothing helped me, following is part of my codes.

void HandTrack::ProcessFrame(...){
    ...
    //Convert the colorImage into grayImage
    Mat GrayImage;
    cvtColor(ColorImages, GrayImage, CV_BGR2GRAY);

    //Convert grayImage into binaryImage
    Mat BinaryImage(GrayImage.rows, GrayImage.cols, CV_8UC1);
    threshold(GrayImage, BinaryImage, 254, 255, CV_THRESH_BINARY);
    bitwise_not(BinaryImage, BinaryImage);

    //Get the contours from binaryImage
    vector<vector<Point>> hand_contours;
    findContours(BinaryImage, hand_contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
    BinaryImage.release();

    //Draw the contours
    Mat OutlineImage(GrayImage.rows, GrayImage.cols, CV_8UC1);
    rectangle(OutlineImage, Point(0, 0), Point(BinaryImage.cols, BinaryImage.rows), Scalar(255, 255, 255),-1,8);
    if (hand_contours.size() > 0) {
        drawContours(OutlineImage, hand_contours, -1, (0, 0, 0), 1);
    }

    waitkey(1);
}

Belows is what I've try:

  1. Add imshow("img",BinaryImage); at last, and nothing change;

  2. Comment this line↓, everything goes well

    findContours(BinaryImage, hand_contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

  3. Step through the codes, everything is well until the '}' below the

    waitkey(1); }

  4. Add hand_contours.~vector(); (destruct fuction) before waitkey(1); the Debug Assertion Failed show wherever it are;

At last, I solved it by changing the local variable 'hand_contours' into global variable. But I still wondering why it solved. Thanks for read :)

ignore it,images in debuging

Your problem is somewhere in here:

//Convert the colorImage into grayImage
Mat GrayImage;
cvtColor(ColorImages, GrayImage, CV_BGR2GRAY);

//Convert grayImage into binaryImage
Mat BinaryImage(GrayImage.rows, GrayImage.cols, CV_8UC1);
threshold(GrayImage, BinaryImage, 254, 255, CV_THRESH_BINARY);
bitwise_not(BinaryImage, BinaryImage);

//Get the contours from binaryImage
vector<vector<Point>> hand_contours;

You created your BinaryImage to be CV_8UC1 which is good, but I have a feeling that your GrayImage is not ALWAYS going to be "...an 8-bit single-channel image." as required by the documentation. Somewhere along the way it probably doesn't get truncated correctly.

Your GrayImage was derived from a color image and probably occasionally has some empty channels. Check to make sure both your dst and src Mat's are the correct format (That is 99.9% of the time what an assertion failure is from).

As to how this issue got solved from changing a global? There's really no way to tell without seeing the rest of the code. My best guess is that somehow it caused some of your functions to change the content of your MAT's to the format you intended before arrival at the function you showed us above. But there's really no way to tell without seeing it.

But, moral of the story is as long as you can check your src and dst are correctly formatted you will avoid most Assertion Failures.

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