Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opencv - Assertion failed (dst.data == dst0.data) in cvCvtColor

The following code will post the error message:

// object is a color image with type cv::Mat

IplImage* temp_object = &(IplImage)object;
IplImage* ipl_object = cvCreateImage(cvGetSize(temp_object), 8, 3);
assert(temp_object->nChannels ==  3 && temp_object->depth == IPL_DEPTH_8U);
assert(ipl_object->nChannels ==  3 && ipl_object->depth == IPL_DEPTH_8U);
cvCvtColor(ipl_object, temp_object, CV_BGR2GRAY);

Error

OpenCV Error: Assertion failed (dst.data == dst0.data) in cvCvtColor, file /opt/local /var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.3.1/modules/imgproc/src/color.cpp, line 3175 terminate called throwing an exception`

Updated code after modification (it should work now). Thanks for the help!

IplImage temp_object (object);
IplImage* ipl_object = cvCreateImage(cvGetSize(&temp_object), 8, 1);
cvCvtColor(&temp_object, ipl_object, CV_BGR2GRAY);
like image 724
LKS Avatar asked Feb 09 '12 13:02

LKS


2 Answers

IplImage* temp_object = &(IplImage)object;

That doesn't give you a pointer to object, reinterpreted as IplImage; instead, it creates a temporary IplImage from object, gives you a pointer to that, and then destroys the temporary, leaving temp_object pointing to nothing valid. Using temp_object afterwards will give undefined behaviour.

I'm not familiar with the library, but perhaps you want a pointer to object (if IplImage is a subtype of whatever type object is):

IplImage* temp_object = static_cast<IplImage *>(&object);

using a cast to convert a pointer (or a reference, if you prefer), not the object itself.

Or maybe you want a new (non-temporary) object:

IplImage temp_object(object);
like image 143
Mike Seymour Avatar answered Sep 22 '22 02:09

Mike Seymour


Another issue is that CV_BGR2GRAY expects the destination to be a single channel, and not triple. Also, the signature of cvCvtColor() starts with SRC and then DST. You probably want to adjust your code to something like:

IplImage* ipl_object = cvCreateImage(cvGetSize(temp_object), 8, 1);
cvCvtColor(&object, ipl_object, CV_BGR2GRAY);
like image 26
karlphillip Avatar answered Sep 24 '22 02:09

karlphillip