Reputation: 6035
I'm trying to use the OpenCV's imread()
function to load a JPEG image in a cv::Mat
, but it fails on iOS (the same code works on OS X). The returned matrix is allocated and valid, but empty (i.e. it contains no data).
Do the functions imread()
and imwrite()
work under iOS?
Upvotes: 6
Views: 8210
Reputation: 31
Try to change Compress PNG Files - Packaging
Settings to No
Build Settings
png
Compress PNG Files
to No
Remove Text Metadata From PNG Files
to No
Upvotes: 3
Reputation: 115
There's an native function for this.
#import <opencv2/imgcodecs/ios.h>
// t and m are UIImage* instances.
// transparentImage and maskImage are destination Mat.
// true/false is alpha condition.
UIImageToMat(t, transparentImage, true);
UIImageToMat(m, maskImage, true);
Upvotes: 1
Reputation: 2458
This works for me when loading images from the app bundle.
NSString *path = [[NSBundle mainBundle] pathForResource:@"pattern" ofType:@"bmp"];
const char * cpath = [path cStringUsingEncoding:NSUTF8StringEncoding];
Mat img_object = imread( cpath, CV_LOAD_IMAGE_GRAYSCALE );
I could only get it to work with bmps though, jpgs and pngs won't work for now in my app.
Upvotes: 2
Reputation: 418
It works, at least when you're saving and loading to and from the Documents-Directory of your running app.
//Creating Path to Documents-Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"ocv%03d.BMP", picNum]];
const char* cPath = [filePath cStringUsingEncoding:NSMacOSRomanStringEncoding];
const cv::string newPaths = (const cv::string)cPath;
//Save as Bitmap to Documents-Directory
cv::imwrite(newPaths, frame);
Upvotes: 9
Reputation: 10329
iOS defines its own file system and I do not believe that imread() and imwrite() interface with it. You need to use native functionality to load and save the images but once you get a pointer to the image data you can wrap it in a cv::Mat and then process it in opencv. It should not be difficult to write your own cv_imread(), cv_imwrite() functions for iOS.
Upvotes: 6