Why do I have an error message when inverting an image in openCV?

I am processing a UIImage for contour creation and in that process I am firstly inverting it, then making it gray. Here is the code:

+(UIImage *)processInvertedImage:(UIImage *)image {

cv::Mat mat;
UIImageToMat(image, mat);

cv::Mat gray;
cv::cvtColor(mat, gray, CV_RGB2GRAY); 

cv::Mat inverted;
cv::invert(gray, inverted);   //// here it crashes

UIImage *binImg = MatToUIImage(inverted);   

return binImg;


This is the error code:

libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(3.4.2) /Volumes/build-storage/build/3_4_iOS-mac/opencv/modules/core/src/lapack.cpp:839: error: (-215:Assertion failed) type == 5 || type == 6 in function 'invert'


Why can't I use the invert methode? I tried to invert before converting to gray, but that did not make a difference.

1 answer

  • answered 2018-11-08 08:04 Quang Hoang

    From the document : cv::invert() requires CV_32F or CV_64F as type, not CV_8U in your case. Also, just want to make sure you want the right inverse, cv::inverse() is the mathematical inverse, not image processing inverse.