Last active
January 23, 2017 20:51
-
-
Save Breta01/1d70a62d5b777f3aa36571758c1d0583 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Getting contours | |
im2, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
# Finding contour of biggest rectangle | |
# Otherwise return corners of original image | |
# Don't forget on our 5px border! | |
height = edges.shape[0] | |
width = edges.shape[1] | |
MAX_COUNTOUR_AREA = (width - 10) * (height - 10) | |
# Page fill at least half of image, then saving max area found | |
maxAreaFound = MAX_COUNTOUR_AREA * 0.5 | |
# Saving page contour | |
pageContour = np.array([[5, 5], [5, height-5], [width-5, heigh-5], [widt-5, 5]]) | |
# Go through all contours | |
for cnt in contours: | |
# Simplify contour | |
perimeter = cv2.arcLength(cnt, True) | |
approx = cv2.approxPolyDP(cnt, 0.03 * perimeter, True) | |
# Page has 4 corners and it is convex | |
# Page area must be bigger than maxAreaFound | |
if (len(approx) == 4 and | |
cv2.isContourConvex(approx) and | |
maxAreaFound < cv2.contourArea(approx) < MAX_COUNTOUR_AREA): | |
maxAreaFound = cv2.contourArea(approx) | |
pageContour = approx | |
# Result in pageConoutr (numpy array of 4 points): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment