annotate Problems/Pierre_Problem.m @ 18:bcc748594b61

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