How to find a rectangle in a photo using iphone?

I will need to identify, rotate and crop the rectangle (business card) from the photo taken with the iphone. I believe that this can be done using OpenCV, but I have not used it before. Can anyone give some advice on this?

+6
iphone image-processing opencv ipad image-recognition
source share
6 answers

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

  • Finding edges with Canny

  • Get the contours of findContours

  • For each circuit

    • approximate contour using approxPolyDP to reduce the number of vertices

    • if the contour has 4 vertices and each angle is 90 degrees, then gives a rectangle

+8
source share

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

  • Accuracy: Low / High
  • 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; } 

Additional Information at WWDC 2014 Achievements in Core Image, Session 514.

Example from shinobicontrols.com

+14
source share

To get started, you should look at the discovery of the OpenCV api function . Special

  • cv::Canny (for edge detection),
  • possibly cv::cornerHarris (to determine the angle),
  • and cv::HoughLines (for finding straight lines in the edge image).

NTN

+2
source share

If you know the approximate height and width of the rectangle, you can take the following steps:

  • Object detection: marking components using contour tracing.
  • After all the objects have been marked, then filter out the detected components, calculating the perimeter of the rectangle. P = 2 * (H + W) Any component having a size smaller or larger than P is ignored, only components close to the size of P. are retained.
  • Find the corner points of the rectangle from the contour points.
  • Extract the rectangle from the original image.

All of the above steps can be performed using OpenCV or Aforge.Net, I personally did this using Aforge.Net.

+2
source share

Depending on the point of view, the map may not be a rectangle, but rather a trapezoid. You can use HoughLines2 in OpenCV to define the edges of the image and try to identify the 4 edges that are likely to become the edges of the business map.

+1
source share

I think that goodFeaturesToTrack() easier to use for Harris-Corner detection. CornerHarris() needs to set the outgoing image for a specific type.

+1
source share

All Articles