Guanssian Filter

来源:互联网 发布:js escape替代方法 编辑:程序博客网 时间:2024/06/05 04:13

in the case of a Gaussian filter, the weight associated with a pixel is proportional to its distance from the central pixel. Recall that the 1D Gaussian function has the following form:


G(x)=Ae^(x^2/2σ^2)



 

The normalizing coefficient A is chosen such that the different weights sum to one. The σ (sigma) value controls the width of the resulting Gaussian function. The greater this value is, the flatter the function will be. For example, if we compute the coefficients of the 1D Gaussian filter for the interval [-4,...,0,...4] with σ=0.5, we obtain:


[0.0 0.0 0.00026 0.10645 0.78657 0.10645 0.00026 0.0 0.0]


While for σ=1.5 these coefficients are:
   [0.00761 0.036075 0.10959 0.21345 0.26666
    0.21345 0.10959 0.03608 0.00761 ]


Note that these values were obtained by calling the cv::getGaussianKernel function with the appropriate σ value:
   cv::Mat gauss= cv::getGaussianKernel(9,sigma,CV_32F);



To apply a 2D Gaussian filter on an image, one can simply apply a 1D Gaussian filter on the image lines first (which will filter the horizontal frequencies) followed by the application of the same 1D Gaussian filter on the image columns (to filter the vertical frequencies). This is possible because the Gaussian filter is a separable filter (that is, the 2D kernel can be decomposed into two 1D filters). The function cv::sepFilter2D can be used to apply a general separable filter. It is also possible to directly apply a 2D kernel using cv::filter2Dfunction.


With OpenCV, the Gaussian filter to be applied on an image is specified by providing to cv::GaussianBlur both the number of coefficients(third parameter, an odd number and positive )
and the value of σ (fourth parameter). You can also simply set the value of σ and let OpenCV determine the appropriate number of coefficients (you then input a value of 0 for the filter size). The opposite is also possible, where you input a size and a value of 0 for σ. The σ value that best fits the given size will be determined. However, it recommended that you input both values for a better control of the filter effect.



Low-pass filters are also used when an image is resized. Suppose you want to reduce the size of an image by a factor of 2. You might think that this can simply be done by eliminating the even columns and rows of the image. Unfortunately, the resulting image will not look very nice. For example, an oblique edge in the original image will appear as a staircase on the reduced image. Other jagged distortions will also be visible on curves and textured parts of the image.


These undesirable artifacts are caused by a phenomenon called spatial aliasing that occurs when you try to include high-frequency components in an image that is too small to contain them.Indeed, smaller images (that is, images with fewer pixels) cannot represent fine textures and sharp edges as nicely as the higher resolution images (think of the difference between high-definition TV versus conventional TV).Since fine details in an image correspond to high frequencies, we'll need to remove those higher frequency components in an image before reducing its size.We learned in this recipe that this can be done through a low-pass filter. Consequently, to reduce the size of an image by half without adding annoying artifacts, you must first apply a low-pass filter to the original image and then throw away one column and row over two. This is exactly what the cv::pyrDown function does:


cv::Mat reducedImage;  // to contain reduced image
   cv::pyrDown(image,reducedImage); // reduce image size by half


This one uses a 5x5 Gaussian filter to low-pass the image. The reciprocal cv::pyrUp function that doubles the size of an image also exists. Of course, if you downsize an image and then upsize it, you will not recover the exact original image. What was lost during the downsizing process cannot be recovered. These two functions are used to create image pyramids. This is a data structure made of stacked versions of an image at different sizes (often each level is half the size of the previous level) that is often built for efficient image analysis. For example, if one wishes to detect an object in an image, the detection can be first accomplished on
the small image at the top of the pyramid, and as you locate the object of interest, you can refine the search by moving to the lower levels of the pyramid containing the higher resolution versions of the image.
Note that there is also a more general cv:resize function that allows you to specify the size you want for the resulting image. You simply call it by specifying a new size that could be smaller or larger than the original image:
   cv::Mat resizedImage;  // to contain resized image
   cv::resize(image,resizedImage,
       cv::Size(image.cols/3,image.rows/3)); // 1/3 resizing
Other options are available to specify resizing in terms of scale factors, or to select a particular interpolation method to be used in the resampling process.




















0 0
原创粉丝点击