6

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?

deleted_user
  • 3,817
  • 1
  • 18
  • 27
MonsieurDart
  • 6,005
  • 2
  • 43
  • 45

5 Answers5

9

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);
thomketler
  • 418
  • 4
  • 9
6

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.

Hammer
  • 10,109
  • 1
  • 36
  • 52
  • Thanks for this hint. I came to the same conclusion, but I wanted to be sure about that. Are you? I use the native iOS methods to load images, it works fine. But in this case, I was trying to share some OpenCV code among different platforms… hence the question. ;-) – MonsieurDart Oct 22 '12 at 16:36
  • I have tried to use them before on iOS and never got it to work. I haven't ever looked at the inside of imread() or imwrite() so I can't say I'm **sure** that there is no way to make them work. I would be shocked if they did though. Hopefully someone can give you a more definite answer – Hammer Oct 22 '12 at 16:42
3

Try to change Compress PNG Files - Packaging Settings to No

  1. Selected targets Build Settings
  2. Search png
  3. Change Compress PNG Files to No
  4. Change Remove Text Metadata From PNG Files to No
JL.L
  • 31
  • 2
  • This exactly fixed my problem! In my project, jpg file could be `imread()` but png. The png could be read after changing these settings. – Yun CHEN Oct 25 '18 at 03:13
2

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.

huesforalice
  • 2,418
  • 2
  • 24
  • 29
  • Thank you! You need really convert image to .bmp and then are able to read it using imread from the app bundle – Julian D. Jun 13 '17 at 15:20
1

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);
RafaelPlantard
  • 117
  • 1
  • 7