【问题标题】:CIDetector either not detecting, or detecting in odd placesCIDetector 要么没有检测到,要么在奇怪的地方检测到
【发布时间】: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


【解决方案1】:

很遗憾,我认为您使用的矩形与您使用的图像不兼容。如果您查看CIDetector Class Reference,它会显示:

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

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

接受这个建议,我在这张图片上运行了你的代码:

有了结果:

bounds = "(296.752716064453, 74.7079086303711, 181.281158447266, 253.205474853516)\n"

我在未找到任何特征的其他平面矩形上运行了您的代码。需要有点“真实世界”的形象。

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2014-12-23
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多