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.

Accepted Reply

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

  • Many thanks @Claude31 . The link you supplied has the solution. I stopped depending on CIDetector & starts to depend on the Vision framework (which is supported by this third party) for detecting my envelopes & Al7amdulilah it works great with the aspect ratios of interest. 

    For future reference, I have already tried to modify CIDetector options you mentioned before while I was searching for a solution but none of them solved the issue.

Add a Comment

Replies

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

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

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

    In case running on iPhone with iOS 15 & trying to detect an envelope with mentioned aspect ratio, I'm getting an empty array of CIRectangleFeature (Expected to receive a CIRectangleFeature array with one element which represents the detected envelope)

    Where do you specify the aspect ratio ?

    The fix I have tried is the following:

    let rectangleDetector = CIDetector(ofType: CIDetectorTypeRectangle, context: CIContext(options: nil), options: [CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorAspectRatio: 2.0, CIDetectorMinFeatureSize: 0]) I have removed it from the method when I found that it doesn't solve the mentioned issue.

Add a Comment

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

  • Many thanks @Claude31 . The link you supplied has the solution. I stopped depending on CIDetector & starts to depend on the Vision framework (which is supported by this third party) for detecting my envelopes & Al7amdulilah it works great with the aspect ratios of interest. 

    For future reference, I have already tried to modify CIDetector options you mentioned before while I was searching for a solution but none of them solved the issue.

Add a Comment