Last active
October 21, 2017 07:27
-
-
Save Prasad9/995e1492ea3fb9fe8651812ff168210f to your computer and use it in GitHub Desktop.
Perform the perspective transform on an image
This file contains 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 get_mask_coord(imshape): | |
vertices = np.array([[(0.09 * imshape[1], 0.99 * imshape[0]), | |
(0.43 * imshape[1], 0.32 * imshape[0]), | |
(0.56 * imshape[1], 0.32 * imshape[0]), | |
(0.85 * imshape[1], 0.99 * imshape[0])]], dtype = np.int32) | |
return vertices | |
def get_perspective_matrices(X_img): | |
offset = 15 | |
img_size = (X_img.shape[1], X_img.shape[0]) | |
# Estimate the coordinates of object of interest inside the image. | |
src = np.float32(get_mask_coord(X_img.shape)) | |
dst = np.float32([[offset, img_size[1]], [offset, 0], [img_size[0] - offset, 0], | |
[img_size[0] - offset, img_size[1]]]) | |
perspective_matrix = cv2.getPerspectiveTransform(src, dst) | |
return perspective_matrix | |
def perspective_transform(X_img): | |
# Doing only for one type of example | |
perspective_matrix = get_perspective_matrices(X_img) | |
warped_img = cv2.warpPerspective(X_img, perspective_matrix, | |
(X_img.shape[1], X_img.shape[0]), | |
flags = cv2.INTER_LINEAR) | |
return warped_img | |
perspective_img = perspective_transform(X_img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment