annotate examples/Image Denoising/SMALL_ImgDenoise_DL_test_SPAMS_lambda.m @ 6:f72603404233

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