annotate Problems/generateImageDenoiseProblem.m @ 207:233e75809e4a luisf_dev

Accelerated the code for LGD and optimal grandient descent
author bmailhe
date Wed, 21 Mar 2012 14:12:25 +0000
parents 9c418bea7f6a
children
rev   line source
ivan@161 1 function data = generateImageDenoiseProblem(im, trainnum, blocksize,...
ivan@161 2 dictsize, sigma, gain, maxval, initdict)
ivan@128 3 %% Generate Image Denoising Problem
idamnjanovic@21 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)
ivan@125 34
ivan@125 35 %
ivan@125 36 % Centre for Digital Music, Queen Mary, University of London.
ivan@125 37 % This file copyright 2010 Ivan Damnjanovic.
ivan@125 38 %
ivan@125 39 % This program is free software; you can redistribute it and/or
ivan@125 40 % modify it under the terms of the GNU General Public License as
ivan@125 41 % published by the Free Software Foundation; either version 2 of the
ivan@125 42 % License, or (at your option) any later version. See the file
ivan@125 43 % COPYING included with this distribution for more information.
idamnjanovic@10 44 %
idamnjanovic@10 45 % Based on KSVD denoise demo by Ron Rubinstein
idamnjanovic@10 46 % See also KSVDDENOISEDEMO and KSVDDEMO.
idamnjanovic@10 47 % Ron Rubinstein
idamnjanovic@10 48 % Computer Science Department
idamnjanovic@10 49 % Technion, Haifa 32000 Israel
idamnjanovic@10 50 % ronrubin@cs
idamnjanovic@10 51 % August 2009
idamnjanovic@10 52 %%
idamnjanovic@10 53 disp(' ');
idamnjanovic@10 54 disp(' ********** Denoising Problem **********');
idamnjanovic@10 55 disp(' ');
idamnjanovic@10 56 disp(' This function reads an image, adds random Gaussian noise,');
idamnjanovic@10 57 disp(' that can be later denoised by using dictionary learning techniques.');
idamnjanovic@10 58 disp(' ');
idamnjanovic@10 59
idamnjanovic@10 60
idamnjanovic@10 61 %% prompt user for image %%
idamnjanovic@10 62 %ask for file name
idamnjanovic@10 63 FS=filesep;
idamnjanovic@10 64 TMPpath=pwd;
idamnjanovic@10 65 if ~ exist( 'im', 'var' ) || isempty(im)
luis@186 66 [pathstr1, name, ext] = fileparts(which('SMALLboxSetup.m'));
idamnjanovic@10 67 cd([pathstr1,FS,'data',FS,'images']);
idamnjanovic@17 68 [filename,pathname] = uigetfile({'*.png;'},'Select an image');
luis@186 69 [pathstr, name, ext] = fileparts(filename);
idamnjanovic@10 70 data.name=name;
idamnjanovic@10 71 im = imread(filename);
idamnjanovic@44 72 %im = double(im);
idamnjanovic@10 73 end;
idamnjanovic@44 74 im = double(im);
idamnjanovic@10 75 cd(TMPpath);
idamnjanovic@10 76
idamnjanovic@10 77 %% check input parameters %%
idamnjanovic@10 78
idamnjanovic@10 79 if ~ exist( 'blocksize', 'var' ) || isempty(blocksize),blocksize = 8;end
idamnjanovic@10 80 if ~ exist( 'dictsize', 'var' ) || isempty(dictsize), dictsize = 256;end
idamnjanovic@10 81 if ~ exist( 'trainnum', 'var' ) || isempty(trainnum),trainnum = 40000;end
idamnjanovic@10 82 if ~ exist( 'sigma', 'var' ) || isempty(sigma), sigma = 20; end
idamnjanovic@10 83 if ~ exist( 'gain', 'var' ) || isempty(gain), gain = 1.15; end
idamnjanovic@10 84 if ~ exist( 'maxval', 'var' ) || isempty(maxval), maxval = 255; end
idamnjanovic@10 85 if ~ exist( 'initdict', 'var' ) || isempty(initdict), initdict = 'odct'; end
idamnjanovic@10 86
idamnjanovic@10 87 %% generate noisy image %%
idamnjanovic@10 88
idamnjanovic@10 89 disp(' ');
idamnjanovic@10 90 disp('Generating noisy image...');
idamnjanovic@10 91
idamnjanovic@10 92 n = randn(size(im)) * sigma;
idamnjanovic@10 93 imnoise = im + n;
idamnjanovic@10 94
idamnjanovic@10 95 %% set parameters %%
idamnjanovic@10 96
idamnjanovic@10 97 x = imnoise;
idamnjanovic@10 98 p = ndims(x);
idamnjanovic@44 99 psnr=20*log10(maxval * sqrt(numel(im)) / norm(im(:)-imnoise(:)));
idamnjanovic@10 100 if (p==2 && any(size(x)==1) && length(blocksize)==1)
idamnjanovic@10 101 p = 1;
idamnjanovic@10 102 end
idamnjanovic@10 103
idamnjanovic@10 104 % blocksize %
idamnjanovic@10 105
idamnjanovic@10 106 if (numel(blocksize)==1)
idamnjanovic@10 107 blocksize = ones(1,p)*blocksize;
idamnjanovic@10 108 end
idamnjanovic@10 109
idamnjanovic@10 110 if (strcmpi(initdict,'odct'))
idamnjanovic@10 111 initdict = odctndict(blocksize,dictsize,p);
idamnjanovic@10 112 elseif (strcmpi(initdict,'data'))
idamnjanovic@10 113 clear initdict; % causes initialization using random examples
idamnjanovic@10 114 else
idamnjanovic@10 115 error('Invalid initial dictionary specified.');
idamnjanovic@10 116 end
idamnjanovic@10 117
idamnjanovic@10 118 if exist( 'initdict', 'var' )
idamnjanovic@10 119 initdict = initdict(:,1:dictsize);
idamnjanovic@10 120 end
idamnjanovic@10 121
idamnjanovic@10 122 %%%% create training data %%%
idamnjanovic@10 123
idamnjanovic@10 124 ids = cell(p,1);
idamnjanovic@10 125 if (p==1)
idamnjanovic@10 126 ids{1} = reggrid(length(x)-blocksize+1, trainnum, 'eqdist');
idamnjanovic@10 127 else
idamnjanovic@10 128 [ids{:}] = reggrid(size(x)-blocksize+1, trainnum, 'eqdist');
idamnjanovic@10 129 end
idamnjanovic@10 130 X = sampgrid(x,blocksize,ids{:});
idamnjanovic@10 131
idamnjanovic@10 132 % remove dc in blocks to conserve memory %
idamnjanovic@10 133
idamnjanovic@10 134 bsize = 2000;
idamnjanovic@10 135 for i = 1:bsize:size(X,2)
idamnjanovic@10 136 blockids = i : min(i+bsize-1,size(X,2));
idamnjanovic@10 137 X(:,blockids) = remove_dc(X(:,blockids),'columns');
idamnjanovic@10 138 end
idamnjanovic@10 139
idamnjanovic@44 140 % Noisy image blocks
ivan@111 141 xcol=im2colstep(x,blocksize);
idamnjanovic@44 142 [b1, dc] = remove_dc(xcol,'columns');
idamnjanovic@44 143
idamnjanovic@10 144 %% output structure %%
idamnjanovic@10 145
idamnjanovic@10 146 data.Original = im;
idamnjanovic@10 147 data.Noisy = imnoise;
idamnjanovic@44 148 data.noisy_psnr=psnr;
idamnjanovic@10 149 data.b = X;
idamnjanovic@44 150 data.b1=b1;
idamnjanovic@44 151 data.b1dc=dc;
idamnjanovic@10 152 data.m = size(X,1);
idamnjanovic@10 153 data.n = size(X,2);
idamnjanovic@10 154 data.p = dictsize;
idamnjanovic@10 155 data.blocksize=blocksize;
idamnjanovic@10 156 data.sigma = sigma;
idamnjanovic@10 157 data.gain = gain;
idamnjanovic@10 158 data.maxval = maxval;
idamnjanovic@10 159 data.initdict= initdict;
idamnjanovic@10 160 data.signalDim=2;
idamnjanovic@65 161 data.sparse=1;
idamnjanovic@10 162 end %% end of function