Clip
Clip

Reputation: 3078

Crop image based on largest quadrilateral contour OpenCV

I am in high school and I am working on a project to build an iOS app that lets users scan business cards, currently I am working on edge detection on the card. I believe I have it working correctly and it is drawing the outline of the card and some other shadows ect.

Here is a sample image I got back while scanning, I should probably convert to grayscale which I'll do as well.

How can I find the largest quadrilateral and crop my image to included only the card? All of my contours are stored in a vector<vector<cv::Point> > The green line seems to be the one I'd be interested in.

enter image description here

Upvotes: 2

Views: 1671

Answers (2)

ajayjain
ajayjain

Reputation: 11

This doesn't directly answer the question, but in python, to find the largest contour, you would do something like:

contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours.sort(key=cv2.contourArea, reverse=True)

cv2.contourArea is a lambda that calculates the area of a contour reverse=True: descending order (biggest first)

Then the 0th element of contours is the biggest contour. Draw with:

cv2.drawContours(image, contours, 0, (255, 0, 0), 2)

Upvotes: 0

Mene
Mene

Reputation: 3799

To find the largest quadrilateral you could just iterate over all and check for their dimensions/area.

The projection can be done with find findHomography and warpPerspective. You will need the four corner points of your quadrilateral and the four corresponding points in your reprojection (i.e. the corners of the image).

Upvotes: 0

Related Questions