Clear Filters
Clear Filters

Understand the algorithm for " wdenoise2(​img_hazy,'​ThresholdR​ule','Hard​') "

2 views (last 30 days)
Sir,
I need to understand the equations relating to " wdenoise2(img_hazy,'ThresholdRule','Hard');" and write a code myself, to reproduce the results shown by wdenoise2 function with threshold rule as hard. The algorithm that I tried to implement, along with the input image are attached herewith and the matlab code is shown below. BUT THE SSIM=1 is not obtained. Can someone help me to trace out what is wrong.
%-----------------------------------------------------
clear all;
close all;
clc;
%-----------------------------------------------------
img= imread('check.png');
img = imresize(img,0.125);
img=imnoise(img(:,:),'gaussian',0,0.01);
HH_fn=wdenoise2(img,'ThresholdRule','Hard');
%-----------------------------------------------------
[M N]=size(img(:,:));
L1=floor(log2(min([M N])));
L2=wmaxlev([M N],'bior4.4');
Level=min(L1,L2);
%-----------------------------------------------------
[LL,HL,LH,HH] = dwt2(img(:,:),'bior4.4','mode', 'sym');
T=bayes_shrink(HH);
HH=hthresh(HH,T);
HL=hthresh(HL,T);
LH=hthresh(LH,T);
LL=hthresh(LL,T);
recon=idwt2(LL,HL,LH,HH,'bior4.4','mode','sym',size(img));
S1=ssim(recon,HH_fn)
%-----------------------------------------------------
function threshold=bayes_shrink(X)
len=prod(size(X));
sigmay2=sum(X.^2,'all')/len;
sigmahat=median(abs(X),'all')/0.6745;
sigmax=sqrt(max(sigmay2-sigmahat.^2,0));
threshold=(sigmahat.^2)./sigmax;
end
%-----------------------------------------------------
function oph=hthresh(X,T);
%A function to perform hard thresholding on a given an input vector X with a given threshold T
ind=find(abs(X)<=T);
ind1=find(abs(X)>T);
X(ind)=0;
X(ind1)=X(ind1);
oph=X;
end

Answers (1)

Siraj
Siraj on 28 Aug 2023
Hii! It is my understanding that you have implemented the “wdenoise2” algorithm and now wants to compare the results of your own implementation vs the results of MATLAB inbuilt function “wdenoise2”.
I believe that the algorithm that you have attached is correctly implemented in your given code, but the SSIM (Structural Similarity Index) is not 1 for the results produced by the internal “wdenoise2” function and your own implementation.
I ran your code multiple times and have few observations that might be useful.
  1. The “HH_fn” changes every time you run the code even if the input is same.
  2. The “recon” changes every time you run the code even if the input is same.
  3. To avoid the above you can reset the random number seed. https://in.mathworks.com/help/matlab/ref/rng.html
  4. Floating point numbers have limited precision therefore we might not get SSIM as exact 1.
  5. Even though we get different values of SSIM for the same input, the value is always above 0.92 which I believe is a good similarity between 2 results.
Refer to the following documentations for more information.
Hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!