Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV imread() returns no image data on iOS

Tags:

ios

opencv

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?

like image 202
MonsieurDart Avatar asked Oct 22 '12 15:10

MonsieurDart


2 Answers

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);
like image 200
thomketler Avatar answered Nov 06 '22 11:11

thomketler


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.

like image 35
Hammer Avatar answered Nov 06 '22 12:11

Hammer