Created
June 8, 2017 11:04
-
-
Save hackintoshrao/230f9d49c24c58f9e067319e71d45e7d to your computer and use it in GitHub Desktop.
undistort and perspective transform chess board image.
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
| # Define a function that takes an image, number of x and y points, | |
| # camera matrix and distortion coefficients | |
| def corners_unwarp(img, nx, ny, mtx, dist): | |
| # Use the OpenCV undistort() function to remove distortion | |
| undist = cv2.undistort(img, mtx, dist, None, mtx) | |
| # Convert undistorted image to grayscale | |
| gray = cv2.cvtColor(undist, cv2.COLOR_BGR2GRAY) | |
| # Search for corners in the grayscaled image | |
| ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None) | |
| if ret == True: | |
| # If we found corners, draw them! (just for fun) | |
| cv2.drawChessboardCorners(undist, (nx, ny), corners, ret) | |
| # Choose offset from image corners to plot detected corners | |
| # This should be chosen to present the result at the proper aspect ratio | |
| # My choice of 100 pixels is not exact, but close enough for our purpose here | |
| offset = 100 # offset for dst points | |
| # Grab the image shape | |
| img_size = (gray.shape[1], gray.shape[0]) | |
| # For source points I'm grabbing the outer four detected corners | |
| src = np.float32([corners[0], corners[nx-1], corners[-1], corners[-nx]]) | |
| # For destination points, I'm arbitrarily choosing some points to be | |
| # a nice fit for displaying our warped result | |
| # again, not exact, but close enough for our purposes | |
| dst = np.float32([[offset, offset], [img_size[0]-offset, offset], | |
| [img_size[0]-offset, img_size[1]-offset], | |
| [offset, img_size[1]-offset]]) | |
| # Given src and dst points, calculate the perspective transform matrix | |
| M = cv2.getPerspectiveTransform(src, dst) | |
| # Warp the image using OpenCV warpPerspective() | |
| warped = cv2.warpPerspective(undist, M, img_size) | |
| # Return the resulting image and matrix | |
| return warped, M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment