ivan@128
|
1 %% Dictionary Learning for Image Denoising - SPAMS parameter test
|
idamnjanovic@25
|
2 %
|
ivan@128
|
3 % *WARNING!* You should have SPAMS in your search path in order for this
|
ivan@128
|
4 % script to work.Due to licensing issues SPAMS can not be automatically
|
ivan@128
|
5 % provided in SMALLbox (http://www.di.ens.fr/willow/SPAMS/downloads.html).
|
ivan@128
|
6 %
|
ivan@107
|
7 % This file contains an example of how SMALLbox can be used to test different
|
ivan@107
|
8 % dictionary learning techniques in Image Denoising problem.
|
ivan@107
|
9 % This example can be used to test SPAMS for different values of
|
ivan@107
|
10 % parameter lambda. In no way it represents extensive testing of image
|
ivan@107
|
11 % denoising. It should only give an idea how SMALL structure can be used
|
ivan@107
|
12 % for testing.
|
ivan@107
|
13
|
ivan@107
|
14 %
|
idamnjanovic@25
|
15 % Centre for Digital Music, Queen Mary, University of London.
|
idamnjanovic@25
|
16 % This file copyright 2010 Ivan Damnjanovic.
|
idamnjanovic@25
|
17 %
|
idamnjanovic@25
|
18 % This program is free software; you can redistribute it and/or
|
idamnjanovic@25
|
19 % modify it under the terms of the GNU General Public License as
|
idamnjanovic@25
|
20 % published by the Free Software Foundation; either version 2 of the
|
idamnjanovic@25
|
21 % License, or (at your option) any later version. See the file
|
idamnjanovic@25
|
22 % COPYING included with this distribution for more information.
|
idamnjanovic@6
|
23 %%
|
idamnjanovic@6
|
24
|
idamnjanovic@6
|
25 clear all;
|
idamnjanovic@6
|
26
|
idamnjanovic@6
|
27 %% Load an image
|
idamnjanovic@6
|
28 TMPpath=pwd;
|
idamnjanovic@6
|
29 FS=filesep;
|
luis@186
|
30 [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m'));
|
idamnjanovic@6
|
31 cd([pathstr1,FS,'data',FS,'images']);
|
idamnjanovic@6
|
32 [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
|
luis@186
|
33 [pathstr, name, ext] = fileparts(filename);
|
idamnjanovic@6
|
34 test_image = imread(filename);
|
idamnjanovic@6
|
35 test_image = double(test_image);
|
idamnjanovic@6
|
36 cd(TMPpath);
|
idamnjanovic@6
|
37 %%
|
idamnjanovic@6
|
38
|
idamnjanovic@6
|
39 % number of different values we want to test
|
idamnjanovic@6
|
40
|
idamnjanovic@6
|
41 n =4;
|
idamnjanovic@6
|
42
|
idamnjanovic@6
|
43 lambda=zeros(1,n);
|
idamnjanovic@6
|
44 time = zeros(2,n);
|
idamnjanovic@6
|
45 psnr = zeros(2,n);
|
idamnjanovic@6
|
46
|
idamnjanovic@6
|
47 for i=1:n
|
idamnjanovic@6
|
48
|
idamnjanovic@6
|
49 % Here we want to test time spent and quality of denoising for
|
idamnjanovic@6
|
50 % different lambda parameters.
|
idamnjanovic@6
|
51
|
idamnjanovic@6
|
52 lambda(i)=1+i*0.5;
|
idamnjanovic@6
|
53
|
idamnjanovic@6
|
54 % Defining Image Denoising Problem as Dictionary Learning Problem.
|
idamnjanovic@6
|
55
|
idamnjanovic@6
|
56 SMALL.Problem = generateImageDenoiseProblem(test_image);
|
idamnjanovic@6
|
57 SMALL.Problem.name=name;
|
idamnjanovic@6
|
58 %%
|
idamnjanovic@6
|
59 % Use SPAMS Online Dictionary Learning Algorithm
|
idamnjanovic@6
|
60 % to Learn overcomplete dictionary (Julien Mairal 2009)
|
idamnjanovic@6
|
61
|
idamnjanovic@6
|
62 % Initialising Dictionary structure
|
idamnjanovic@6
|
63 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
idamnjanovic@6
|
64 % to zero values
|
idamnjanovic@6
|
65
|
idamnjanovic@6
|
66 SMALL.DL(1)=SMALL_init_DL();
|
idamnjanovic@6
|
67
|
idamnjanovic@6
|
68 % Defining fields needed for dictionary learning
|
idamnjanovic@6
|
69
|
idamnjanovic@6
|
70 SMALL.DL(1).toolbox = 'SPAMS';
|
idamnjanovic@6
|
71 SMALL.DL(1).name = 'mexTrainDL';
|
idamnjanovic@6
|
72
|
idamnjanovic@6
|
73 % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters.
|
idamnjanovic@6
|
74
|
idamnjanovic@6
|
75 SMALL.DL(1).param=struct(...
|
idamnjanovic@6
|
76 'D', SMALL.Problem.initdict,...
|
idamnjanovic@6
|
77 'K', SMALL.Problem.p,...
|
idamnjanovic@6
|
78 'lambda', lambda(i),...
|
idamnjanovic@6
|
79 'iter', 200,...
|
idamnjanovic@6
|
80 'mode', 3,...
|
idamnjanovic@6
|
81 'modeD', 0);
|
idamnjanovic@6
|
82
|
idamnjanovic@6
|
83 % Learn the dictionary
|
idamnjanovic@6
|
84
|
idamnjanovic@6
|
85 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
|
idamnjanovic@6
|
86
|
idamnjanovic@6
|
87 % Set SMALL.Problem.A dictionary
|
idamnjanovic@6
|
88 % (backward compatiblity with SPARCO: solver structure communicate
|
idamnjanovic@6
|
89 % only with Problem structure, ie no direct communication between DL and
|
idamnjanovic@6
|
90 % solver structures)
|
idamnjanovic@6
|
91
|
idamnjanovic@6
|
92 SMALL.Problem.A = SMALL.DL(1).D;
|
ivan@161
|
93 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
|
ivan@107
|
94
|
ivan@107
|
95 %%
|
ivan@107
|
96 % Initialising solver structure
|
ivan@107
|
97 % Setting solver structure fields (toolbox, name, param, solution,
|
ivan@107
|
98 % reconstructed and time) to zero values
|
ivan@107
|
99
|
ivan@107
|
100 SMALL.solver(1)=SMALL_init_solver;
|
ivan@107
|
101
|
ivan@107
|
102 % Defining the parameters needed for image denoising
|
ivan@107
|
103 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
|
ivan@107
|
104 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
|
ivan@107
|
105
|
ivan@107
|
106 SMALL.solver(1).toolbox='ompbox';
|
ivan@107
|
107 SMALL.solver(1).name='omp2';
|
ivan@107
|
108 SMALL.solver(1).param=struct(...
|
ivan@107
|
109 'epsilon',Edata,...
|
ivan@107
|
110 'maxatoms', maxatoms);
|
ivan@107
|
111
|
ivan@107
|
112 % Denoising the image - find the sparse solution in the learned
|
ivan@107
|
113 % dictionary for all patches in the image and the end it uses
|
ivan@107
|
114 % reconstruction function to reconstruct the patches and put them into a
|
ivan@107
|
115 % denoised image
|
ivan@107
|
116
|
ivan@107
|
117 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
|
idamnjanovic@6
|
118
|
idamnjanovic@6
|
119 %% show results %%
|
idamnjanovic@6
|
120 % This will show denoised image and dictionary for all lambdas. If you
|
idamnjanovic@6
|
121 % are not interested to see it and do not want clutter your screen
|
idamnjanovic@6
|
122 % comment following line
|
idamnjanovic@6
|
123
|
idamnjanovic@6
|
124 SMALL_ImgDeNoiseResult(SMALL);
|
idamnjanovic@6
|
125
|
idamnjanovic@6
|
126
|
idamnjanovic@6
|
127 time(1,i) = SMALL.DL(1).time;
|
idamnjanovic@6
|
128 psnr(1,i) = SMALL.solver(1).reconstructed.psnr;
|
idamnjanovic@6
|
129
|
idamnjanovic@6
|
130 clear SMALL
|
idamnjanovic@6
|
131 end
|
idamnjanovic@6
|
132
|
idamnjanovic@6
|
133 %% show time and psnr %%
|
idamnjanovic@6
|
134 figure('Name', 'SPAMS LAMBDA TEST');
|
idamnjanovic@6
|
135
|
idamnjanovic@6
|
136 subplot(1,2,1); plot(lambda, time(1,:), 'ro-');
|
idamnjanovic@6
|
137 title('time vs lambda');
|
idamnjanovic@6
|
138 subplot(1,2,2); plot(lambda, psnr(1,:), 'b*-');
|
idamnjanovic@6
|
139 title('PSNR vs lambda');
|
idamnjanovic@6
|
140
|