How do I find a rectangle in a photo using iphone?

TiansHUo picture TiansHUo · May 26, 2011 · Viewed 7.3k times · Source

I will need to identify, rotate and crop a rectangle(business card) from a photo took from an iphone. I believe this can be done by using OpenCV, but I have not used it before. Could anyone give some tips on this?

Answer

Ded77 picture Ded77 · Sep 26, 2014

From iOS 8, you can now use the new detector CIDetectorTypeRectangle that returns CIRectangleFeature. You can add options :

  • Accuracy : low/hight
  • Aspect ratio : 1.0 if you want to find only squares for example

    CIImage *image = // your image
    NSDictionary *options = @{CIDetectorAccuracy: CIDetectorAccuracyHigh, CIDetectorAspectRatio: @(1.0)};
    CIDetector *rectangleDetector = [CIDetector detectorOfType:CIDetectorTypeRectangle context:nil options:options];
    
    NSArray *rectangleFeatures = [rectangleDetector featuresInImage:image];
    
    for (CIRectangleFeature *rectangleFeature in rectangleFeatures) {
        CGPoint topLeft = rectangleFeature.topLeft;
        CGPoint topRight = rectangleFeature.topRight;
        CGPoint bottomLeft = rectangleFeature.bottomLeft;
        CGPoint bottomRight = rectangleFeature.bottomRight;
    }
    

More information in WWDC 2014 Advances in Core Image, Session 514.

An example from shinobicontrols.com