バイラテラルフィルター-Matlabの実装
Bilateral Filter Matlab Implementation
例:最初に使用する バイラテラルフィルタリング フィルタ(BF)は、元の画像をフィルタリングして低周波数部分を取得し、元の画像と低周波数の差を取得します 高周波成分 、 高周波成分 そして、低周波成分は個別に強化されてから合成されます。
バイラテラルフィルタリング この機能は、エッジを維持するノイズ除去です。ガウスフィルタリングと比較して、画像を滑らかにし、画像のエッジの保護を強化します。主な理由は、フィルターが2つの部分で構成されており、1つはピクセルの空間距離に関連し、もう1つはピクセルのピクセル差に関連しているためです。
なぜ式で話しましょう バイラテラルフィルタリング 画像をぼかすときのエッジ保存機能があり、 バイラテラルフィルタリング 式は次のとおりです。
その中で、空間近接係数は
明るさの相似係数は
バイラテラルフィルターの重みは、空間近接係数と輝度類似係数の積に等しくなります。
空間近接係数はガウスフィルター係数です。ピクセル距離が遠いほど、重みは小さくなります。 値が大きいほど、スムージング効果が明確になります。
明るさの類似性係数は、空間ピクセルの違いに関連しています。ピクセル差が大きいほど、重みは小さくなります。これが、バイラテラルフィルターがエッジとノイズ除去を維持できる理由です。いつ 値が大きいほど、同じグレースケールの差があるピクセルに対するスムージング効果が大きくなり、エッジ保存効果が悪くなります。論文で与えられた参照は
一般的に取られる ガウスノイズ 標準偏差の2倍。
Matlabのコードを以下に示します。コードのこの部分は、グレースケール画像とカラー画像を処理できます。コードのダウンロードアドレスには、Mathworksアカウントが必要です。
https://cn.mathworks.com/matlabcentral/fileexchange/12191-bilateral-filtering
バイラテラルフィルター
function B = bfilter2(A,w,sigma) %A is the given image, normalized to the double matrix of [0,1] %W is the side length of the bilateral filter (kernel)/2 % Definition domain variance σd is recorded as SIGMA(1), and range variance σr is recorded as SIGMA(2) % This function implements 2-D bilateral filtering using % the method outlined in: % % C. Tomasi and R. Manduchi. Bilateral Filtering for % Gray and Color Images. In Proceedings of the IEEE % International Conference on Computer Vision, 1998. % % B = bfilter2(A,W,SIGMA) performs 2-D bilateral filtering % for the grayscale or color image A. A should be a double % precision matrix of size NxMx1 or NxMx3 (i.e., grayscale % or color images, respectively) with normalized values in % the closed interval [0,1]. The half-size of the Gaussian % bilateral filter window is defined by W. The standard % deviations of the bilateral filter are given by SIGMA, % where the spatial-domain standard deviation is given by % SIGMA(1) and the intensity-domain standard deviation is % given by SIGMA(2). % % Douglas R. Lanman, Brown University, September 2006. % root@xxxxx, http://mesh.brown.edu/dlanman %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Pre-process input and select appropriate filter. % Verify that the input image exists and is valid. if ~exist('A','var') || isempty(A) error('Input image A is undefined or invalid.') end if ~isfloat(A) || ~sum([1,3] == size(A,3)) || ... min(A(:)) 1 error(['Input image A must be a double precision ',... 'matrix of size NxMx1 or NxMx3 on the closed ',... 'interval [0,1].']) end % Verify bilateral filter window size. if ~exist('w','var') || isempty(w) || ... Numel(w)~==1||ww<1%Calculate the number of elements in the array w = 5 end w = ceil(w)% is the smallest integer greater than w % Verify bilateral filter standard deviations. if ~exist('sigma','var') || isempty(sigma) || ... numel(sigma) ~= 2 || sigma(1) <= 0 || sigma(2) <= 0 sigma = [3 0.1] end % Apply either grayscale or color bilateral filtering. if size(A,3)===1% If the input image is a grayscale image, the grayscale image filtering method is called B = bfltGray(A,w,sigma(1),sigma(2)) else If the input image is a color image, the color image filtering method is called B = bfltColor(A,w,sigma(1),sigma(2)) end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Implements bilateral filtering for grayscale images. function B = bfltGray(A,w,sigma_d,sigma_r) % Pre-compute Gaussian distance weights. [X,Y] = meshgrid(-w:w,-w:w) %Create a kernel distance matrix, e.g. % [x,y]=meshgrid(-1:1,-1:1) % % x = % % -1 0 1 % -1 0 1 % -1 0 1 % % % y = % % -1 -1 -1 % 0 0 0 % 1 1 1 %Calculate the definition domain core G = exp(-(X.^2+Y.^2)/(2*sigma_d^2)) % Create waitbar. The calculation process is slow, you can see the progress in real time when creating waitbar h = waitbar(0,'Applying bilateral filter...') set(h,'Name','Bilateral Filter Progress') % Apply bilateral filter. %Calculate the range kernel H and multiply it with the domain kernel G to get the bilateral weight function F dim = size(A)% Get the width and height of the input image B = zeros(dim) for i = 1:dim(1) for j = 1:dim(2) % Extract local region. iMin = max(i-w,1) iMax = min(i+w,dim(1)) jMin = max(j-w,1) jMax = min(j+w,dim(2)) % Defines the area where the current nuclear role is (iMin:iMax,jMin:jMax) I = A(iMin:iMax,jMin:jMax) %Extract the source image value of the area and assign it to I % Compute Gaussian intensity weights. H = exp(-(I-A(i,j)).^2/(2*sigma_r^2)) % Calculate bilateral filter response. F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1) B(i,j) = sum(F(:).*I(:))/sum(F(:)) end waitbar(i/dim(1)) end % Close waitbar. close(h) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Implements bilateral filter for color images. function B = bfltColor(A,w,sigma_d,sigma_r) % Convert input sRGB image to CIELab color space. if exist('applycform','file') A = applycform(A,makecform('srgb2lab')) else A = colorspace('Lab<-RGB',A) end % Pre-compute Gaussian domain weights. [X,Y] = meshgrid(-w:w,-w:w) G = exp(-(X.^2+Y.^2)/(2*sigma_d^2)) % Rescale range variance (using maximum luminance). sigma_r = 100*sigma_r % Create waitbar. h = waitbar(0,'Applying bilateral filter...') set(h,'Name','Bilateral Filter Progress') % Apply bilateral filter. dim = size(A) B = zeros(dim) for i = 1:dim(1) for j = 1:dim(2) % Extract local region. iMin = max(i-w,1) iMax = min(i+w,dim(1)) jMin = max(j-w,1) jMax = min(j+w,dim(2)) I = A(iMin:iMax,jMin:jMax,:) % Compute Gaussian range weights. dL = I(:,:,1)-A(i,j,1) da = I(:,:,2)-A(i,j,2) db = I(:,:,3)-A(i,j,3) H = exp(-(dL.^2+da.^2+db.^2)/(2*sigma_r^2)) % Calculate bilateral filter response. F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1) norm_F = sum(F(:)) B(i,j,1) = sum(sum(F.*I(:,:,1)))/norm_F B(i,j,2) = sum(sum(F.*I(:,:,2)))/norm_F B(i,j,3) = sum(sum(F.*I(:,:,3)))/norm_F end waitbar(i/dim(1)) end % Convert filtered image back to sRGB color space. if exist('applycform','file') B = applycform(B,makecform('lab2srgb')) else B = colorspace('RGB<-Lab',B) end % Close waitbar. close(h)
メイン機能:
clear all Image_pri = imread('academy.jpg') Image_normalized = im2double(Image_pri) w = 5% window size sigma = [3 0.1]% variance Image_bf = bfilter2(Image_normalized,w,sigma) Image_bfOut = uint8(Image_bf*255) figure(1) subplot(1,2,1) imshow(Image_pri) subplot(1,2,2) imshow(Image_bfOut) filter_gaussian = fspecial('gaussian',[5,5],3)% generated gaussian space wave filter gaussian_image = imfilter(Image_pri,filter_gaussian,'replicate') figure(2) subplot(1,2,1) imshow(Image_pri) subplot(1,2,2) imshow(gaussian_image)