CIDetector要么不检测,要么在奇怪的地方检测

我正在练习使用一些快速2,并遇到了使用CIDetector一些困难。

我有一个应用程序,其中有一系列的图片; 三个不同的矩形和三个不同的人/组照片。 我只是试图在这些图像上的CIDetector看到什么是公认的。 我拥有的最成功的是脸部 – 但是它所识别的脸部在图像上非常奇怪的地方。

这是我testing的一个矩形图像,以及它的输出: 矩形测试

这是一张脸像: 在这里输入图像说明

这是我的检测代码:

///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)) } 

和星星的创build地点:

 ///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 } 

有没有人遇到过这样的事情?

不幸的是,我不认为你使用的矩形与你使用的图像兼容。 如果您查看CIDetector类参考它读取:

CIDetectorTypeRectangle在静止图像或video中search矩形区域的检测器,返回提供有关检测区域信息的CIRectangleFeature对象。

矩形检测器查找可能代表图像中透视显示的矩形对象的区域,例如在桌面上看到的纸张或书籍。

接受这个build议,我在这个图像上运行了你的代码: 在这里输入图像说明

结果如下:

bounds =“(296.752716064453,74.7079086303711,181.281158447266,253.205474853516)\ n”

我在没有find任何function的其他平面矩形上运行你的代码。 需要有点“真实世界”的形象。