Reconstruct image with coefficients after thresholding
6 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
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
on 29 Nov 2021
Edited: yanqi liu
on 29 Nov 2021
sorry,sir,may be use hard and soft to compare,such as
clc; clear all; close all;
X = imread('cameraman.tif');
figure;
subplot(2, 4, 1); imshow(X,[]);
title('origin image');
init = 0;
randn('seed', init);
% noise
XX = imnoise(X, 'poisson');
subplot(2, 4, 2); imshow(XX,[]);
title('noise image');
% wavedec2
[c, l] = wavedec2(XX, 2, 'haar');
n = [1, 2];
p = [10.28, 24.08];
% wthcoef2 h
nc1 = wthcoef2('h', c, l, n, p, 's');
% wthcoef2 v
nc2 = wthcoef2('v', c, l, n, p, 's');
% wthcoef2 d
nc3 = wthcoef2('d', c, l, n, p, 's');
% waverec2
X1 = waverec2(nc1, l, 'haar');
X2 = waverec2(nc2, l, 'haar');
X3 = waverec2(nc3, l, 'haar');
subplot(2, 4, 3); imshow(X1,[]);
title('waverec2-h-hard');
subplot(2, 4, 4); imshow(X2,[]);
title('waverec2-v-hard');
subplot(2, 4, 5); imshow(X3,[]);
title('waverec2-d-hard');
% wthcoef2 h s
mc1 = wthcoef2('h', nc1, l, n, p,'s');
% wthcoef2 v s
mc2 = wthcoef2('v', nc2, l, n, p,'s');
% wthcoef2 d s
mc3 = wthcoef2('d', nc3, l, n, p,'s');
% waverec2
X4 = waverec2(mc1, l, 'haar');
X5 = waverec2(mc2, l, 'haar');
X6 = waverec2(mc3, l, 'haar');
subplot(2, 4, 6); imshow(X4,[]);
title('waverec2-h-soft');
subplot(2, 4, 7); imshow(X5,[]);
title('waverec2-v-soft');
subplot(2, 4, 8); imshow(X6,[]);
title('waverec2-d-soft');
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');
See Also
Categories
Find more on Denoising and Compression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!