Simple RGB to Gray program crashes

282 views Asked by At

I am trying to make a simple conversion of an image from RGB to Grayscale with OpenCV. I am using dev-cp on windows, here is the code:

#include <cv.h>
#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )
{
  char* imageName = argv[1];

  Mat image;
  image = imread( imageName );

  Mat gray_image;
  cvtColor(image,gray_image,CV_RGB2GRAY);
}

When I execute it, seems it crashes on the cvtColor.

1

There are 1 answers

3
Micka On

please try

#include <cv.h>
#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )
{
  if (argc < 2) return 0;

  char* imageName = argv[1];

  Mat image;
  image = imread( imageName );

  if(image.empty()) return 0;

  Mat gray_image;
  cvtColor(image,gray_image,CV_RGB2GRAY);

  imshow("image",image);
  cv::waitKey(0);

  return 0;
}