[PYTHON] Perform various filtering with OpenCV (gradient, high-pass, Laplacian, Gaussian)

Introduction

OpenCV (Open Source Computer Vision Library) is a collection of BSD-licensed video / image processing libraries. There are many algorithms for image filtering, template matching, object recognition, video analysis, machine learning, and more.

Example of motion tracking using OpenCV (OpenCV Google Summer of Code 2015) https://www.youtube.com/watch?v=OUbUFn71S4s

Click here for installation and easy usage http://qiita.com/olympic2020/items/d5d475a446ec9c73261e

Click here for still image filtering Try edge detection with OpenCV

Click here for processing video files Try converting videos in real time with OpenCV Try converting webcam / camcorder video in real time with OpenCV

This time, I will apply various filters.

Various filters

The filters introduced this time are as follows.

program

The matrix for performing various filtering processes is called the kernel. The program performs the following processing for each filter.

  1. Define the kernel
  2. Apply cv2.filter2D ()
  3. Save to file

filter.py


import cv2
import numpy as np


#Constant definition
FILE_ORG = "img.png "
FILE_GRAY = "gray.png "
FILE_GRADIENT = "gradient.png "
FILE_HIGH_PASS = "highpass.png "
FILE_LAPLACIAN_3x3 = "laplacian3x3.png "
FILE_LAPLACIAN_5x5 = "laplacian5x5.png "
FILE_GAUSSIAN = "gaussian.png "

#Load the original image
img_org = cv2.imread(FILE_ORG, cv2.IMREAD_COLOR)

#Grayscale conversion
img_gray = cv2.cvtColor(img_org, cv2.COLOR_BGR2GRAY)
cv2.imwrite(FILE_GRAY, img_gray)

#Gradient filter (3x3)
kernel_gradient_3x3 = np.array([
                            [ 1,  1,  1],
                            [ 0,  0,  0],
                            [-1, -1, -1]
                            ], np.float32)
img_gradient_3x3 = cv2.filter2D(img_gray, -1, kernel_gradient_3x3)
cv2.imwrite(FILE_GRADIENT_3x3, img_gradient_3x3)

#Gradient filter (5x5)
kernel_gradient_5x5 = np.array([
                            [ 5,  5,  5,  5,  5],
                            [ 3,  3,  3,  3,  3],
                            [ 0,  0,  0,  0,  0],
                            [-3, -3, -3, -3, -3],
                            [-5, -5, -5, -5, -5]
                            ], np.float32)
img_gradient_5x5 = cv2.filter2D(img_gray, -1, kernel_gradient_5x5)
cv2.imwrite(FILE_GRADIENT_5x5, img_gradient_5x5)

#High pass filter
kernel_high_pass = np.array([
                            [-1, -1, -1],
                            [-1,  8, -1],
                            [-1, -1, -1]
                            ], np.float32)
img_high_pass = cv2.filter2D(img_gray, -1, kernel_high_pass)
cv2.imwrite(FILE_HIGH_PASS, img_high_pass)

#Laplacian filter (3x3)
kernel_laplacian_3x3 = np.array([
                            [1,  1, 1],
                            [1, -8, 1],
                            [1,  1, 1]
                            ], np.float32)
img_laplacian_3x3 = cv2.filter2D(img_gray, -1, kernel_laplacian_3x3)
cv2.imwrite(FILE_LAPLACIAN_3x3, img_laplacian_3x3)

#Laplacian filter (5x5)
kernel_laplacian_5x5 = np.array([
                            [-1, -3, -4, -3, -1],
                            [-3,  0,  6,  0, -3],
                            [-4,  6, 20,  6, -4],
                            [-3,  0,  6,  0, -3],
                            [-1, -3, -4, -3, -1]
                            ], np.float32)
img_laplacian_5x5 = cv2.filter2D(img_gray, -1, kernel_laplacian_5x5)
cv2.imwrite(FILE_LAPLACIAN_5x5, img_laplacian_5x5)

#Gaussian filter
kernel_gaussian = np.array([
                            [1,  2, 1],
                            [2,  4, 2],
                            [1,  2, 1]
                            ], np.float32) / 16
img_gaussian = cv2.filter2D(img_gray, -1, kernel_gaussian)
cv2.imwrite(FILE_GAUSSIAN, img_gaussian)

Execution result

It is an image of the execution result. The 3x3 filter captures the edges of the building sharply. On the other hand, the 5x5 filter captures large changes in the landscape.

img.png The original image

gray.png grayscale

gradient3x3.png ** Gradient filter (3x3) **

gradient5x5.png ** Gradient filter (5x5) **

highpass.png ** High pass filter **

laplacian3x3.png ** Laplacian filter (3x3) **

laplacian5x5.png ** Laplacian filter (5x5) **

gaussian.png ** Gaussian filter **

Recommended Posts

Perform various filtering with OpenCV (gradient, high-pass, Laplacian, Gaussian)
[Python] Using OpenCV with Python (Image Filtering)
Extract edges with OpenCV (Laplacian, Sobel, Canny)
Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)