Created
November 8, 2017 21:06
-
-
Save cipher982/1c0620c6ab994fcfa87a257a49dbc5b5 to your computer and use it in GitHub Desktop.
opencv binary transformation lane tracking
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
| # for a given sobel kernel size and threshold values | |
| def toMagSobel(img, sobel_kernel=3, mag_thresh=(0, 255)): | |
| # Convert to grayscale | |
| if len(np.shape(img)) > 2: | |
| gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| else: | |
| gray = img | |
| # Take both Sobel x and y gradients | |
| sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel) | |
| sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel) | |
| # Calculate the gradient magnitude | |
| gradmag = np.sqrt(sobelx**2 + sobely**2) | |
| # Rescale to 8 bit | |
| scale_factor = np.max(gradmag)/255 | |
| gradmag = (gradmag/scale_factor).astype(np.uint8) | |
| # Create a binary image of ones where threshold is met, zeros otherwise | |
| binary_output = np.zeros_like(gradmag) | |
| binary_output[(gradmag >= mag_thresh[0]) & (gradmag <= mag_thresh[1])] = 1 | |
| plt.suptitle('After Binary Mag Sobel', fontsize=14, fontweight='bold') | |
| plt.imshow(binary_output) | |
| plt.show() | |
| # Return the binary image | |
| return binary_output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment