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

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


See opencv sample code OpenCV2.2\samples\cpp\squares.cpp. They do the following:

  • Detect edges using Canny

  • Retrieve contours by findContours

  • For each contour

    • approximate contour using approxPolyDP to decrease number of vertices

    • if contour has 4 vertices and each angle is ~90 degrees then yield a rectangle


To get you started, you should look at the feature detection api of OpenCV. Especially

  • cv::Canny (for edge detection),
  • maybe cv::cornerHarris (for corner detection),
  • and cv::HoughLines (for finding straight lines in the edge image).

HTH