Ask Your Question
0

Visual Studio throws an exception when I run a simple program

asked 2013-06-12 04:20:33 -0600

Robionic gravatar image

I am a newbie to Open CV and I am facing an odd error while compiling a simple open CV program. The program is as follows

#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
using namespace cv;
int main( int argc, char** argv )
{
  Mat image = imread( "image.jpg", 0 );
 Mat gray_image;
 cvtColor( image, gray_image, COLOR_BGR2GRAY);
 imwrite( "Gray_Image.jpg", gray_image);
 namedWindow( "Actual Image", WINDOW_AUTOSIZE );
 namedWindow( "Gray image", WINDOW_AUTOSIZE );
 imshow( "Actual Image", image );
 imshow( "Gray image", gray_image );
 waitKey(0);
 return 0;
}

It throws me an exception error saying

Unhandled exception at 0x7c812afb in helloworld.exe: Microsoft C++ exception: cv::Exception at memory location 0x0011e25c

I am able to continue compiling the program, by clicking on the continue option, and finally obtain the output too.

I tried using debugger/breakpoints and found the error in

cvtColor( image, gray_image, COLOR_BGR2GRAY);

I double checked the arguments for the same, but I am not able to resolve the exact error.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-06-12 05:30:11 -0600

berak gravatar image

looking at your code,

Mat image = imread( "image.jpg", 0 ); // you're already loading it as grayscale

so, no further cvtColor() needed. and yes, cvtColor() will crash, trying to convert a non-color-input

Mat image = imread( "image.jpg", 1 ); // force loading as bgr
Mat gray_image;
cvtColor( image, gray_image, COLOR_BGR2GRAY); // won't crash
edit flag offensive delete link more

Comments

Thank you very much for your reply...The issue is resolved.. Thanks for this insight.. Else I would have thought there are some installation problems..

Robionic gravatar imageRobionic ( 2013-06-12 06:34:22 -0600 )edit

nice, btw, that you got it to compile with vs2005 ;)

berak gravatar imageberak ( 2013-06-12 07:12:14 -0600 )edit

Question Tools

Stats

Asked: 2013-06-12 04:20:33 -0600

Seen: 4,741 times

Last updated: Jun 12 '13