idamnjanovic@10
|
1 function data=Pierre_Problem(src, trg, blocksize, dictsize);
|
idamnjanovic@10
|
2 %%% Generate Pierre Problem
|
idamnjanovic@21
|
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@10
|
12 %
|
idamnjanovic@10
|
13 % Pierre_Problem is a part of the SMALLbox and generates the problem
|
idamnjanovic@10
|
14 % suggested by Professor Pierre Vandergheynst on the SMALL meeting in
|
idamnjanovic@10
|
15 % Villars.
|
idamnjanovic@10
|
16 % The function takes as an input:
|
idamnjanovic@10
|
17 % - src - source image matrix (if not present function promts user for
|
idamnjanovic@10
|
18 % an image file) ,
|
idamnjanovic@10
|
19 % - trg - target image matrix (if not present function promts user for
|
idamnjanovic@10
|
20 % an image file) ,
|
idamnjanovic@10
|
21 % - blocksize - block (patch) vertical/horizontal dimension (default 8),
|
idamnjanovic@10
|
22 % - dictsize - dictionary size (default - all patches from target
|
idamnjanovic@10
|
23 % image).
|
idamnjanovic@10
|
24 %
|
idamnjanovic@10
|
25 % The output of the function is stucture with following fields:
|
idamnjanovic@10
|
26 % - srcname - source image name,
|
idamnjanovic@10
|
27 % - imageSrc - source image matrix,
|
idamnjanovic@10
|
28 % - trgname - target image name,
|
idamnjanovic@10
|
29 % - imageTrg - Target image matrix,
|
idamnjanovic@10
|
30 % - A - dictonary with patches from the source image,
|
idamnjanovic@10
|
31 % - b - measurement matrix (i.e. patches from target image to be
|
idamnjanovic@10
|
32 % represented in dictionary A,
|
idamnjanovic@10
|
33 % - m - size of patches (default 25),
|
idamnjanovic@10
|
34 % - n - number of patches to be represented,
|
idamnjanovic@10
|
35 % - p - dictionary size,
|
idamnjanovic@10
|
36 % - blocksize - block size (default [5 5]),
|
idamnjanovic@10
|
37 % - maxval - maximum value (default - 255)
|
idamnjanovic@10
|
38 % - sparse - if 1 SMALL_solve will keep solution matrix in sparse form,
|
idamnjanovic@10
|
39 % due to memory constrains.
|
idamnjanovic@10
|
40
|
idamnjanovic@10
|
41 %% prompt user for images %%
|
idamnjanovic@10
|
42
|
idamnjanovic@28
|
43 % ask for source file name
|
idamnjanovic@10
|
44
|
idamnjanovic@10
|
45 TMPpath=pwd;
|
idamnjanovic@10
|
46 FS=filesep;
|
idamnjanovic@10
|
47 if ~ exist( 'src', 'var' ) || isempty(src)
|
idamnjanovic@10
|
48 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
|
idamnjanovic@10
|
49 cd([pathstr1,FS,'data',FS,'images']);
|
idamnjanovic@10
|
50 [filename,pathname] = uigetfile({'*.png;'},'Select a source image');
|
idamnjanovic@10
|
51 [pathstr, name, ext, versn] = fileparts(filename);
|
idamnjanovic@10
|
52 data.srcname=name;
|
idamnjanovic@10
|
53 src = imread(filename);
|
idamnjanovic@10
|
54 src = double(src);
|
idamnjanovic@10
|
55 end;
|
idamnjanovic@10
|
56
|
idamnjanovic@28
|
57 % ask for target file name
|
idamnjanovic@10
|
58
|
idamnjanovic@10
|
59 if ~ exist( 'trg', 'var' ) || isempty(trg)
|
idamnjanovic@10
|
60 [filename,pathname] = uigetfile({'*.png;'},'Select a target image');
|
idamnjanovic@10
|
61 [pathstr, name, ext, versn] = fileparts(filename);
|
idamnjanovic@10
|
62 data.trgname=name;
|
idamnjanovic@10
|
63 trg = imread(filename);
|
idamnjanovic@10
|
64 trg = double(trg);
|
idamnjanovic@10
|
65 end;
|
idamnjanovic@10
|
66 cd(TMPpath);
|
idamnjanovic@10
|
67
|
idamnjanovic@10
|
68 %% set parameters %%
|
idamnjanovic@10
|
69
|
idamnjanovic@10
|
70 maxval = 255;
|
idamnjanovic@10
|
71 if ~ exist( 'blocksize', 'var' ) || isempty(blocksize),blocksize = 5;end
|
idamnjanovic@10
|
72
|
idamnjanovic@10
|
73 if ~ exist( 'dictsize', 'var' ) || isempty(dictsize),
|
idamnjanovic@10
|
74 dictsize = (size(src,1)-blocksize+1)*(size(src,2)-blocksize+1);
|
idamnjanovic@10
|
75 patch_idx=1:dictsize;
|
idamnjanovic@10
|
76 else
|
idamnjanovic@10
|
77 num_blocks_src=(size(src,1)-blocksize+1)*(size(src,2)-blocksize+1);
|
idamnjanovic@10
|
78 patch_idx=1:floor(num_blocks_src/dictsize):dictsize*floor(num_blocks_src/dictsize);
|
idamnjanovic@10
|
79 end
|
idamnjanovic@10
|
80
|
idamnjanovic@10
|
81 p = ndims(src);
|
idamnjanovic@10
|
82 if (p==2 && any(size(src)==1) && length(blocksize)==1)
|
idamnjanovic@10
|
83 p = 1;
|
idamnjanovic@10
|
84 end
|
idamnjanovic@10
|
85
|
idamnjanovic@10
|
86
|
idamnjanovic@10
|
87 % blocksize %
|
idamnjanovic@10
|
88 if (numel(blocksize)==1)
|
idamnjanovic@10
|
89 blocksize = ones(1,p)*blocksize;
|
idamnjanovic@10
|
90 end
|
idamnjanovic@10
|
91 %%
|
idamnjanovic@10
|
92 %% create dictionary data %%
|
idamnjanovic@10
|
93
|
idamnjanovic@10
|
94 S=im2col(src,blocksize,'sliding');
|
idamnjanovic@10
|
95
|
idamnjanovic@10
|
96 for j= 1:size(S,2)
|
idamnjanovic@10
|
97 S(:,j)=S(:,j)./norm(S(:,j));
|
idamnjanovic@10
|
98 end
|
idamnjanovic@10
|
99
|
idamnjanovic@10
|
100 %% create measurement matrix %%
|
idamnjanovic@10
|
101
|
idamnjanovic@10
|
102 T=im2col(trg,blocksize, 'distinct');
|
idamnjanovic@10
|
103
|
idamnjanovic@10
|
104 %% output structure %%
|
idamnjanovic@10
|
105
|
idamnjanovic@10
|
106 data.imageSrc = src;
|
idamnjanovic@10
|
107 data.imageTrg = trg;
|
idamnjanovic@10
|
108 data.A = S(:,patch_idx);
|
idamnjanovic@10
|
109 data.b = T;
|
idamnjanovic@10
|
110 data.m = size(T,1);
|
idamnjanovic@10
|
111 data.n = size(T,2);
|
idamnjanovic@10
|
112 data.p = size(data.A,2);
|
idamnjanovic@10
|
113 data.blocksize=blocksize;
|
idamnjanovic@10
|
114 data.maxval=maxval;
|
idamnjanovic@10
|
115
|
idamnjanovic@28
|
116 % keep coefficients matrix in sparse form and do not convert it to full.
|
idamnjanovic@31
|
117 % getting around out of memory problem when converting big matrix from
|
idamnjanovic@28
|
118 % sparse to full... (check SMALL_solve function)
|
idamnjanovic@10
|
119 data.sparse=1;
|
idamnjanovic@10
|
120
|
idamnjanovic@10
|
121
|