Matlab simulation of image bilateral filtering algorithm

Table of contents

1. Theoretical basis

2. Core program

3. Simulation conclusion


1. Theoretical basis

       Image bilateral filtering is a commonly used image filtering technique, which can smooth the image and preserve the edge information of the image. However, the traditional bilateral filtering algorithm has a large amount of calculation when processing large-scale images, resulting in slow processing speed. In order to solve this problem, researchers proposed a fast bilateral filtering algorithm for images. This article will introduce the principle and implementation steps of the image fast bilateral filtering algorithm.

       The bilateral filtering algorithm uses a two-dimensional Gaussian filter to smooth the image, and adjusts the degree of smoothing according to the position of the pixel and the difference between the pixel values, thereby preserving the edge information of the image.

       The image fast bilateral filtering algorithm is an improved bilateral filtering algorithm, which uses a fast approximate Gaussian filtering method, thus speeding up the filtering process. Specifically, the image fast bilateral filtering algorithm can be divided into the following steps:
1. Decompose the input image $I_{\text{in}}$ into images of different scales $I_{\text{in}}^{(i) }$, where $i$ represents the scale.
2. Perform Gaussian filtering on the image $I_{\text{in}}^{(i)}$ of each scale to obtain the image $G_{\sigma}^{(i)}$.
3. Perform bilateral filtering on the image $G_{\sigma}^{(i)}$ of each scale to obtain the image $B_{\sigma}^{(i)}$.
4. Perform weighted average of images $B_{\sigma}^{(i)}$ of different scales to obtain the final output image $I_{\text{out}}$.
Among them, the parameters of Gaussian filtering and bilateral filtering can be adjusted as required.
       The principle of the image fast bilateral filtering algorithm is to use the Gaussian pyramid structure to decompose the image into images of different scales, then perform bilateral filtering on the images of each scale, and finally perform weighted average of the images of different scales to obtain the final output image. Using this method can speed up the filtering process while preserving the edge information of the image. 

       The image fast bilateral filtering algorithm uses the Gaussian pyramid structure to decompose the image into images of different scales, then performs bilateral filtering on the images of each scale, and finally performs weighted average of the images of different scales to obtain the final output image. Using this method can speed up the filtering process while preserving the edge information of the image.

       In the filtering algorithm, the pixel value of the target point is usually determined by the value of a small local neighbor pixel around its location. The specific implementation in 2D Gaussian filtering is to assign different Gaussian weight values ​​to the surrounding pixel values ​​within a certain range, and obtain the final result of the current point after weighted average. The Gaussian weighting factor here is generated using the spatial distance (2D in the image) relationship between two pixels. Through the curve of the Gaussian distribution, it can be found that the closer the point is to the target pixel, the greater the contribution to the final result, and vice versa. Its formulaic description is generally as follows:

       Gaussian filtering has a good performance in the low-pass filtering algorithm, but it has another problem, that is, only the spatial position relationship between pixels is considered, so the filtering result will lose edge information. The edge here mainly refers to the main different color areas in the image (such as blue sky, black hair, etc.), and Bilateral adds another weight division to Gaussian blur to solve this problem. The preservation of the edge in Bilateral filtering is realized by the following expression: 

2. Core program

function [ outImg , param ]  =  shiftable_jointBF(inImg, rangeImg, sigmaS, sigmaR, w, tol)
 

% create spatial filter
filt     = fspecial('gaussian', [w w], sigmaS);

% set range interval and the order of raised cosine
T  =  maxFilter(rangeImg, w);
N  =  ceil( 0.405 * (T / sigmaR)^2 );

gamma    =  1 / (sqrt(N) * sigmaR);
twoN     =  2^N;

% compute truncation
if tol == 0
    M = 0;
else
    if sigmaR > 40
        M = 0;
    elseif sigmaR > 10
        sumCoeffs = 0;
        for k = 0 : round(N/2)
            sumCoeffs = sumCoeffs + nchoosek(N,k)/twoN;
            if sumCoeffs > tol/2
                M = k;
                break;
            end
        end
    else
        M = ceil( 0.5 * ( N - sqrt(4*N*log(2/tol)) ) );
    end
end


% main filter
[m, n, b]  =  size(inImg);
outImg1    =  zeros(m, n, b);
outImg2    =  zeros(m, n, b);
outImg     =  zeros(m, n, b);

warning off; %#ok<WNOFF>

for k = M : N - M
    
    coeff = nchoosek(N,k) / twoN;
    
    temp1  = cos( (2*k - N) * gamma * rangeImg);
    temp2  = sin( (2*k - N) * gamma * rangeImg);
    
    if size(inImg, 3) > 1
      temp1 = repmat(temp1, [1 1 size(inImg, 3)]);
      temp2 = repmat(temp2, [1 1 size(inImg, 3)]);
    end
    
    phi1 =  imfilter(inImg .* temp1, filt);
    phi2 =  imfilter(inImg .* temp2, filt);
    phi3 =  imfilter(temp1, filt);
    phi4 =  imfilter(temp2, filt);
    
    outImg1 = outImg1 + coeff * ( temp1 .* phi1 +  temp2 .* phi2 );
    outImg2 = outImg2 + coeff * ( temp1 .* phi3 +  temp2 .* phi4 );
    
end

idx1 = find( outImg2 < 0.0001);
idx2 = find( outImg2 > 0.0001);

outImg( idx1 ) = inImg( idx1 );
outImg( idx2 ) = outImg1( idx2 ) ./ outImg2 (idx2 );

% save parameters
param.T  = T;
param.N  = N;
param.M  = M;

up171

3. Simulation conclusion

 

 

Guess you like

Origin blog.csdn.net/ccsss22/article/details/130498526