In the previous post we have seen how to manually define custom filters.
We made use of filter2D function with Gaussian kernel and Blur operator to demonstrate working of this function.
Opencv provides predefined functions for most common operators like blur operator, Gaussian operator, in this post we are going to discuss how to make use of this interface.
Results:
We made use of filter2D function with Gaussian kernel and Blur operator to demonstrate working of this function.
Opencv provides predefined functions for most common operators like blur operator, Gaussian operator, in this post we are going to discuss how to make use of this interface.
- Blur operator
We have already discussed this filter in this post. - Gaussian Filter
Gaussian filter is discussed here. - Median Filter
As the name suggests median operator takes median of all the elements covered by mask, and replaces center pixel with this median.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include "cv.h" | |
#include "highgui.h" | |
#include <cstring> | |
using namespace cv; | |
using namespace std; | |
int main() | |
{ | |
string path = "res/download.jpg"; | |
Mat src = imread(path,CV_LOAD_IMAGE_ANYCOLOR); | |
if(!src.data) | |
{ | |
cout<<"fail\n"; | |
return -1; | |
} | |
Mat blurBlock, gaussian, median, bilateral; | |
int kernel_size = 5; | |
// Blur operator. | |
// Size defines size of kernel | |
// Point is an anchor point , and here point -1,-1 defines a center point of the mask. | |
// BORDER_DEFAULT specifies the border type for padding image. | |
// For more explanation please read my post on filter2d. | |
// Blur operator. | |
blur(src,blurBlock,Size(kernel_size,kernel_size), Point(-1,-1), BORDER_DEFAULT); | |
// Gaussian Filter | |
// The two 0s after Size(kernel_size,kernel_size) specify the valuse of sigma1 and sigma2. | |
// Here 0 and 0 represent that value of sigmax and sigmay should be calculated from the dimension of kernel | |
GaussianBlur(src,gaussian,Size(kernel_size,kernel_size), 0, 0, BORDER_DEFAULT); | |
// Median Blur | |
// Default size of kernel. | |
medianBlur(src, median, kernel_size); | |
// Display the results | |
namedWindow("Input",CV_WINDOW_AUTOSIZE); | |
namedWindow("Block",CV_WINDOW_AUTOSIZE); | |
namedWindow("Gaussian",CV_WINDOW_AUTOSIZE); | |
namedWindow("Median",CV_WINDOW_AUTOSIZE); | |
imshow("Input",src); | |
imshow("Block",blurBlock); | |
imshow("Gaussian",gaussian); | |
imshow("Median",median); | |
// wait for keypress event. | |
waitKey(0); | |
return 0; | |
} |
No comments:
Post a Comment