Extract edges with OpenCV (Laplacian, Sobel, Canny)

Preprocessing of images used for machine learning

In order for the computer to understand "what is in the image", the process of drawing the outline of the detection target and finding the feature from it is often taken.

In this article, I will post the result of doing the outline drawing part with Python + OpenCV in three ways.

The original image

We have prepared two images to be processed.

Nature and humans. nature.jpg

lady.jpg

code

This is about this.

edge.py


import cv2

img = cv2.imread('xxxxxxx.jpg')

#Edge detection
edge_laplacian = cv2.Laplacian(img, -1)#Laplacian
edge_sobel = cv2.Sobel(img, -1, 0, 1)#Sobel
edge_canny = cv2.Canny(img, 10.0, 200.0)#Cany

#File export
cv2.imwrite('laplacian.jpg', edge_laplacian)
cv2.imwrite('sobel.jpg', edge_sobel)
cv2.imwrite('canny.jpg', edge_canny)

Output result

Nature

Laplacian ↓ laplacian.jpg Sobel ↓ sobel.jpg Cany ↓ canny.jpg

Human

Laplacian ↓ laplacian.jpg Sobel ↓ sobel.jpg Cany ↓ canny.jpg

Recommended Posts

Extract edges with OpenCV (Laplacian, Sobel, Canny)
Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)
Edge detection (Laplacian, Sobel, Canny)
Perform various filtering with OpenCV (gradient, high-pass, Laplacian, Gaussian)
Detect stoop with OpenCV
Rotate sprites with OpenCV
Data Augmentation with openCV
Extract EXIF with sips
Easy TopView with OpenCV
Stumble with homebrew opencv3