how to use edge detection using gradient magnitude in python

Solutions on MaxInterview for how to use edge detection using gradient magnitude in python by the best coders in the world

showing results for - "how to use edge detection using gradient magnitude in python"
Camilla
20 Jan 2018
1import numpy as np
2import cv2
3
4# Load the image, convert it to grayscale, and show it
5image = cv2.imread("hand.png")
6image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
7cv2.imshow("Greyscale", image)
8
9# Compute the Laplacian of the image
10lap = cv2.Laplacian(image, cv2.CV_64F)
11lap = np.uint8(np.absolute(lap))
12cv2.imshow("Laplacian", lap)
13cv2.waitKey(0)
14
15# Compute gradients along the X and Y axis, respectively
16sobelX = cv2.Sobel(image, cv2.CV_64F, 1, 0)
17sobelY = cv2.Sobel(image, cv2.CV_64F, 0, 1)
18
19# The sobelX and sobelY images are now of the floating
20# point data type -- we need to take care when converting
21# back to an 8-bit unsigned integer that we do not miss
22# any images due to clipping values outside the range
23# of [0, 255]. First, we take the absolute value of the
24# graident magnitude images, THEN we convert them back
25# to 8-bit unsigned integers
26sobelX = np.uint8(np.absolute(sobelX))
27sobelY = np.uint8(np.absolute(sobelY))
28
29# We can combine our Sobel gradient images using our
30# bitwise OR
31sobelCombined = cv2.bitwise_or(sobelX, sobelY)
32
33# Show our Sobel images
34cv2.imshow("Sobel X", sobelX)
35cv2.imshow("Sobel Y", sobelY)
36cv2.imshow("Sobel Combined", sobelCombined)
37cv2.waitKey(0)
38
similar questions