【发布时间】:2016-03-18 14:50:43
【问题描述】:
我正在练习使用一些 swift 2,但在使用 CIDetector 时遇到了一些困难。
我有一个应用程序,其中包含一系列图片;三个不同的矩形和三张不同的人/组照片。我只是在这些图像上尝试使用 CIDetector 来查看识别的内容。我取得的最大成功是在人脸方面——但是它识别的人脸在图像上非常奇怪的地方。
这是我的检测代码:
///does the detecting of features
func detectFeatures() -> [CIFeature]? {
print("detecting")
let detectorOptions: [String: AnyObject] = [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorImageOrientation: 6]
var detectorType : String
switch currentPhoto.currentType {
case .RECTANGLES:
detectorType = CIDetectorTypeRectangle
case .FACES:
detectorType = CIDetectorTypeFace
default:
print("error in phototype")
return nil
}
let detector = CIDetector(ofType: detectorType, context: nil, options: detectorOptions)
let image = CIImage(image: imageViewer.image!)
guard image != nil else{
print("image not cast to CIImage")
return nil
}
return detector.featuresInImage(image!)
}
星星的添加位置:
for f in features!{
print(f.bounds.origin)
imageViewer.layer.addSublayer(createMarker(f.bounds.size, location: f.bounds.origin))
}
星星的诞生地:
///Creates a star on a layer and puts it over the point passed in
func createMarker(size: CGSize, location: CGPoint) -> CALayer{
print("The marker will be at [\(location.x),\(location.y)]")
//doesn't appear in correct place on the screen
let newLayer = CALayer()
newLayer.bounds = imageViewer.bounds
newLayer.frame = CGRect(origin: location, size: size)
newLayer.contents = UIImage(named: "star")?.CGImage
newLayer.backgroundColor = UIColor(colorLiteralRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).CGColor
newLayer.masksToBounds = false
return newLayer
}
有没有人遇到过这样的事情?
【问题讨论】:
-
图片视图的UIViewContentMode是什么?
-
Aspect Fit - 我的理解是这使图像具有相同的比例,但在 UIImageView 的范围内?
-
使其具有相同的纵横比,而不是相同的比例。我会尝试左上角让您的代码按原样工作。
-
很酷,感谢您的提示 - 我刚刚尝试过,这使得图片尺寸在某些情况下保持不变,因为它大于屏幕宽度,所以我看不到整个图像。这也仍然无法检测到矩形。
-
@geeks_kick 你知道如何检查图像中是否有多个矩形吗?
标签: ios swift image-recognition cidetector