双边滤波基于小波变换的多尺度自适应THZ增强双边滤波器的MATLAB仿真
Posted fpga&matlab
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双边滤波基于小波变换的多尺度自适应THZ增强双边滤波器的MATLAB仿真相关的知识,希望对你有一定的参考价值。
1.软件版本
MATLAB2021a
2.本算法理论知识
提出了一种”基于小波变换的多尺度自适应双边滤波器“算法。
其对应的算法流程如下所示:
下面,我们从理论上限介绍一下这里所采用的改进后的算法。
第一:多尺度图像的自适应双边滤波
这个部分,是我们这里所需要研究的创新的算法,这里重点从理论上介绍一下。
首先通过图像亮度,将图像区分为前景图和背景图。
这里,我们主要通过二值话处理,进行图像的前景和背景的区分,这个部分的理论为:
这里,门限T的计算,我们主要通过matlab自带的一个函数获得,这个函数会根据每个图像自动计算出门限T。
graythresh
对于的代码为:
然后分别对前景和背景进行sigma参数的自适应调整。
这里,sigma的计算公式为:
这里,我们根据上面的背景前景,做如下的设置。
这里,我们分别对前景和背景下乘以系数K1和K2,其中根据亮度分布(亮的为背景、暗的为物体)来确定BF的各像素的两个sigma值,让亮区域平滑更多(选用大sigma),暗区域平滑偏小(小sigma)。
3.核心代码
% Pre-process input and select appropriate filter.
function B = bfilter2(A,w,sigma)
% 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(:)) < 0 || max(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 || w < 1
w = 5;
end
w = ceil(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
B = bfltGray(A,w,sigma(1),sigma(2));
else
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);
G = exp(-(X.^2+Y.^2)/(2*sigma_d^2));
% 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 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);
4.操作步骤与仿真结论
5.参考文献
A25-09
6.完整源码获得方式
订阅MATLAB/FPGA教程,免费获得教程案例以及任意2份完整源码
以上是关于双边滤波基于小波变换的多尺度自适应THZ增强双边滤波器的MATLAB仿真的主要内容,如果未能解决你的问题,请参考以下文章
图像增强基于matlab GUI图像双边滤波含Matlab源码 1342期
图像增强基于matlab双边滤波retinex算法暗光图像增强含Matlab源码 2305期
图像增强基于matlab双边滤波retinex算法暗光图像增强含Matlab源码 2305期