annotate Problems/generateImageDenoiseProblem.m @ 10:207a6ae9a76f version1.0

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