To perform edge detection using Sobel, Laplacian, and Canny edge detectors.
Anaconda - Python 3.7
Import the necessary modules.
Convert image into GRAY using COLOR_BGR2GRAY
Apply Gaussian Blur to the gray image
Perform edge detection on a image.
- Sobel
- Laplacian
- Canny
Display the original images with edge detected images.
import cv2
from cv2 import COLOR_BGR2GRAY
import matplotlib.pyplot as plt
input_img = cv2.imread("mikasa.jpg",1)
gray = cv2.cvtColor(input_img,COLOR_BGR2GRAY)
img = cv2.GaussianBlur(gray,(3,3),0)
cv2.imshow("GaussianBlur",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=9)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=13)
sobelxy =cv2.Sobel(img,cv2.CV_64F,1,1,ksize=31)
plt.imshow(img,cmap = 'gray')
plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.show()
plt.imshow(sobelx,cmap = 'gray')
plt.title('sobelx')
plt.xticks([]), plt.yticks([])
plt.show()
plt.imshow(sobely,cmap = 'gray')
plt.title('sobely')
plt.xticks([]), plt.yticks([])
plt.show()
plt.imshow(sobelxy,cmap = 'gray')
plt.title('sobelxy')
plt.xticks([]), plt.yticks([])
plt.show()
laplacian = cv2.Laplacian(img,cv2.CV_64F)
cv2.imshow("laplacian",laplacian)
cv2.waitKey(0)
cv2.destroyAllWindows()
canny_edges = cv2.Canny(img, 120, 150)
cv2.imshow("Canny",canny_edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Original | Sobel X |
---|---|
![]() |
![]() |
Sobel Y | Sobel XY |
![]() |
![]() |
Original | Laplacian |
---|---|
![]() |
![]() |
Original | Canny Edge |
---|---|
![]() |
![]() |
Thus the edges are detected using Sobel, Laplacian, and Canny edge detectors.