CIDetector未检测到或在奇怪的地方检测到 [英] CIDetector either not detecting, or detecting in odd places

查看:157
本文介绍了CIDetector未检测到或在奇怪的地方检测到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习使用一些Swift 2,并且在使用CIDetector时遇到了一些困难.

I am practicing using some swift 2 and have run in to some difficulties in using the CIDetector.

我有一个应用,其中包含一系列图片;三个不同的矩形和三张不同的人物/群组照片.我一直在尝试在这些图像上使用CIDetector来查看识别出的内容.我最成功的是面孔-但是,它所识别的面孔在图像上非常奇怪的地方.

I have an app which has a series of pictures in; three different rectangles and three different photos of people/groups. I have been just trying the CIDetector on these images to see what is recognised. The most success I have has is with the faces - however the face it's recognising are in very weird places on the image.

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

Here is a rectangle image I tested, along with it's output:

这是一张脸部图像:

And here is a face image:

这是我的检测代码:

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

以前有没有人遇到过类似的事情?

Has anyone encountered anything like this before?

推荐答案

不幸的是,我认为您使用的矩形与您使用的图像不兼容.如果您查看 CIDetector类参考内容为:

Unfortunately, I don't think the rectangles you are using are compatible with the image you're using. If you look at the CIDetector Class Reference it reads:

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

CIDetectorTypeRectangle A detector that searches for rectangular areas in a still image or video, returning CIRectangleFeature objects that provide information about detected regions.

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

The rectangle detector finds areas that are likely to represent rectangular objects that appear in perspective in the image, such as papers or books seen on a desktop.

采纳此建议,我在此图像上运行了您的代码:

Taking this advice I ran your code on this image:

结果:

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

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

我在没有找到特征的其他平面矩形上运行了您的代码.需要有点像现实世界".图片.

I ran your code on other plan rectangles with no features found. Needs to be somewhat of a "real-world" image.

这篇关于CIDetector未检测到或在奇怪的地方检测到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