Home > Back-end >  Exception Unhandled: at cvtColor() in openCV
Exception Unhandled: at cvtColor() in openCV

Time:09-18

I am trying to detect color using openCV in c on visual studio. When I try debugging the code using local windows debugger I get exception unhandle message on the line cvtColor(img, imgHSV, COLOR_BGR2HSV);

THIS IS MY CODE:

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;



void main()
{
    Mat imgHSV, mask;
    int hmin = 0, stmin = 0, vmin = 0;
    int hmax = 179, stmax = 255, vmax = 255;

    string path = "Resource/lambo.png";
    Mat img = imread(path);
    cvtColor(img, imgHSV, COLOR_BGR2HSV);

    namedWindow("Trackbars", (640, 200));
    createTrackbar("hmin: ", "Trackbars", &hmin, 179);
    createTrackbar("hmax: ", "Trackbars", &hmax, 179);
    createTrackbar("stmin: ", "Trackbars", &stmin, 255);
    createTrackbar("stmax: ", "Trackbars", &stmax, 255);
    createTrackbar("vmin: ", "Trackbars", &vmin, 255);
    createTrackbar("vmax: ", "Trackbars", &vmax, 255);

    while (true) {
        Scalar lower(hmin, stmin, vmin);
        Scalar upper(hmax, stmax, vmax);
        inRange(imgHSV, lower, upper, mask);

        imshow("Image", img);
        imshow("Image HSV", imgHSV);
        imshow("Image tracked", mask);
        waitKey(0);
    }
}

This is the message:

Unhandled exception at 0x00007FFDDDBD4ED9 in OpenCV_Course.exe: Microsoft C   exception: cv::Exception at memory location 0x0000005FB4AFEAD0.

CodePudding user response:

Your cv::imread probably failed and returned an empty matrix. Try:

Mat img = imread(path);
if (!img.data) {
  cout << "could not open: " << path << endl;
  return 1;
}

And fix your image path if it prints this message.

  • Related