annotate Problems/generateImageDenoiseProblem.m @ 39:8f734534839a

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:35:01 +0000
parents 0211faef9add
children 2c59257d734c
rev   line source
idamnjanovic@10 1 function data=generateImageDenoiseProblem(im, trainnum, blocksize, dictsize, sigma, gain, maxval, initdict);
idamnjanovic@10 2 %%% Generate Image Denoising Problem
idamnjanovic@10 3 %
idamnjanovic@21 4 % Centre for Digital Music, Queen Mary, University of London.
idamnjanovic@21 5 % This file copyright 2010 Ivan Damnjanovic.
idamnjanovic@21 6 %
idamnjanovic@21 7 % This program is free software; you can redistribute it and/or
idamnjanovic@21 8 % modify it under the terms of the GNU General Public License as
idamnjanovic@21 9 % published by the Free Software Foundation; either version 2 of the
idamnjanovic@21 10 % License, or (at your option) any later version. See the file
idamnjanovic@21 11 % COPYING included with this distribution for more information.
idamnjanovic@21 12 %
idamnjanovic@10 13 % generateImageDenoiseProblem is a part of the SMALLbox and generates
idamnjanovic@10 14 % a problem that can be used for comparison of Dictionary Learning/Sparse
idamnjanovic@10 15 % Representation techniques in image denoising scenario.
idamnjanovic@10 16 % The function takes as an input:
idamnjanovic@10 17 % - im - image matrix (if not present function promts user for an
idamnjanovic@10 18 % image file) ,
idamnjanovic@10 19 % - trainnum - number of training samples (default - 40000)
idamnjanovic@10 20 % - blocksize - block (patch) vertical/horizontal dimension (default 8),
idamnjanovic@10 21 % - dictsize - dictionary size (default - 256),
idamnjanovic@10 22 % - sigma - noise level (default - 20),
idamnjanovic@10 23 % - noise gain (default - 1.15),
idamnjanovic@10 24 % - maxval - maximum value (default - 255)
idamnjanovic@10 25 % - initdict - initial dictionary (default - 4x overcomlete dct)
idamnjanovic@10 26 %
idamnjanovic@10 27 % The output of the function is stucture with following fields:
idamnjanovic@10 28 % - name - name of the original image (if image is read inside of the
idamnjanovic@10 29 % function)
idamnjanovic@10 30 % - Original - original image matrix,
idamnjanovic@10 31 % - Noisy - image with added noise,
idamnjanovic@10 32 % - b - training patches,
idamnjanovic@10 33 % - m - size of training patches (default 64),
idamnjanovic@10 34 % - n - number of training patches,
idamnjanovic@10 35 % - p - number of dictionary elements to be learned,
idamnjanovic@10 36 % - blocksize - block size (default [8 8]),
idamnjanovic@10 37 % - sigma - noise level,
idamnjanovic@10 38 % - noise gain (default - 1.15),
idamnjanovic@10 39 % - maxval - maximum value (default - 255)
idamnjanovic@10 40 % - initdict - initial dictionary (default - 4x overcomlete dct)
idamnjanovic@10 41 % - signalDim - signal dimension (default - 2)
idamnjanovic@10 42 %
idamnjanovic@10 43 % Based on KSVD denoise demo by Ron Rubinstein
idamnjanovic@10 44 % See also KSVDDENOISEDEMO and KSVDDEMO.
idamnjanovic@10 45 % Ron Rubinstein
idamnjanovic@10 46 % Computer Science Department
idamnjanovic@10 47 % Technion, Haifa 32000 Israel
idamnjanovic@10 48 % ronrubin@cs
idamnjanovic@10 49 % August 2009
idamnjanovic@10 50 %%
idamnjanovic@10 51 disp(' ');
idamnjanovic@10 52 disp(' ********** Denoising Problem **********');
idamnjanovic@10 53 disp(' ');
idamnjanovic@10 54 disp(' This function reads an image, adds random Gaussian noise,');
idamnjanovic@10 55 disp(' that can be later denoised by using dictionary learning techniques.');
idamnjanovic@10 56 disp(' ');
idamnjanovic@10 57
idamnjanovic@10 58
idamnjanovic@10 59 %% prompt user for image %%
idamnjanovic@10 60 %ask for file name
idamnjanovic@10 61 FS=filesep;
idamnjanovic@10 62 TMPpath=pwd;
idamnjanovic@10 63 if ~ exist( 'im', 'var' ) || isempty(im)
idamnjanovic@10 64 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@10 65 cd([pathstr1,FS,'data',FS,'images']);
idamnjanovic@17 66 [filename,pathname] = uigetfile({'*.png;'},'Select an image');
idamnjanovic@10 67 [pathstr, name, ext, versn] = fileparts(filename);
idamnjanovic@10 68 data.name=name;
idamnjanovic@10 69 im = imread(filename);
idamnjanovic@10 70 im = double(im);
idamnjanovic@10 71 end;
idamnjanovic@10 72 cd(TMPpath);
idamnjanovic@10 73
idamnjanovic@10 74 %% check input parameters %%
idamnjanovic@10 75
idamnjanovic@10 76 if ~ exist( 'blocksize', 'var' ) || isempty(blocksize),blocksize = 8;end
idamnjanovic@10 77 if ~ exist( 'dictsize', 'var' ) || isempty(dictsize), dictsize = 256;end
idamnjanovic@10 78 if ~ exist( 'trainnum', 'var' ) || isempty(trainnum),trainnum = 40000;end
idamnjanovic@10 79 if ~ exist( 'sigma', 'var' ) || isempty(sigma), sigma = 20; end
idamnjanovic@10 80 if ~ exist( 'gain', 'var' ) || isempty(gain), gain = 1.15; end
idamnjanovic@10 81 if ~ exist( 'maxval', 'var' ) || isempty(maxval), maxval = 255; end
idamnjanovic@10 82 if ~ exist( 'initdict', 'var' ) || isempty(initdict), initdict = 'odct'; end
idamnjanovic@10 83
idamnjanovic@10 84 %% generate noisy image %%
idamnjanovic@10 85
idamnjanovic@10 86 disp(' ');
idamnjanovic@10 87 disp('Generating noisy image...');
idamnjanovic@10 88
idamnjanovic@10 89 n = randn(size(im)) * sigma;
idamnjanovic@10 90 imnoise = im + n;
idamnjanovic@10 91
idamnjanovic@10 92 %% set parameters %%
idamnjanovic@10 93
idamnjanovic@10 94 x = imnoise;
idamnjanovic@10 95 p = ndims(x);
idamnjanovic@10 96
idamnjanovic@10 97 if (p==2 && any(size(x)==1) && length(blocksize)==1)
idamnjanovic@10 98 p = 1;
idamnjanovic@10 99 end
idamnjanovic@10 100
idamnjanovic@10 101 % blocksize %
idamnjanovic@10 102
idamnjanovic@10 103 if (numel(blocksize)==1)
idamnjanovic@10 104 blocksize = ones(1,p)*blocksize;
idamnjanovic@10 105 end
idamnjanovic@10 106
idamnjanovic@10 107 if (strcmpi(initdict,'odct'))
idamnjanovic@10 108 initdict = odctndict(blocksize,dictsize,p);
idamnjanovic@10 109 elseif (strcmpi(initdict,'data'))
idamnjanovic@10 110 clear initdict; % causes initialization using random examples
idamnjanovic@10 111 else
idamnjanovic@10 112 error('Invalid initial dictionary specified.');
idamnjanovic@10 113 end
idamnjanovic@10 114
idamnjanovic@10 115 if exist( 'initdict', 'var' )
idamnjanovic@10 116 initdict = initdict(:,1:dictsize);
idamnjanovic@10 117 end
idamnjanovic@10 118
idamnjanovic@10 119 %%%% create training data %%%
idamnjanovic@10 120
idamnjanovic@10 121 ids = cell(p,1);
idamnjanovic@10 122 if (p==1)
idamnjanovic@10 123 ids{1} = reggrid(length(x)-blocksize+1, trainnum, 'eqdist');
idamnjanovic@10 124 else
idamnjanovic@10 125 [ids{:}] = reggrid(size(x)-blocksize+1, trainnum, 'eqdist');
idamnjanovic@10 126 end
idamnjanovic@10 127 X = sampgrid(x,blocksize,ids{:});
idamnjanovic@10 128
idamnjanovic@10 129 % remove dc in blocks to conserve memory %
idamnjanovic@10 130
idamnjanovic@10 131 bsize = 2000;
idamnjanovic@10 132 for i = 1:bsize:size(X,2)
idamnjanovic@10 133 blockids = i : min(i+bsize-1,size(X,2));
idamnjanovic@10 134 X(:,blockids) = remove_dc(X(:,blockids),'columns');
idamnjanovic@10 135 end
idamnjanovic@10 136
idamnjanovic@10 137 %% output structure %%
idamnjanovic@10 138
idamnjanovic@10 139 data.Original = im;
idamnjanovic@10 140 data.Noisy = imnoise;
idamnjanovic@10 141 data.b = X;
idamnjanovic@10 142 data.m = size(X,1);
idamnjanovic@10 143 data.n = size(X,2);
idamnjanovic@10 144 data.p = dictsize;
idamnjanovic@10 145 data.blocksize=blocksize;
idamnjanovic@10 146 data.sigma = sigma;
idamnjanovic@10 147 data.gain = gain;
idamnjanovic@10 148 data.maxval = maxval;
idamnjanovic@10 149 data.initdict= initdict;
idamnjanovic@10 150 data.signalDim=2;
idamnjanovic@10 151
idamnjanovic@10 152 end %% end of function