0

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

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
xixi
  • 11
  • 2
  • The problem is in your configuration. Be sure to link to debug OpenCV libraries in debug mode, and release libs in release. Also be sure to use OpenCV compiled with the same compiler as your project. Now you havn't solved the problem, it's just hidden – Miki Jul 20 '16 at 10:48
  • Hello Miki, Thanks for your answer, I have find some answers similarly before. I'm not good at configuration, but I'll try to check up it, thanks you. – xixi Jul 21 '16 at 09:17

1 Answers1

0

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.

Community
  • 1
  • 1
Jeremy Gamet
  • 165
  • 7
  • Thanks for answer! I have step in debug again, and I'm sure that GrayImage and BinaryImage are one channel image(http://i.stack.imgur.com/tc00Q.png). The code I paste upon are all codes related, you can find that GrayImage、BinaryImage are new(); except the ColorImage(CV_8UC4) from earlier part. I was want to paste all but it's too long. Thanks again :) – xixi Jul 20 '16 at 12:33