-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbFilter.m
More file actions
44 lines (36 loc) · 1.12 KB
/
Copy pathbFilter.m
File metadata and controls
44 lines (36 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function outIm = bFilter(im, w, sigmDist, sigmInt)
%Bilateral filtering for grayscale images
% % Inputs:
% im -- input image
% w -- window size (window will be -w:w, -w:w)
% sigmDist -- spatial domain standard deviation
% sigmInt -- intensity domain standard deviation
% % Outputs:
% outIm -- output image
% Normalize image to [0,1]
minIm=min(im(:));
maxIm=max(im(:));
im = im - minIm;
im = im./maxIm;
%Calculate gaussian distance weights
[X,Y]=meshgrid(-w:w, -w:w);
G = exp(-(X.^2 + Y.^2)/(2*sigmDist^2));
outIm=zeros(size(im));
[R,C]=size(im);
for i=1:R
for j=1:C
% Extract local region
iMin=max(i-w, 1);
iMax=min(i+w, R);
jMin=max(j-w, 1);
jMax=min(j+w, C);
localReg = im(iMin:iMax, jMin:jMax);
% Compute Gaussian intensity weights
H=exp(-(localReg-im(i,j)).^2./(2*sigmInt^2));
% Calculate bilateral filter response
F=H.*G((iMin:iMax)-i+w+1, (jMin:jMax)-j+w+1);
outIm(i,j) = sum(F(:).*localReg(:))/sum(F(:));
end
end
outIm=outIm.*maxIm;
outIm=outIm+minIm;