-1

I'm having a problem with OpenCV findContours when running. I don't quite understand what the error is. During building, there is no error.

Here is the error message:

[Error when run]1

Here is my code:

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    VideoCapture cap(0); //capture the video from web cam

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }

    namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

    int iLowH = 0;
    int iHighH = 179;

    int iLowS = 0;
    int iHighS = 255;

    int iLowV = 0;
    int iHighV = 255;

    //Create trackbars in "Control" window
    cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
    cvCreateTrackbar("HighH", "Control", &iHighH, 179);

    cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
    cvCreateTrackbar("HighS", "Control", &iHighS, 255);

    cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
    cvCreateTrackbar("HighV", "Control", &iHighV, 255);

    while (true)
    {
        Mat imgOriginal;

        bool bSuccess = cap.read(imgOriginal); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        Mat imgHSV;

        cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

        Mat imgThresholded;

        inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

                                                                                                      //morphological opening (remove small objects from the foreground)
        erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        //morphological closing (fill small holes in the foreground)
        dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;

        findContours(imgThresholded, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

        imshow("Thresholded Image", imgThresholded); //show the thresholded image
        imshow("Original", imgOriginal); //show the original image

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }

    return 0;

}
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Post minimal code to reproduce, this is impossible to answer like this. From the error it looks like you're accessing memory which you shouldn't, maybe you didn't allocate some required input. Like in http://stackoverflow.com/questions/1102123/debug-assertion-failed-expression-block-type-is-valid – stijn Nov 19 '16 at 14:38
  • What is the full name of the OpenCV library you link with? Did you compile your application in Debug or Release mode in this case? – Dan Mašek Nov 19 '16 at 15:05
  • @DanMašek i compile in debug mode. List of library: opencv_core2413d.lib opencv_imgproc2413d.lib opencv_highgui2413d.lib opencv_ml2413d.lib opencv_video2413d.lib opencv_features2d2413d.lib opencv_calib3d2413d.lib opencv_legacy2413d.lib opencv_objdetect2413d.lib opencv_contrib2413d.lib opencv_flann2413d.lib – Adib Idzam Nov 20 '16 at 07:54
  • @AdibIdzam OK. Next question, did you build your own OpenCV binaries? Or did you get some precompiled ones? If so, what compiler were they built with/intended for? MSVS 2015 has Visual C++ 14 (`vc14`) by default. `vc12` was in MSVS2013, `vc11` in MSVS2012. AFAIK the official download package of 2.4.13 only has binaries for vc11 and vc12. – Dan Mašek Nov 20 '16 at 15:49
  • @DanMašek i used vc11 from the package 2.4.13 – Adib Idzam Nov 21 '16 at 04:23
  • @AdibIdzam There's your reason for the errors then. – Dan Mašek Nov 21 '16 at 16:23

2 Answers2

0

Actually, this is a known compatibility error between VS2013, VS2015 and OpenCV. The temporary solution worked for me is to add the 2 lines shown below before return.

cv::imshow("img", threshold);
cv::destroyAllWindows();
Chem-man17
  • 1,700
  • 1
  • 12
  • 27
Tom
  • 1
  • Dan Masek , i thanks for the improvement. Also @Tom, thanks that helped to remove the error for findContours. any reason for the addition line? – Adib Idzam Jan 08 '17 at 13:44
-1

I also met these mistakes, but when developing on visual studio 2010. and spent a lot of time with them. In the end I found that the reason for this is just that I used opencv - lib instead of vc10 (for vs2010) vc14 (for vs2015). Changing the opencv-lib corrects the error. Build of vc10 - opencv lib also costs me a lot of time. When someone has tried but still doesn't get opencv-build successful. please contact me. I can maybe help you.

  • No idea why someone downvoted your reply. But perhaps you could expand a bit the topic of the libraries? What is `opencv - lib`? What are `vc10`, `vc14`? – zkoza Apr 26 '21 at 20:26