annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_SPAMS_lambda.m @ 50:d5155eaa3f68

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