luis@236
|
1 %% Dictionary Learning for Image Denoising - KSVD vs KSVDS vs SPAMS
|
luis@236
|
2 %
|
luis@236
|
3 % *WARNING!* You should have SPAMS in your search path in order for this
|
luis@236
|
4 % script to work.Due to licensing issues SPAMS can not be automatically
|
luis@236
|
5 % provided in SMALLbox (http://www.di.ens.fr/willow/SPAMS/downloads.html).
|
luis@236
|
6 %
|
luis@236
|
7 % This file contains an example of how SMALLbox can be used to test different
|
luis@236
|
8 % dictionary learning techniques in Image Denoising problem.
|
luis@236
|
9 % It calls generateImageDenoiseProblem that will let you to choose image,
|
luis@236
|
10 % add noise and use noisy image to generate training set for dictionary
|
luis@236
|
11 % learning.
|
luis@236
|
12 % Two dictionary learning techniques were compared:
|
luis@236
|
13 % - KSVD - M. Elad, R. Rubinstein, and M. Zibulevsky, "Efficient
|
luis@236
|
14 % Implementation of the K-SVD Algorithm using Batch Orthogonal
|
luis@236
|
15 % Matching Pursuit", Technical Report - CS, Technion, April 2008.
|
luis@236
|
16 % - KSVDS - R. Rubinstein, M. Zibulevsky, and M. Elad, "Learning Sparse
|
luis@236
|
17 % Dictionaries for Sparse Signal Approximation", Technical
|
luis@236
|
18 % Report - CS, Technion, June 2009.
|
luis@236
|
19 %
|
luis@236
|
20
|
luis@236
|
21 %
|
luis@236
|
22 % Centre for Digital Music, Queen Mary, University of London.
|
luis@236
|
23 % This file copyright 2009 Ivan Damnjanovic.
|
luis@236
|
24 %
|
luis@236
|
25 % This program is free software; you can redistribute it and/or
|
luis@236
|
26 % modify it under the terms of the GNU General Public License as
|
luis@236
|
27 % published by the Free Software Foundation; either version 2 of the
|
luis@236
|
28 % License, or (at your option) any later version. See the file
|
luis@236
|
29 % COPYING included with this distribution for more information.
|
luis@236
|
30 %
|
luis@236
|
31 %%
|
luis@236
|
32
|
luis@236
|
33 clear;
|
luis@236
|
34
|
luis@236
|
35 % If you want to load the image outside of generateImageDenoiseProblem
|
luis@236
|
36 % function uncomment following lines. This can be useful if you want to
|
luis@236
|
37 % denoise more then one image for example.
|
luis@236
|
38
|
luis@236
|
39 % TMPpath=pwd;
|
luis@236
|
40 % FS=filesep;
|
luis@236
|
41 % [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m'));
|
luis@236
|
42 % cd([pathstr1,FS,'data',FS,'images']);
|
luis@236
|
43 % [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes');
|
luis@236
|
44 % [pathstr, name, ext] = fileparts(filename);
|
luis@236
|
45 % test_image = imread(filename);
|
luis@236
|
46 % test_image = double(test_image);
|
luis@236
|
47 % cd(TMPpath);
|
luis@236
|
48 % SMALL.Problem.name=name;
|
luis@236
|
49
|
luis@236
|
50
|
luis@236
|
51 % Defining Image Denoising Problem as Dictionary Learning
|
luis@236
|
52 % Problem. As an input we set the number of training patches.
|
luis@236
|
53
|
luis@236
|
54 SMALL.Problem = generateImageDenoiseProblem('', 40000);
|
luis@236
|
55
|
luis@236
|
56
|
luis@236
|
57 %%
|
luis@236
|
58 % Use KSVD Dictionary Learning Algorithm to Learn overcomplete dictionary
|
luis@236
|
59
|
luis@236
|
60 % Initialising Dictionary structure
|
luis@236
|
61 % Setting Dictionary structure fields (toolbox, name, param, D and time)
|
luis@236
|
62 % to zero values
|
luis@236
|
63
|
luis@236
|
64 SMALL.DL(1)=SMALL_init_DL();
|
luis@236
|
65
|
luis@236
|
66 % Defining the parameters needed for dictionary learning
|
luis@236
|
67
|
luis@236
|
68 SMALL.DL(1).toolbox = 'KSVD';
|
luis@236
|
69 SMALL.DL(1).name = 'ksvd';
|
luis@236
|
70
|
luis@236
|
71 % Defining the parameters for KSVD
|
luis@236
|
72 % In this example we are learning 256 atoms in 20 iterations, so that
|
luis@236
|
73 % every patch in the training set can be represented with target error in
|
luis@236
|
74 % L2-norm (EData)
|
luis@236
|
75 % Type help ksvd in MATLAB prompt for more options.
|
luis@236
|
76
|
luis@236
|
77 Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
|
luis@236
|
78 maxatoms = floor(prod(SMALL.Problem.blocksize)/2);
|
luis@236
|
79
|
luis@236
|
80 SMALL.DL(1).param=struct(...
|
luis@236
|
81 'Edata', Edata,...
|
luis@236
|
82 'initdict', SMALL.Problem.initdict,...
|
luis@236
|
83 'dictsize', SMALL.Problem.p,...
|
luis@236
|
84 'iternum', 20,...
|
luis@236
|
85 'memusage', 'high');
|
luis@236
|
86
|
luis@236
|
87 % Learn the dictionary
|
luis@236
|
88
|
luis@236
|
89 SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1));
|
luis@236
|
90
|
luis@236
|
91 % Set SMALL.Problem.A dictionary
|
luis@236
|
92 % (backward compatiblity with SPARCO: solver structure communicate
|
luis@236
|
93 % only with Problem structure, ie no direct communication between DL and
|
luis@236
|
94 % solver structures)
|
luis@236
|
95
|
luis@236
|
96 SMALL.Problem.A = SMALL.DL(1).D;
|
luis@236
|
97 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem);
|
luis@236
|
98
|
luis@236
|
99 %%
|
luis@236
|
100 % Initialising solver structure
|
luis@236
|
101 % Setting solver structure fields (toolbox, name, param, solution,
|
luis@236
|
102 % reconstructed and time) to zero values
|
luis@236
|
103
|
luis@236
|
104 SMALL.solver(1)=SMALL_init_solver;
|
luis@236
|
105
|
luis@236
|
106 % Defining the parameters needed for image denoising
|
luis@236
|
107
|
luis@236
|
108 SMALL.solver(1).toolbox='ompbox';
|
luis@236
|
109 SMALL.solver(1).name='omp2';
|
luis@236
|
110 SMALL.solver(1).param=struct(...
|
luis@236
|
111 'epsilon',Edata,...
|
luis@236
|
112 'maxatoms', maxatoms);
|
luis@236
|
113
|
luis@236
|
114 % Denoising the image - find the sparse solution in the learned
|
luis@236
|
115 % dictionary for all patches in the image and the end it uses
|
luis@236
|
116 % reconstruction function to reconstruct the patches and put them into a
|
luis@236
|
117 % denoised image
|
luis@236
|
118
|
luis@236
|
119 SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1));
|
luis@236
|
120
|
luis@236
|
121 % Show PSNR after reconstruction
|
luis@236
|
122
|
luis@236
|
123 SMALL.solver(1).reconstructed.psnr
|
luis@236
|
124
|
luis@236
|
125 %%
|
luis@236
|
126 % Use KSVDS Dictionary Learning Algorithm to denoise image
|
luis@236
|
127
|
luis@236
|
128 % Initialising solver structure
|
luis@236
|
129 % Setting solver structure fields (toolbox, name, param, solution,
|
luis@236
|
130 % reconstructed and time) to zero values
|
luis@236
|
131
|
luis@236
|
132 SMALL.DL(2)=SMALL_init_DL();
|
luis@236
|
133
|
luis@236
|
134 % Defining the parameters needed for dictionary learning
|
luis@236
|
135
|
luis@236
|
136 SMALL.DL(2).toolbox = 'KSVDS';
|
luis@236
|
137 SMALL.DL(2).name = 'ksvds';
|
luis@236
|
138
|
luis@236
|
139 % Defining the parameters for KSVDS
|
luis@236
|
140 % In this example we are learning 256 atoms in 20 iterations, so that
|
luis@236
|
141 % every patch in the training set can be represented with target error in
|
luis@236
|
142 % L2-norm (EDataS). We also impose "double sparsity" - dictionary itself
|
luis@236
|
143 % has to be sparse in the given base dictionary (Tdict - number of
|
luis@236
|
144 % nonzero elements per atom).
|
luis@236
|
145 % Type help ksvds in MATLAB prompt for more options.
|
luis@236
|
146
|
luis@236
|
147 EdataS=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain;
|
luis@236
|
148 SMALL.DL(2).param=struct(...
|
luis@236
|
149 'Edata', EdataS, ...
|
luis@236
|
150 'Tdict', 6,...
|
luis@236
|
151 'stepsize', 1,...
|
luis@236
|
152 'dictsize', SMALL.Problem.p,...
|
luis@236
|
153 'iternum', 20,...
|
luis@236
|
154 'memusage', 'high');
|
luis@236
|
155 SMALL.DL(2).param.initA = speye(SMALL.Problem.p);
|
luis@236
|
156 SMALL.DL(2).param.basedict{1} = odctdict(8,16);
|
luis@236
|
157 SMALL.DL(2).param.basedict{2} = odctdict(8,16);
|
luis@236
|
158
|
luis@236
|
159 % Learn the dictionary
|
luis@236
|
160
|
luis@236
|
161 SMALL.DL(2) = SMALL_learn(SMALL.Problem, SMALL.DL(2));
|
luis@236
|
162
|
luis@236
|
163 % Set SMALL.Problem.A dictionary and SMALL.Problem.basedictionary
|
luis@236
|
164 % (backward compatiblity with SPARCO: solver structure communicate
|
luis@236
|
165 % only with Problem structure, ie no direct communication between DL and
|
luis@236
|
166 % solver structures)
|
luis@236
|
167
|
luis@236
|
168 SMALL.Problem.A = SMALL.DL(2).D;
|
luis@236
|
169 SMALL.Problem.basedict{1} = SMALL.DL(2).param.basedict{1};
|
luis@236
|
170 SMALL.Problem.basedict{2} = SMALL.DL(2).param.basedict{2};
|
luis@236
|
171
|
luis@236
|
172 % Setting up reconstruction function
|
luis@236
|
173
|
luis@236
|
174 SparseDict=1;
|
luis@236
|
175 SMALL.Problem.reconstruct = @(x) ImageDenoise_reconstruct(x, SMALL.Problem, SparseDict);
|
luis@236
|
176
|
luis@236
|
177 % Initialising solver structure
|
luis@236
|
178 % Setting solver structure fields (toolbox, name, param, solution,
|
luis@236
|
179 % reconstructed and time) to zero values
|
luis@236
|
180
|
luis@236
|
181 SMALL.solver(2)=SMALL_init_solver;
|
luis@236
|
182
|
luis@236
|
183 % Defining the parameters needed for image denoising
|
luis@236
|
184
|
luis@236
|
185 SMALL.solver(2).toolbox='ompsbox';
|
luis@236
|
186 SMALL.solver(2).name='omps2';
|
luis@236
|
187 SMALL.solver(2).param=struct(...
|
luis@236
|
188 'epsilon',Edata,...
|
luis@236
|
189 'maxatoms', maxatoms);
|
luis@236
|
190
|
luis@236
|
191 % Denoising the image - find the sparse solution in the learned
|
luis@236
|
192 % dictionary for all patches in the image and the end it uses
|
luis@236
|
193 % reconstruction function to reconstruct the patches and put them into a
|
luis@236
|
194 % denoised image
|
luis@236
|
195
|
luis@236
|
196 SMALL.solver(2)=SMALL_solve(SMALL.Problem, SMALL.solver(2));
|
luis@236
|
197
|
luis@236
|
198
|
luis@236
|
199 %%
|
luis@236
|
200 % Plot results and save midi files
|
luis@236
|
201
|
luis@236
|
202 % show results %
|
luis@236
|
203
|
luis@236
|
204 SMALL_ImgDeNoiseResult(SMALL);
|