Created
June 8, 2017 15:07
-
-
Save hackintoshrao/cc0e10f58079ede6866111d6ed75b420 to your computer and use it in GitHub Desktop.
Sobel edge detection with kernel size and magnitude of x and y edge detection
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
# Define a function to return the magnitude of the gradient | |
# for a given sobel kernel size and threshold values | |
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)): | |
# Convert to grayscale | |
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
# 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 | |
# 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