简体   繁体   中英

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);
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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM