简体   繁体   中英

UIImage to cv::Mat CV_8UC3

There are a ton of questions about converting a UIIMage to a cv::Mat with CV_8UC4 encoding. There's even the tutorial on the OpenCV iOS site.

However, for the life of me I can't figure out how to convert a UIImage to 8UC3 correctly. Using the stock OpenCV example, trying to convert to 8UC1 also breaks, as the cv:Mat has a ton of null pointers after initialization.

Any insight into how I might be able to do this correctly? Thanks!

Try calling the following after converting UIImage to cv::Mat...

cvtColor(srcMat, destMat, CV_RGBA2BGR);

For me when I want to convert from UIImage into Mat simply I use the predefined function UIImageToMat like this:

UIImageToMat(extractedUIImage, matImage);

But first you need to include this header file from openCV:

#import <opencv2/highgui/ios.h>

Same to convert from Mat into UIImage:

UIImage *extractedImage = MatToUIImage(matImage);

Just give it a try it works fine for me

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