Unhandled Exception in C++ OpenCV Color Detection Program

82 Views Asked by At

I'm trying to create a program in Visual Studio that detects color in black and white video clips. Here is my code:

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


using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    //Load clip
    VideoCapture cap("C:/Users/nicho/Videos/ThermalClip1.mp4");
    //Note: location of video file on current computer may differ from initial code. Change accordingly

    //If clip cannot be loaded, exit program
    if (cap.isOpened() == false)
    {
        cout << "Error. Cannot open video file" << endl;
        cin.get();
        return -1;
    }

    namedWindow("Control", WINDOW_AUTOSIZE);//Create control window

    int hLowVal = 0;//initial hue value (lower)
    int hUpVal = 179;//initial hue value (upper)

    int hSatLow = 0;//initial saturation (lower)
    int hSatUp = 255; //initial saturation (upper)

    int valueLower = 0;
    int valueUpper = 255;

    //Create trackbars for control window
    createTrackbar("Lower Hue", "Adjust", &hLowVal, 179);
    createTrackbar("Increase Hue", "Adjust", &hUpVal, 179);
    createTrackbar("Lower Saturation", "Adjust", &hSatLow, 255);
    createTrackbar("Increase Saturation", "Adjust", &hSatUp, 255);
    createTrackbar("Lower Value", "Adjust", &valueLower, 255);
    createTrackbar("Increase Value", "Adjust", &valueUpper, 255);

    while (1)
    {
        //Load actual image
        Mat actualImage;
        bool temp = cap.read(actualImage);


        //Store converted image
        Mat convertToHSV;
        cvtColor(actualImage, convertToHSV, COLOR_BGR2HSV);

        //Matrix for window where thermal spot will be detected
        Mat detectionScreen;
        inRange(convertToHSV, Scalar(hLowVal, hSatLow, valueLower), Scalar(hUpVal, hSatUp, valueUpper), detectionScreen);

        //remove small objects from foreground
        erode(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        dilate(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        //fill up small holes in foreground
        dilate(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
        erode(detectionScreen, detectionScreen, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

        //Show the thermal spot
        imshow("Thermal Spot detected", detectionScreen);

        imshow("Original Image", detectionScreen);

        if (waitKey(30) == 27)//If escape key is pressed, break loop
        {
            break;
        }
    }
    
    return 0;
}

The problem is at this line of code:

createTrackbar("Lower Hue", "Adjust", &hLowVal, 179);

When I tried to run it, it came up with this error message:

Error:Unhandled exception at 0x00007FFD5B4E4C3C in ThermalSpotDetector.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000BA269DD5C0

Does anyone have any idea what is happening?

1

There are 1 best solutions below

0
wohlstad On

As you can see in the cv::createTrackbar documentation,
the second parameter winname is:

Name of the window that will be used as a parent of the created trackbar.

When you created the window by calling cv::namedWindow here:

namedWindow("Control", WINDOW_AUTOSIZE);

you used the name "Control" (the first argument in the call), and this should be the window name.

Therefore you should change your first createTrackbar call to:

//--------------------------vvvvvvvv----------------
createTrackbar("Lower Hue", "Control", &hLowVal, 179);

Same goes to all other createTrackbar calls.