📚
DLIP
  • Introduction
  • Prerequisite
  • Image Processing Basics
    • Notes
      • Thresholding
      • Spatial Filtering
      • Masking with Bitwise Operation
      • Model n Calibration
    • Tutorial
      • Tutorial: Install OpenCV C++
      • Tutorial: Create OpenCV Project
      • Tutorial: C++ basics
      • Tutorial: OpenCV Basics
      • Tutorial: Image Watch for Debugging
      • Tutorial: Spatial Filter
      • Tutorial: Thresholding and Morphology
      • Tutorial: Camera Calibration
      • Tutorial: Color Image Processing
      • Tutorial: Edge Line Circle Detection
      • Tutorial: Corner Detection and Optical Flow
      • Tutorial: OpenCV C++ Cheatsheet
      • Tutorial: Installation for Py OpenCV
      • Tutorial: OpenCv (Python) Basics
    • LAB
      • Lab Report Template
      • Lab Report Grading Criteria
      • LAB Report Instruction
      • LAB: Grayscale Image Segmentation
        • LAB: Grayscale Image Segmentation -Gear
        • LAB: Grayscale Image Segmentation - Bolt and Nut
      • LAB: Color Image Segmentation
        • LAB: Facial Temperature Measurement with IR images
        • LAB: Magic Cloak
      • LAB: Straight Lane Detection and Departure Warning
      • LAB: Dimension Measurement with 2D camera
      • LAB: Tension Detection of Rolling Metal Sheet
  • Deep Learning for Perception
    • Notes
      • Lane Detection with Deep Learning
      • Overview of Deep Learning
        • Object Detection
        • Deep Learning Basics: Introduction
        • Deep Learning State of the Art
        • CNN, Object Detection
      • Perceptron
      • Activation Function
      • Optimization
      • Convolution
      • CNN Overview
      • Evaluation Metric
      • LossFunction Regularization
      • Bias vs Variance
      • BottleNeck Unit
      • Object Detection
      • DL Techniques
        • Technical Strategy by A.Ng
    • Tutorial - PyTorch
      • Tutorial: Install PyTorch
      • Tutorial: Python Numpy
      • Tutorial: PyTorch Tutorial List
      • Tutorial: PyTorch Example Code
      • Tutorial: Tensorboard in Pytorch
      • Tutorial: YOLO in PyTorch
        • Tutorial: Yolov8 in PyTorch
        • Tutorial: Train Yolo v8 with custom dataset
          • Tutorial: Train Yolo v5 with custom dataset
        • Tutorial: Yolov5 in Pytorch (VS code)
        • Tutorial: Yolov3 in Keras
    • LAB
      • Assignment: CNN Classification
      • Assignment: Object Detection
      • LAB: CNN Object Detection 1
      • LAB: CNN Object Detection 2
      • LAB Grading Criteria
    • Tutorial- Keras
      • Train Dataset
      • Train custom dataset
      • Test model
      • LeNet-5 Tutorial
      • AlexNet Tutorial
      • VGG Tutorial
      • ResNet Tutorial
    • Resource
      • Online Lecture
      • Programming tutorial
      • Books
      • Hardware
      • Dataset
      • Useful sites
  • Must Read Papers
    • AlexNet
    • VGG
    • ResNet
    • R-CNN, Fast-RCNN, Faster-RCNN
    • YOLOv1-3
    • Inception
    • MobileNet
    • SSD
    • ShuffleNet
    • Recent Methods
  • DLIP Project
    • Report Template
    • DLIP 2021 Projects
      • Digital Door Lock Control with Face Recognition
      • People Counting with YOLOv4 and DeepSORT
      • Eye Blinking Detection Alarm
      • Helmet-Detection Using YOLO-V5
      • Mask Detection using YOLOv5
      • Parking Space Management
      • Vehicle, Pedestrian Detection with IR Image
      • Drum Playing Detection
      • Turtle neck measurement program using OpenPose
    • DLIP 2022 Projects
      • BakeryCashier
      • Virtual Mouse
      • Sudoku Program with Hand gesture
      • Exercise Posture Assistance System
      • People Counting Embedded System
      • Turtle neck measurement program using OpenPose
    • DLIP Past Projects
  • Installation Guide
    • Installation Guide for Pytorch
      • Installation Guide 2021
    • Anaconda
    • CUDA cuDNN
      • CUDA 10.2
    • OpenCV
      • OpenCV Install and Setup
        • OpenCV 3.4.13 with VS2019
        • OpenCV3.4.7 VS2017
        • MacOS OpenCV C++ in XCode
      • Python OpenCV
      • MATLAB-OpenCV
    • Framework
      • Keras
      • TensorFlow
        • Cheat Sheet
        • Tutorial
      • PyTorch
    • IDE
      • Visual Studio Community
      • Google Codelab
      • Visual Studio Code
        • Python with VS Code
        • Notebook with VS Code
        • C++ with VS Code
      • Jupyter Notebook
        • Install
        • How to use
    • Ubuntu
      • Ubuntu 18.04 Installation
      • Ubuntu Installation using Docker in Win10
      • Ubuntu Troubleshooting
    • ROS
  • Programming
    • Python_Numpy
      • Python Tutorial - Tips
      • Python Tutorial - For Loop
      • Python Tutorial - List Tuple, Dic, Set
    • Markdown
      • Example: API documentation
    • Github
      • Create account
      • Tutorial: Github basic
      • Tutorial: Github Desktop
    • Keras
      • Tutorial Keras
      • Cheat Sheet
    • PyTorch
      • Cheat Sheet
      • Autograd in PyTorch
      • Simple ConvNet
      • MNIST using LeNet
      • Train ConvNet using CIFAR10
  • Resources
    • Useful Resources
    • Github
Powered by GitBook
On this page
  • Tutorial: Spatial Filter
  • I. Introduction
  • II. Tutorial
  • Convolution for Spatial Filter
  • OpenCV: Filtering
  • III. Exercise
  • Exercise 1
  • Exercise 2
  • Resource

Was this helpful?

  1. Image Processing Basics
  2. Tutorial

Tutorial: Spatial Filter

PreviousTutorial: Image Watch for DebuggingNextTutorial: Thresholding and Morphology

Last updated 1 month ago

Was this helpful?

Tutorial: Spatial Filter

Deep Learning Image Processing.

Updated. 2024.3

PDF version:

I. Introduction

In this tutorial, you will learn how to design various spatial filters in OpenCV. You will learn which filters to apply for Gaussian and impulse noises and how to combine various filters to enhance the noise-corrupted images. You are required to create a camera application that allows the user to select the type and size of the image filter.

The correlation of spatial filtering by kernel w(s,t) on the image f(x,y) to obtain the output image g(x,y) is represented as

Convolution 1 GIF

II. Tutorial

Convolution for Spatial Filter

We will learn how the filter convolution works by programming in MATLAB.

Then, we will learn how to use filter functions in OpenCV.

An example of convolution with arbitrary values : No Padding with stride 1

Exercise

  1. Download the example code and test images.

  1. Write a simple program that applies a filter mask w(s,t) to image f(x,y) to result the output image g(x,y).

Note that the index of an array in a program starts from ‘0’ (C/C++) or ‘1 (MATLAB)

You need to increase the size of the source image around the border by applying zero padding.

Depending on the type of the filter mask w(s,t), you can obtain smoothing or sharpening filter.

% u,v: image points
% s,t: kernel points
% w: kernel, w(s,t)
% f: source image, f(x,y)

clc; clear all; close all;

% image read
f = imread('Pattern_GaussNoise.jpg');
f = rgb2gray(f);
[M, N] = size(f);

% define window
w =[1 1 1 ;  1 1 1 ;  1 1 1];
[wM, wN] = size(w);
wSum = sum(w(:));
if(wSum == 0)
    wSum = 1;
end

%Padding
% e.g. 3 x 3 Filter -> pad 1 each side or 2 total
b = (wM - 1) / 2; % b: yPad
a = (wN - 1) / 2; % a: xPad

% fPad image index: [1 to M+(2*b)] x [1 to N+(2*a)]
fPad = zeros(M+wM-1,N+wN-1);
fPad(a+1:a+M,b+1:b+N) = double(f);

% apply 2D-convolution
gPad = zeros(size(fPad));
tic

for v = b+1:b+M  
    for u = a+1:a+N
        % convolution of kernel at one point (v,u)
        conv = 0;
        for t = -b:b
            for s = -a:a
                 % your code goes here
				 % your code goes here
            end
        end
        gPad(v,u) = conv / wSum;
    end
end


g = gPad(b+1:b+M, a+1:a+N); % cropping
toc

figure, imshow(f)
figure, imshow(uint8(fPad))
figure, imshow(uint8(g))

OpenCV: Filtering

Example 1. Normalized Block Filter

In this tutorial, you will learn how to apply diverse linear filters to smooth images using OpenCV functions such as: blur, GaussianBlur, medianBlur

int i = 3;
blur( src, dst, Size( i, i ), Point(-1,-1) );

We specify arguments :

  • src: Source image

  • dst: Destination image

  • Size( w,h ): Defines the size of the kernel to be used ( of width w pixels and height h pixels)

  • Point(-1, -1): Indicates where the anchor point (the pixel evaluated) is located with respect to the neighborhood. If there is a negative value, then the center of the kernel is considered the anchor point.

