annotate Problems/generateImageDenoiseProblem.m @ 81:a30e8bd6d948

matlab_midi scripts
author Ivan <ivan.damnjanovic@eecs.qmul.ac.uk>
date Mon, 28 Mar 2011 17:35:01 +0100
parents 55faa9b5d1ac
children 8208316abec6
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@44 70 %im = double(im);
idamnjanovic@10 71 end;
idamnjanovic@44 72 im = double(im);
idamnjanovic@10 73 cd(TMPpath);
idamnjanovic@10 74
idamnjanovic@10 75 %% check input parameters %%
idamnjanovic@10 76
idamnjanovic@10 77 if ~ exist( 'blocksize', 'var' ) || isempty(blocksize),blocksize = 8;end
idamnjanovic@10 78 if ~ exist( 'dictsize', 'var' ) || isempty(dictsize), dictsize = 256;end
idamnjanovic@10 79 if ~ exist( 'trainnum', 'var' ) || isempty(trainnum),trainnum = 40000;end
idamnjanovic@10 80 if ~ exist( 'sigma', 'var' ) || isempty(sigma), sigma = 20; end
idamnjanovic@10 81 if ~ exist( 'gain', 'var' ) || isempty(gain), gain = 1.15; end
idamnjanovic@10 82 if ~ exist( 'maxval', 'var' ) || isempty(maxval), maxval = 255; end
idamnjanovic@10 83 if ~ exist( 'initdict', 'var' ) || isempty(initdict), initdict = 'odct'; end
idamnjanovic@10 84
idamnjanovic@10 85 %% generate noisy image %%
idamnjanovic@10 86
idamnjanovic@10 87 disp(' ');
idamnjanovic@10 88 disp('Generating noisy image...');
idamnjanovic@10 89
idamnjanovic@10 90 n = randn(size(im)) * sigma;
idamnjanovic@10 91 imnoise = im + n;
idamnjanovic@10 92
idamnjanovic@10 93 %% set parameters %%
idamnjanovic@10 94
idamnjanovic@10 95 x = imnoise;
idamnjanovic@10 96 p = ndims(x);
idamnjanovic@44 97 psnr=20*log10(maxval * sqrt(numel(im)) / norm(im(:)-imnoise(:)));
idamnjanovic@10 98 if (p==2 && any(size(x)==1) && length(blocksize)==1)
idamnjanovic@10 99 p = 1;
idamnjanovic@10 100 end
idamnjanovic@10 101
idamnjanovic@10 102 % blocksize %
idamnjanovic@10 103
idamnjanovic@10 104 if (numel(blocksize)==1)
idamnjanovic@10 105 blocksize = ones(1,p)*blocksize;
idamnjanovic@10 106 end
idamnjanovic@10 107
idamnjanovic@10 108 if (strcmpi(initdict,'odct'))
idamnjanovic@10 109 initdict = odctndict(blocksize,dictsize,p);
idamnjanovic@10 110 elseif (strcmpi(initdict,'data'))
idamnjanovic@10 111 clear initdict; % causes initialization using random examples
idamnjanovic@10 112 else
idamnjanovic@10 113 error('Invalid initial dictionary specified.');
idamnjanovic@10 114 end
idamnjanovic@10 115
idamnjanovic@10 116 if exist( 'initdict', 'var' )
idamnjanovic@10 117 initdict = initdict(:,1:dictsize);
idamnjanovic@10 118 end
idamnjanovic@10 119
idamnjanovic@10 120 %%%% create training data %%%
idamnjanovic@10 121
idamnjanovic@10 122 ids = cell(p,1);
idamnjanovic@10 123 if (p==1)
idamnjanovic@10 124 ids{1} = reggrid(length(x)-blocksize+1, trainnum, 'eqdist');
idamnjanovic@10 125 else
idamnjanovic@10 126 [ids{:}] = reggrid(size(x)-blocksize+1, trainnum, 'eqdist');
idamnjanovic@10 127 end
idamnjanovic@10 128 X = sampgrid(x,blocksize,ids{:});
idamnjanovic@10 129
idamnjanovic@10 130 % remove dc in blocks to conserve memory %
idamnjanovic@10 131
idamnjanovic@10 132 bsize = 2000;
idamnjanovic@10 133 for i = 1:bsize:size(X,2)
idamnjanovic@10 134 blockids = i : min(i+bsize-1,size(X,2));
idamnjanovic@10 135 X(:,blockids) = remove_dc(X(:,blockids),'columns');
idamnjanovic@10 136 end
idamnjanovic@10 137
idamnjanovic@44 138 % Noisy image blocks
idamnjanovic@44 139 xcol=im2col(x,blocksize,'sliding');
idamnjanovic@44 140 [b1, dc] = remove_dc(xcol,'columns');
idamnjanovic@44 141
idamnjanovic@10 142 %% output structure %%
idamnjanovic@10 143
idamnjanovic@10 144 data.Original = im;
idamnjanovic@10 145 data.Noisy = imnoise;
idamnjanovic@44 146 data.noisy_psnr=psnr;
idamnjanovic@10 147 data.b = X;
idamnjanovic@44 148 data.b1=b1;
idamnjanovic@44 149 data.b1dc=dc;
idamnjanovic@10 150 data.m = size(X,1);
idamnjanovic@10 151 data.n = size(X,2);
idamnjanovic@10 152 data.p = dictsize;
idamnjanovic@10 153 data.blocksize=blocksize;
idamnjanovic@10 154 data.sigma = sigma;
idamnjanovic@10 155 data.gain = gain;
idamnjanovic@10 156 data.maxval = maxval;
idamnjanovic@10 157 data.initdict= initdict;
idamnjanovic@10 158 data.signalDim=2;
idamnjanovic@65 159 data.sparse=1;
idamnjanovic@10 160 end %% end of function