Rectangle detection is not working correct on iOS 15

I am wondering if it is possible to detect a document or an envelope with aspect ratio (width / height) equals to or more than 2.0 on iOS 15 using a CIDetector object.

I found that starting from iOS 15, my application stopped to detect envelopes with the previously mentioned aspect ratios. I have tried to use CIDetectorAspectRatio, CIDetectorFocalLength & CIDetectorMinFeatureSize options with desired values to fine-tune the detection, but that didn't solve the problem.

The following is the method I'm using for getting the detected rectangles. It returns an CIRectangleFeature array of 1 element in case running application on iPhone with iOS version earlier than iOS 15, while it returns an empty array in case I'm running the application on iPhone with iOS 15 or later.

static func rectangles(inImage image: CIImage) -> [CIRectangleFeature]? {
        let rectangleDetector = CIDetector(ofType: CIDetectorTypeRectangle, context: CIContext(options: nil), options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
        guard let rectangleFeatures = rectangleDetector?.features(in: image) as? [CIRectangleFeature] else {
            return nil
        }
        return rectangleFeatures
    }

Thank you in advance.

Answered by Claude31 in 691476022

Just an idea (did not test): try to set CIDetectorMaxFeatureCount (and may be also CIDetectorAspectRatio: )

let rectangleDetector = CIDetector(ofType: CIDetectorTypeRectangle, context: CIContext(options: nil), options: [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorMaxFeatureCount: 5])

Did you try also not to pass a context ?

let rectangleDetector = CIDetector(ofType: CIDetectorTypeRectangle, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorMaxFeatureCount: 5])

See also: https://stackoverflow.com/questions/54399074/cidetector-not-detecting-proper-rectangle-in-ios

What do you get as return ? nil or wrong rectangleFeatures ?

I'm not familiar with it. Where do you specify the aspect ratio ?

Accepted Answer

Just an idea (did not test): try to set CIDetectorMaxFeatureCount (and may be also CIDetectorAspectRatio: )

let rectangleDetector = CIDetector(ofType: CIDetectorTypeRectangle, context: CIContext(options: nil), options: [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorMaxFeatureCount: 5])

Did you try also not to pass a context ?

let rectangleDetector = CIDetector(ofType: CIDetectorTypeRectangle, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorMaxFeatureCount: 5])

See also: https://stackoverflow.com/questions/54399074/cidetector-not-detecting-proper-rectangle-in-ios

Rectangle detection is not working correct on iOS 15
 
 
Q