Last active
April 11, 2018 20:12
-
-
Save Breta01/5b192330aafef42a89c38a6f5bff47f6 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
def fourCornersSort(pts): | |
""" Sort corners: top-left, bot-left, bot-right, top-right """ | |
# Difference and sum of x and y value | |
# Inspired by http://www.pyimagesearch.com | |
diff = np.diff(pts, axis=1) | |
summ = pts.sum(axis=1) | |
# Top-left point has smallest sum... | |
# np.argmin() returns INDEX of min | |
return np.array([pts[np.argmin(summ)], | |
pts[np.argmax(diff)], | |
pts[np.argmax(summ)], | |
pts[np.argmin(diff)]]) | |
def contourOffset(cnt, offset): | |
""" Offset contour, by 5px border """ | |
# Matrix addition | |
cnt += offset | |
# if value < 0 => replace it by 0 | |
cnt[cnt < 0] = 0 | |
return cnt | |
# Sort and offset corners | |
pageContour = fourCornersSort(pageContour[:, 0]) | |
pageContour = contourOffset(pageContour, (-5, -5)) | |
# Recalculate to original scale - start Points | |
sPoints = pageContour.dot(image.shape[0] / 800) | |
# Using Euclidean distance | |
# Calculate maximum height (maximal length of vertical edges) and width | |
height = max(np.linalg.norm(sPoints[0] - sPoints[1]), | |
np.linalg.norm(sPoints[2] - sPoints[3])) | |
width = max(np.linalg.norm(sPoints[1] - sPoints[2]), | |
np.linalg.norm(sPoints[3] - sPoints[0])) | |
# Create target points | |
tPoints = np.array([[0, 0], | |
[0, height], | |
[width, height], | |
[width, 0]], np.float32) | |
# getPerspectiveTransform() needs float32 | |
if sPoints.dtype != np.float32: | |
sPoints = sPoints.astype(np.float32) | |
# Wraping perspective | |
M = cv2.getPerspectiveTransform(sPoints, tPoints) | |
newImage = cv2.warpPerspective(image, M, (int(width), int(height))) | |
# Saving the result. Yay! (don't forget to convert colors bact to BGR) | |
cv2.imwrite("folder/resultImage.jpg", cv2.cvtColor(newImage, cv2.COLOR_BGR2RGB)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment