Filters B - MedianBlur and Bilateral
In previous chapter, we looked into the Homogeneous and Gaussian blurring filters.
- Homogeneous Smoothing
- Gaussian Smoothing
- Median Smoothing
- Bilateral Smoothing
In this chapter, we will see the rest of the two: Median and Bilateral filters.
Here is the image we've been using:
The Median filter is a common technique for smoothing. Median smoothinging is widely used in edge detection algorithms because under certain conditions, it preserves edges while removing noise.
#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 ); // Median smoothing medianBlur( src, dst, i ); // show the blurred image with the text imshow( "Median filter", dst ); // wait for 5 seconds waitKey(5000); } }
The syntax of the blur() looks like this:
void medianBlur(InputArray src, OutputArray dst, int ksize)
The parameters are:
- src - input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be CV_8U, CV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.
- dst - destination array of the same size and type as src.
- ksize - aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...
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} )
So far, we reviewed 3 image filters. Bilateral blurring is one of the most advanced filter to smooth an image and reduce noise. The other three filters will smooth away the edges while removing noises, however, this filter can reduce noise of the image while preserving the edges. The drawback of this type of filter is that it takes longer to filter the input image.
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:
vvoid bilateralFilter(InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT )
Parameters:
- src - Source 8-bit or floating-point, 1-channel or 3-channel image.
- dst - Destination image of the same size and type as src.
- d - Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigmaSpace.
- sigmaColor - Filter sigma in the color space. A larger value of the parameter means that farther colors within the pixel neighborhood will be mixed together, resulting in larger areas of semi-equal color.
- sigmaSpace - Filter sigma in the coordinate space. A larger value of the parameter means that farther pixels will influence each other as long as their colors are close enough. When d>0 , it specifies the neighborhood size regardless of sigmaSpace . Otherwise, d is proportional to sigmaSpace.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8) project( bilateral ) find_package( OpenCV REQUIRED ) add_executable( bilateral b.cpp ) target_link_libraries( bilateral ${OpenCV_LIBS} )
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization