Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)

How to find the differential filter, basics http://qiita.com/shim0mura/items/5d3cbef873f2dd81d82c

OpenCV has the ability to apply Sobel and Laplacian filters.

Sobel filter

cv2.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]]) → dst http://docs.opencv.org/3.0-last-rst/modules/imgproc/doc/filtering.html?highlight=laplacian#sobel

--src: Input image --dst: Output image --ddepth: Output color depth --dx: Derivative order in the x direction --dy: Derivative order in the y direction --ksize: Specify kernel size 1, 3, 5, 7

It says that when the kernel size is 1, a 1x3 kernel or a 3x1 kernel is used, but when k = 5,7, it seems like this. http://stackoverflow.com/questions/9567882/sobel-filter-kernel-of-large-size

import cv2
from matplotlib import pyplot as plt

img = cv2.imread('Lenna.jpg', 0)

sobelx = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=3)
sobely = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=3)
sobelx5 = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=5)
sobely5 = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=5)

plt.subplot(2,2,1),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X k=3'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y k=3'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx5,cmap = 'gray')
plt.title('Sobel X k=5'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely5,cmap = 'gray')
plt.title('Sobel Y k=5'), plt.xticks([]), plt.yticks([])
plt.show()

result: スクリーンショット 2016-11-29 15.59.30.png

The line is darker and the blur is stronger when k = 5 than when k = 3.

Laplacian filter

cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]]) → dst http://docs.opencv.org/3.0-last-rst/modules/imgproc/doc/filtering.html?highlight=laplacian#laplacian

--src: Input image --dst: Output image --ddepth: Output color depth --ksize: kernel size

Since it is a Laplacian, it does not need to specify the direction unlike the one with the first derivative.

import cv2
from matplotlib import pyplot as plt

lap = cv2.Laplacian(img, cv2.CV_32F)
lap5 = cv2.Laplacian(img, cv2.CV_32F,ksize=3)

plt.subplot(1,2,1),plt.imshow(lap,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(1,2,2),plt.imshow(lap5,cmap = 'gray')
plt.title('Laplacian k=3'), plt.xticks([]), plt.yticks([])

plt.show()

result: スクリーンショット 2016-11-29 16.25.00.png

reference:

Digital image processing (https://www.cgarts.or.jp/book/img_engineer/) http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_gradients/py_gradients.html#gradients http://qiita.com/supersaiakujin/items/494cc16836738b5394c8

Recommended Posts

Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)
inspect experiment
svm experiment 1
Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)
[Python] Using OpenCV with Python (Edge Detection)
Extract edges with OpenCV (Laplacian, Sobel, Canny)
Binarization with OpenCV / Python
"Apple processing" with OpenCV3 + Python3
Try edge detection with OpenCV
Image editing with python OpenCV
Camera capture with Python + OpenCV
Edge detection (Laplacian, Sobel, Canny)
[Python] Using OpenCV with Python (Basic)
[Python] Filter spreadsheets with gspread
Real-time edge detection with OpenCV
Face detection with Python + OpenCV
Using OpenCV with Python @Mac
Shining life with Python and OpenCV
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
Text extraction with AWS Textract (Python3.6)
Easy Python + OpenCV programming with Canopy
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Load gif images with Python + OpenCV
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
Draw arrows (vectors) with opencv / python
Basic study of OpenCV with Python
Color extraction with Python + OpenCV solved the mystery of the green background
Draw a watercolor illusion with edge detection in Python3 and openCV3
Object extraction in images by pattern matching using OpenCV with Python
Face detection with Python + OpenCV (rotation invariant)
Easy keyword extraction with TermExtract for Python
Save video frame by frame with Python OpenCV
Capturing images with Pupil, python and OpenCV
I tried non-photorealistic rendering with Python + opencv
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Image acquisition from camera with Python + OpenCV
[python, openCV] base64 Face recognition with images
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
[Python] Read images with OpenCV (for beginners)
Until you can use opencv with python
Light image processing with Python x OpenCV
Smoothing edge-saved with python + OpenCV (BilateralFilter, NLMeansFilter)
Image processing with Python 100 knocks # 9 Gaussian filter
Hello World and face detection with OpenCV 4.3 + Python
I tried "smoothing" the image with Python + OpenCV
Text extraction with GCP Cloud Vision API (Python3.6)
Performance comparison of face detector with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
How to crop an image with Python + OpenCV
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Track green objects with python + numpy (particle filter)
Make OpenCV3 available from python3 installed with pyenv
Image processing from scratch with python (4) Contour extraction