Example 2. Convolution with Filter Kernel

OpenCV offers the function filter2D() for Filter Kernel Convolution.

void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT )

filter2D(src, dst, ddepth , kernel, anchor);

Example 3. Gaussian Filter

void cv::GaussianBlur	(	
    InputArray 	src,
    OutputArray 	dst,
    Size 	ksize,
    double 	sigmaX,
    double 	sigmaY = 0,
    int 	borderType = BORDER_DEFAULT 
)	
    
int i = 3;
GaussianBlur( src, dst, Size( i, i ), 0, 0 );
blur = cv.GaussianBlur(img,(5,5),0)
  • Size(w, h): The size of the kernel to be used (the neighbors to be considered). w and h have to be odd and positive numbers.

  • sigmaX= The standard deviation in x. Writing 0 implies that it is calculated using kernel

  • sigmaY=The standard deviation in y. Writing 0 implies that it is calculated using kernel

Example 4. Median Filter

// C++
void cv::medianBlur	(	InputArray 	src,
OutputArray 	dst,
int 	ksize 
)	

int i = 3;
medianBlur ( src, dst, i );
# Python
median = cv.medianBlur(img,5)
  • i: The size of the kernel (there is only one because we use a square window). It must be odd.

Example 5. Bilateral Filter

// C++

void cv::bilateralFilter	(	
    InputArray 	src,
    OutputArray 	dst,
    int 	d,
    double 	sigmaColor,
    double 	sigmaSpace,
    int 	borderType = BORDER_DEFAULT 
)	
    
int i = 3;
bilateralFilter ( src, dst, i, i*2, i/2 );
# Python
blur = cv.bilateralFilter(img,9,75,75)
  • d: The diameter of each pixel neighborhood.

  • sigmaColor: Standard deviation in the color space.

  • sigmaSpace: Standard deviation in the coordinate space (in pixel terms)

Example 6: Laplacian operator (Sharpening Filter)

void cv::Laplacian	(	
    InputArray 	src,
    OutputArray 	dst,
    int 	ddepth,
    int 	ksize = 1,
    double 	scale = 1,
    double 	delta = 0,
    int 	borderType = BORDER_DEFAULT 
)	
    
int kernel_size = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
Laplacian( src, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
src.convertTo(src, CV_16S);
result_laplcaian = src - dst;
result_laplcaian.convertTo(result_laplcaian, CV_8U);
  • ddepth: Depth of the destination image. Since our input is CV_8U we define ddepth = CV_16S to avoid overflow

  • kernel_size: The kernel size of the Sobel operator to be applied internally. We use 3 in this example.

  • scale, delta and BORDER_DEFAULT: We leave them as default values.

III. Exercise

Exercise 1

Create a new C++ project in Visual Studio Community

  • Project Name: DLIP_Tutorial_Filter_Image

  • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  • Source File: DLIP_Tutorial_Filter_Image.cpp

Download the example code and test images.

Using the provided sample code, apply the following filters to all test images.

Choose appropriate filter to each image and explain why.

  • blur()

  • GaussianBlur()

  • medianBlur()

  • filter2D() : Design a normalized box filter kernel 5 by 5

  • Laplacian()

Show the result images to TA

Exercise 2

Create a camera(webcam) application that has filtering function.

Create a new C++ project in Visual Studio Community

  • Project Name: DLIP_Tutorial_Filter_Vid

  • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  • Source File: DLIP_Tutorial_Filter_Vid.cpp

Download the exercise code

You should make a keyboard input menu to let the user choose the type and size of the filter.

  • Filter options: Gaussian, Laplacian(3x3), Median, others..

  • Example: 'B' for blur with Gaussian, 'L' for Laplacian, 'M' for Median

  • Let the user also select the size of Gaussian Filter(bluriness)

    • Example: As 'UP' key is pressed, filter kernel_size is increased from 3, 5, 7, 9 , ....

Show the result images to TA.

Resource

An example code of Spatial Filter with OpenCV-C++

An example code of Spatial Filter with OpenCV-Python

ConvGif2

First, read the OpenCV 4.9.0 documentation

OpenCV offers the function to perform smoothing with the normalized box filter.

It is performed by the function :

This filter is provided by the function:

Provided by OpenCV function

Calculates the Laplacian of an image using

source code: Matlab filter tutorial
test images
link
blur()
GaussianBlur()
medianBlur()
bilateralFilter()
blur
GaussianBlur
medianBlur
bilateralFilter
Laplacian()
Example code: Filter_demo_student.cpp
Test images
Example code: Webcam Filter Demo
Example code: Spatial filter demo
Example code: Spatial filter Python demo
click here to download