Search code examples
opencvhsv

opencv c++ HSV image channel separation exception


I know this question has been asked a number of times and I'm trying to implement their answers but its causing an exception in my code.

OS: Windows 7 OpenCV: 2.4.9

Here is my code:

#include "stdafx.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#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 webcam

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

   Mat imgHSV;
   _sleep(100); //give the camera a chance to wake up
    while (true)
    {
        Mat imgOriginal;
        bool success = cap.read(imgOriginal);
         if (!success) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }



   cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
       vector<Mat> hsv_planes;
   split( imgHSV, hsv_planes );
//hsv_planes[0] // H channel
//hsv_planes[1] // S channel
//hsv_planes[2] // V channel
  imshow(thresholded_window, hsg_planes[2]); //show the S channel
        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;
}

Exception is thrown on the split line: "Unhandled exception at 0x62B978C9 (opencv_core249.dll) in TrackColour.exe: 0xC0000005: Access violation writing location 0xFEEEFEEE."


Solution

  • I figured out my problem, incase this happens to anyone else.

    In VS I was doing a debug build but I was linking to the non debug versions of opencv. Notice in my error message opencv_core249, it should have been opencv_core249d (Note the 'd'). I updated my CV linking to use the debug libraries and it works.

    The other opencv calls performed fine using the wrong libraries, something must be unique with split.