idamnjanovic@6: %% DICTIONARY LEARNING FOR IMAGE DENOISING idamnjanovic@25: % ivan@107: % ivan@107: % This file contains an example of how SMALLbox can be used to test different ivan@107: % dictionary learning techniques in Image Denoising problem. ivan@107: % This example can be used to test SPAMS for different values of ivan@107: % parameter lambda. In no way it represents extensive testing of image ivan@107: % denoising. It should only give an idea how SMALL structure can be used ivan@107: % for testing. ivan@107: ivan@107: % idamnjanovic@25: % Centre for Digital Music, Queen Mary, University of London. idamnjanovic@25: % This file copyright 2010 Ivan Damnjanovic. idamnjanovic@25: % idamnjanovic@25: % This program is free software; you can redistribute it and/or idamnjanovic@25: % modify it under the terms of the GNU General Public License as idamnjanovic@25: % published by the Free Software Foundation; either version 2 of the idamnjanovic@25: % License, or (at your option) any later version. See the file idamnjanovic@25: % COPYING included with this distribution for more information. idamnjanovic@6: %% idamnjanovic@6: idamnjanovic@6: clear all; idamnjanovic@6: idamnjanovic@6: %% Load an image idamnjanovic@6: TMPpath=pwd; idamnjanovic@6: FS=filesep; idamnjanovic@6: [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); idamnjanovic@6: cd([pathstr1,FS,'data',FS,'images']); idamnjanovic@6: [filename,pathname] = uigetfile({'*.png;'},'Select a file containin pre-calculated notes'); idamnjanovic@6: [pathstr, name, ext, versn] = fileparts(filename); idamnjanovic@6: test_image = imread(filename); idamnjanovic@6: test_image = double(test_image); idamnjanovic@6: cd(TMPpath); idamnjanovic@6: %% idamnjanovic@6: idamnjanovic@6: % number of different values we want to test idamnjanovic@6: idamnjanovic@6: n =4; idamnjanovic@6: idamnjanovic@6: lambda=zeros(1,n); idamnjanovic@6: time = zeros(2,n); idamnjanovic@6: psnr = zeros(2,n); idamnjanovic@6: idamnjanovic@6: for i=1:n idamnjanovic@6: idamnjanovic@6: % Here we want to test time spent and quality of denoising for idamnjanovic@6: % different lambda parameters. idamnjanovic@6: idamnjanovic@6: lambda(i)=1+i*0.5; idamnjanovic@6: idamnjanovic@6: % Defining Image Denoising Problem as Dictionary Learning Problem. idamnjanovic@6: idamnjanovic@6: SMALL.Problem = generateImageDenoiseProblem(test_image); idamnjanovic@6: SMALL.Problem.name=name; idamnjanovic@6: %% idamnjanovic@6: % Use SPAMS Online Dictionary Learning Algorithm idamnjanovic@6: % to Learn overcomplete dictionary (Julien Mairal 2009) idamnjanovic@6: idamnjanovic@6: % Initialising Dictionary structure idamnjanovic@6: % Setting Dictionary structure fields (toolbox, name, param, D and time) idamnjanovic@6: % to zero values idamnjanovic@6: idamnjanovic@6: SMALL.DL(1)=SMALL_init_DL(); idamnjanovic@6: idamnjanovic@6: % Defining fields needed for dictionary learning idamnjanovic@6: idamnjanovic@6: SMALL.DL(1).toolbox = 'SPAMS'; idamnjanovic@6: SMALL.DL(1).name = 'mexTrainDL'; idamnjanovic@6: idamnjanovic@6: % Type 'help mexTrainDL in MATLAB prompt for explanation of parameters. idamnjanovic@6: idamnjanovic@6: SMALL.DL(1).param=struct(... idamnjanovic@6: 'D', SMALL.Problem.initdict,... idamnjanovic@6: 'K', SMALL.Problem.p,... idamnjanovic@6: 'lambda', lambda(i),... idamnjanovic@6: 'iter', 200,... idamnjanovic@6: 'mode', 3,... idamnjanovic@6: 'modeD', 0); idamnjanovic@6: idamnjanovic@6: % Learn the dictionary idamnjanovic@6: idamnjanovic@6: SMALL.DL(1) = SMALL_learn(SMALL.Problem, SMALL.DL(1)); idamnjanovic@6: idamnjanovic@6: % Set SMALL.Problem.A dictionary idamnjanovic@6: % (backward compatiblity with SPARCO: solver structure communicate idamnjanovic@6: % only with Problem structure, ie no direct communication between DL and idamnjanovic@6: % solver structures) idamnjanovic@6: idamnjanovic@6: SMALL.Problem.A = SMALL.DL(1).D; ivan@107: SMALL.Problem.reconstruct = @(x) ImgDenoise_reconstruct(x, SMALL.Problem); ivan@107: ivan@107: %% ivan@107: % Initialising solver structure ivan@107: % Setting solver structure fields (toolbox, name, param, solution, ivan@107: % reconstructed and time) to zero values ivan@107: ivan@107: SMALL.solver(1)=SMALL_init_solver; ivan@107: ivan@107: % Defining the parameters needed for image denoising ivan@107: Edata=sqrt(prod(SMALL.Problem.blocksize)) * SMALL.Problem.sigma * SMALL.Problem.gain; ivan@107: maxatoms = floor(prod(SMALL.Problem.blocksize)/2); ivan@107: ivan@107: SMALL.solver(1).toolbox='ompbox'; ivan@107: SMALL.solver(1).name='omp2'; ivan@107: SMALL.solver(1).param=struct(... ivan@107: 'epsilon',Edata,... ivan@107: 'maxatoms', maxatoms); ivan@107: ivan@107: % Denoising the image - find the sparse solution in the learned ivan@107: % dictionary for all patches in the image and the end it uses ivan@107: % reconstruction function to reconstruct the patches and put them into a ivan@107: % denoised image ivan@107: ivan@107: SMALL.solver(1)=SMALL_solve(SMALL.Problem, SMALL.solver(1)); idamnjanovic@6: idamnjanovic@6: %% show results %% idamnjanovic@6: % This will show denoised image and dictionary for all lambdas. If you idamnjanovic@6: % are not interested to see it and do not want clutter your screen idamnjanovic@6: % comment following line idamnjanovic@6: idamnjanovic@6: SMALL_ImgDeNoiseResult(SMALL); idamnjanovic@6: idamnjanovic@6: idamnjanovic@6: time(1,i) = SMALL.DL(1).time; idamnjanovic@6: psnr(1,i) = SMALL.solver(1).reconstructed.psnr; idamnjanovic@6: idamnjanovic@6: clear SMALL idamnjanovic@6: end idamnjanovic@6: idamnjanovic@6: %% show time and psnr %% idamnjanovic@6: figure('Name', 'SPAMS LAMBDA TEST'); idamnjanovic@6: idamnjanovic@6: subplot(1,2,1); plot(lambda, time(1,:), 'ro-'); idamnjanovic@6: title('time vs lambda'); idamnjanovic@6: subplot(1,2,2); plot(lambda, psnr(1,:), 'b*-'); idamnjanovic@6: title('PSNR vs lambda'); idamnjanovic@6: