I'm trying to use OpenCV with iOS. Everything works fine when I use an image included in the application through Xcode.
However I need to read an image taken through the camera. I've tested numerous suggestions from here on StackOverflow and other sites, but to no luck.
I have tried using OpenCV's UIImageToMat, and I have tried saving the image to the Documents Directory on the device, and then reading this file in through imread().
Unfortunately the Mat object's data is NULL and the matrix is empty. Has anybody got any ideas?
let filename = getDocumentsDirectory().appendingPathComponent("temp.jpg")
try? dataImage.write(to: filename)
let test = OpenCVWrapper()
let plate = test.getLicensePlate(filename.absoluteString)
print(plate ?? "nil")
I have checked that the file does indeed exist in the documents directory, so I really have no idea what's going on!
OK so after many hours of frustration, I have it working. Posting my solution here for anyone else who wishes to use the OpenALPR library for iOS (and by extension OpenCV with imread()).
Firstly, the code above uses a URL path, converted to a String with the .absolutestring method. This path will not work with imread(). You will need to use the following instead:
if let image = UIImage(data: dataImage)?.fixOrientation() {
if let data = UIImageJPEGRepresentation(image, 1) {
var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
path.append("/temp.jpg")
try? data.write(to: URL(fileURLWithPath: path))
let cv = OpenCVWrapper()
plate = cv.getLicensePlate(path)
print(plate ?? "nil")
}
}
If you are performing analysis which is orientation sensitive, you will need to fix your captured images orientation before you work on it. See here for an explanation.
Here is the Swift 3 version of the UIImage extension mentioned in the link aforementioned:
extension UIImage {
func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
return CGRect(x: x, y: y, width: width, height: height)
}
func fixOrientation() -> UIImage {
if self.imageOrientation == UIImageOrientation.up {
return self
}
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
self.draw(in: CGRectMake(0, 0, self.size.width, self.size.height))
let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return normalizedImage;
}
}