将 imread() 与 iOS 一起使用
Using imread() with iOS
我正在尝试将 OpenCV 与 iOS 一起使用。当我通过 Xcode.
使用应用程序中包含的图像时,一切正常
但是我需要读取通过相机拍摄的图像。我已经在 Whosebug 和其他网站上测试了这里的许多建议,但没有成功。
我试过使用 OpenCV 的 UIImageToMat,我试过将图像保存到设备上的文档目录,然后通过 imread() 读取此文件。
不幸的是,Mat 对象的数据为 NULL,矩阵为空。有人有什么想法吗?
let filename = getDocumentsDirectory().appendingPathComponent("temp.jpg")
try? dataImage.write(to: filename)
let test = OpenCVWrapper()
let plate = test.getLicensePlate(filename.absoluteString)
print(plate ?? "nil")
我已经检查过文档目录中确实存在该文件,所以我真的不知道发生了什么!
好的,经过几个小时的挫折,我让它工作了。将我的解决方案发布在这里,供任何希望将 OpenALPR 库用于 iOS(以及通过 imread() 扩展 OpenCV)的其他人使用。
首先,上面的代码使用 URL 路径,使用 .absolutestring 方法转换为字符串。此路径不适用于 imread()。您将需要改用以下内容:
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")
}
}
如果您正在执行对方向敏感的分析,则需要在处理之前修复捕获的图像方向。有关解释,请参阅 here。
这里是 Swift 前面提到的 link 中提到的 UIImage 扩展的 3 版本:
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;
}
}
我正在尝试将 OpenCV 与 iOS 一起使用。当我通过 Xcode.
使用应用程序中包含的图像时,一切正常但是我需要读取通过相机拍摄的图像。我已经在 Whosebug 和其他网站上测试了这里的许多建议,但没有成功。
我试过使用 OpenCV 的 UIImageToMat,我试过将图像保存到设备上的文档目录,然后通过 imread() 读取此文件。
不幸的是,Mat 对象的数据为 NULL,矩阵为空。有人有什么想法吗?
let filename = getDocumentsDirectory().appendingPathComponent("temp.jpg")
try? dataImage.write(to: filename)
let test = OpenCVWrapper()
let plate = test.getLicensePlate(filename.absoluteString)
print(plate ?? "nil")
我已经检查过文档目录中确实存在该文件,所以我真的不知道发生了什么!
好的,经过几个小时的挫折,我让它工作了。将我的解决方案发布在这里,供任何希望将 OpenALPR 库用于 iOS(以及通过 imread() 扩展 OpenCV)的其他人使用。
首先,上面的代码使用 URL 路径,使用 .absolutestring 方法转换为字符串。此路径不适用于 imread()。您将需要改用以下内容:
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")
}
}
如果您正在执行对方向敏感的分析,则需要在处理之前修复捕获的图像方向。有关解释,请参阅 here。
这里是 Swift 前面提到的 link 中提到的 UIImage 扩展的 3 版本:
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;
}
}