Reconstruct image with coefficients after thresholding

5 views (last 30 days)
Ana Gabriela Guedes
Ana Gabriela Guedes on 28 Nov 2021
Commented: yanqi liu on 29 Nov 2021
Hi! I have an exercise in which I had to decompose a noisy image at level 2 using the Haar wavelet and then extract the approximation coefficients and the horizontal, vertical and diagonal detail coefficients. After this we had to preform a hard thresholding (set the detail coefficients at all levels with the absolute value less than the threshold to 0).
My code for all this part is the following:
% b) Wavelet decomposition with Haar wavelet
[C S] = wavedec2(noiseI,2,'haar');
%Extract approximation and detail coefficients.
[detHor1,detVert1,detDiag1] = detcoef2('all',C,S,1); % Level 1 detail coefficients
App1 = appcoef2(C,S,'haar',1); % Level 1 approximation coefficients
[detHor2,detVert2,detDiag2] = detcoef2('all',C,S,2); % Level 2 detail coefficients
App2 = appcoef2(C,S,'haar',2); % Level 2 approximation coefficients
% Took this code from the documentation but don't really know what is this for
iVert1 = wcodemat(detVert1,255,'mat',1);
iHor1 = wcodemat(detHor1,255,'mat',1);
iDiag1 = wcodemat(detDiag1,255,'mat',1);
iApp1 = wcodemat(App1,255,'mat',1);
iVert2 = wcodemat(detVert2,255,'mat',1);
iHor2 = wcodemat(detHor2,255,'mat',1);
iDiag2 = wcodemat(detDiag2,255,'mat',1);
iApp2 = wcodemat(App2,255,'mat',1);
% Hard thresholding
thr = 0.02
H1thres = wthresh(iHor1,'h',thr);
V1thres = wthresh(iVert1,'h',thr);
D1thres = wthresh(iDiag1,'h',thr);
A1thres = wthresh(iApp1,'h',thr);
H2thres = wthresh(iHor2,'h',thr);
V2thres = wthresh(iVert2,'h',thr);
D2thres = wthresh(iDiag2,'h',thr);
A2thres = wthresh(i2,'h',thr);
% All of this was then plotted
After this the exercise asks for reconstruct the image with the coefficients after thresholding using waverec2 and display 4 subplots in one figure, including the original image, the noisy image, the denoised image and the difference of the original image and the denoised image.However, because I have so many coefficients I am not sure of what to put as inputs in the waverec2 function. Can someone please help me? (I am not fully sure if the rest of the code is or not correct tho)
Thank you a lot in advance!

Answers (1)

yanqi liu
yanqi liu on 29 Nov 2021
clc; clear all; close all;
I = imread('cameraman.tif');
I2 = imnoise(I, 'poisson');
I3=data_filter(I2);
figure;
montage({I, I2, I3}, 'Size', [1 3], 'BackgroundColor', 'r', 'BorderSize', [7 7])
function s3=data_filter(s)
% wavedec
sz=size(s);
s=double(s(:)');
[c,l]=wavedec(s,3,'db1');
a3=appcoef(c,l,'db1',3);
d3=detcoef(c,l,3);
d2=detcoef(c,l,2);
d1=detcoef(c,l,1);
N1 = 10;
sigma1=median(abs(d1))/0.6745;
thr1=(sigma1*sqrt(2*(log10(N1))))/(log10(2));
% hard thresholding
ythard1=wthresh(d1,'h',thr1);
sigma2=median(abs(d2))/0.6745;
thr2=(sigma2*sqrt(2*(log10(N1))))/(log10(3));
ythard2=wthresh(d2,'h',thr2);
sigma3=median(abs(d3))/0.6745;
thr3=(sigma3*sqrt(2*(log10(N1))))/(log10(4));
ythard3=wthresh(d3,'h',thr3);
c2=[a3 ythard3 ythard2 ythard1];
s3=waverec(c2,l,'db1');
s3=reshape(s3,sz(1),sz(2));
s3=im2uint8(mat2gray(s3));
end
  3 Comments
yanqi liu
yanqi liu on 29 Nov 2021
clc; clear all; close all;
X = imread('cameraman.tif');
figure;
subplot(1, 3, 1); imshow(X,[]);
title('origin image');
init = 0;
randn('seed', init);
% noise
XX = imnoise(X, 'gaussian');
subplot(1, 3, 2); imshow(XX,[]);
title('noise image');
% de noise
[c,s]=wavedec2(X,2,'haar');
[thr,sorh,keepapp]=ddencmp('den','wv',XX);
[XX2,cxc,lxc,perf0,perfl2]=wdencmp('gbl',c,s,'haar',2,thr,sorh,keepapp);
subplot(1, 3, 3); imshow(XX2,[]);
title('denoise image');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!