Filters A - Average and GaussianBlur
- Homogeneous Smoothing
- Gaussian Smoothing
- Median Smoothing
- Bilateral Smoothing
Here is the image we're going to play with.
After loading an image, this code applies a linear image filter and show the filtered images sequentially.
The filter used here the most simplest one called homogeneous smoothing or box filter.
It does smoothing by sliding a kernel (filter) across the image. Each pixel value will be calculated based on the value of the kernel and the overlapping pixel's value of the original image. Mathematically speaking, we do convolution operation on an image with a kernel. What kernel we're applying to an image makes difference to the the result of the smoothing. What we do for this filter is assigning an average values of a pixel's neighbors.
We need to choose right size of the kernel. If it's too large, it may blur and remove small features of the image. But if it is too small, we may not be able to eliminate noises of the image.
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui_c.h> using namespace cv; int main(int argc, char** argv) { namedWindow("Before" , CV_WINDOW_AUTOSIZE); // Load the source image Mat src = imread( "/home/khong/OpenCV/workspace/images/zebra.jpg", 1); // Create a destination Mat object Mat dst; // display the source image imshow("Before", src); for (int i=1; i<51; i=i+2) { //smooth the image in the "src" and save it to "dst" blur(src, dst, Size(i,i)); //show the blurred image with the text imshow( "Smoothing by avaraging", dst ); //wait for 3 seconds waitKey(3000); } }
The syntax of the blur() looks like this:
void blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT )
The parameters are:
- src - input image; it can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
- dst - output image of the same size and type as src.
- ksize - blurring kernel size.
- anchor - anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
- borderType - border mode used to extrapolate pixels outside of the image.
In our code, kernel size we're using increased from 1x1 to 49x49. We see the smoothed image when we increase the kernel size.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8) project( average ) find_package( OpenCV REQUIRED ) add_executable( average a.cpp ) target_link_libraries( average ${OpenCV_LIBS} )
This is the most commonly used blurring method. We can use this filter to eliminate noises in an image. We need to very careful in choosing the size of the kernel and the standard deviation of the Gaussian distribution in x and y direction should be chosen carefully.
Here is the code using the Gaussian blur:
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui_c.h> using namespace cv; int main(int argc, char** argv) { namedWindow("Before" , CV_WINDOW_AUTOSIZE); // Load the source image Mat src = imread( "/home/khong/OpenCV/workspace/images/zebra.jpg", 1); // Create a destination Mat object Mat dst; // display the source image imshow("Before", src); for (int i=1; i<51; i=i+2) { // smooth the image in the "src" and save it to "dst" // blur(src, dst, Size(i,i)); // Gaussian smoothing GaussianBlur( src, dst, Size( i, i ), 0, 0 ); //show the blurred image with the text imshow( "Gaussian filter", dst ); //wait for 5 seconds waitKey(5000); } }
GaussianBlur() syntax:
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT )
Parameters:
- src - input image; the image can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
- dst - output image of the same size and type as src.
- ksize - Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd. Or, they can be zeroâs and then they are computed from sigma*.
- sigmaX - Gaussian kernel standard deviation in X direction.
- sigmaY - Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height , respectively; to fully control the result regardless of possible future modifications of all this semantics, it is recommended to specify all of ksize, sigmaX, and sigmaY.
- borderType - pixel extrapolation method.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8) project( gaussian ) find_package( OpenCV REQUIRED ) add_executable( gaussian g.cpp ) target_link_libraries( gaussian ${OpenCV_LIBS} )
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization