[PYTHON] Edge detection (Laplacian, Sobel, Canny)

Execution environment

Google Colaboratory

Preparing to load images with Google Colaboratory

python


from google.colab import files
from google.colab import drive
drive.mount('/content/drive')

Loading the required libraries

python


import cv2 #opencv
import matplotlib.pyplot as plt 
%matplotlib inline

Image preparation

python


img = plt.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

code

python


#Original image
plt.subplot(2,3,1)
plt.axis('off') 
plt.title("Original", fontsize=10)
plt.imshow(gray)

#Laplacian
plt.subplot(2,3,4)
plt.axis('off') 
plt.title("Laplacian", fontsize=10)
dst = cv2.Laplacian(gray,ddepth = -1)
plt.imshow(dst)

#Sobel
plt.subplot(2,3,5)
plt.axis('off') 
plt.title("Sobel", fontsize=10)
dst = cv2.Sobel(gray,ddepth = -1,dx = 0,dy = 1) #dx,Determine the number of differential characters with dy.
plt.imshow(dst)

#Sobel
plt.subplot(2,3,6)
plt.axis('off') 
plt.title("Canny", fontsize=10)
dst = cv2.Canny(gray,threshold1 = 64,threshold2 = 128)
#The smaller thresholds 1 and 2 are used for joining edges.
#Larger ones are used for initial detection of stronger edges.

plt.imshow(dst)
plt.show()

result

image.png

Recommended Posts

Edge detection (Laplacian, Sobel, Canny)
Extract edges with OpenCV (Laplacian, Sobel, Canny)
Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)
Try edge detection with OpenCV
Real-time edge detection with OpenCV
cv2.Canny (): Makes the adjustment of edge detection by the Canny method nice
[Python] Using OpenCV with Python (Edge Detection)