-
Notifications
You must be signed in to change notification settings - Fork 7
/
imageDenoisingExample.m
60 lines (47 loc) · 1.45 KB
/
imageDenoisingExample.m
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
44
45
46
47
48
49
50
51
52
53
54
55
56
% This script illustrates image denoisning using Gibbs Sample
%
% (image in this example is taken from Statistical Machine Learning
% course taught by Frank Wood in Spring 2012 at Columbia University)
% load data
data = load('data');
realImg = data.img;
noisyImg = data.noisy_img;
% parameters of Gibbs Sample
couplingStrength = -1;
externalStrength = -1;
nBurnin = 100;
nSamples = 100;
nThin = 3;
% samples after burnin and thinning
samples = gibbsIsingModel(noisyImg,couplingStrength,externalStrength,...
nSamples,nBurnin,nThin);
% vizualise noisy image
figure(1)
imshow(noisyImg)
title('Noisy Image')
% vizualise last sample
figure(2)
imshow(samples(:,:,nSamples))
title('Denoised Image, sample from posterior')
% vizualise noisy and denoised images side by side
sbs = ones(300,603);
sbs(:,1:300) = noisyImg;
sbs(:,304:603) = samples(:,:,nSamples);
imshow(sbs)
title('Image Denoising: Before and After')
% vizualise picture without noise
figure(3)
imshow(realImg)
title('Real Image, without noise')
% compute proportion of 'wrong' pixels in each sample
err = zeros(1,nSamples);
for i = 1:nSamples
err(i) = wrongPixels(samples(:,:,i),realImg);
end
[R,C] = size(realImg);
nPixels = R*C;
err = err / nPixels;
% plot histogram of number of wrong pixels
figure(4)
hist(err)
title('Proportion of wrong pixels in samples. Initial noise = 0.0973')