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

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