annotate Problems/generatePierreProblem.m @ 161:f42aa8bcb82f ivand_dev

debug and clean the SMALLbox Problems code
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Wed, 31 Aug 2011 12:02:19 +0100
parents
children 9c418bea7f6a
rev   line source
ivan@161 1 function data=generatePierreProblem(src, trg, blocksize, dictsize);
ivan@161 2 %% Generate Pierre Villars Problem
ivan@161 3 %
ivan@161 4 % Pierre_Problem is a part of the SMALLbox and generates the problem
ivan@161 5 % suggested by Professor Pierre Vandergheynst on the SMALL meeting in
ivan@161 6 % Villars.
ivan@161 7 % The function takes as an input:
ivan@161 8 % - src - source image matrix (if not present function promts user for
ivan@161 9 % an image file) ,
ivan@161 10 % - trg - target image matrix (if not present function promts user for
ivan@161 11 % an image file) ,
ivan@161 12 % - blocksize - block (patch) vertical/horizontal dimension (default 8),
ivan@161 13 % - dictsize - dictionary size (default - all patches from target
ivan@161 14 % image).
ivan@161 15 %
ivan@161 16 % The output of the function is stucture with following fields:
ivan@161 17 % - srcname - source image name,
ivan@161 18 % - imageSrc - source image matrix,
ivan@161 19 % - trgname - target image name,
ivan@161 20 % - imageTrg - Target image matrix,
ivan@161 21 % - A - dictonary with patches from the source image,
ivan@161 22 % - b - measurement matrix (i.e. patches from target image to be
ivan@161 23 % represented in dictionary A,
ivan@161 24 % - m - size of patches (default 25),
ivan@161 25 % - n - number of patches to be represented,
ivan@161 26 % - p - dictionary size,
ivan@161 27 % - blocksize - block size (default [5 5]),
ivan@161 28 % - maxval - maximum value (default - 255)
ivan@161 29 % - sparse - if 1 SMALL_solve will keep solution matrix in sparse form,
ivan@161 30 % due to memory constrains.
ivan@161 31
ivan@161 32 %
ivan@161 33 % Centre for Digital Music, Queen Mary, University of London.
ivan@161 34 % This file copyright 2010 Ivan Damnjanovic.
ivan@161 35 %
ivan@161 36 % This program is free software; you can redistribute it and/or
ivan@161 37 % modify it under the terms of the GNU General Public License as
ivan@161 38 % published by the Free Software Foundation; either version 2 of the
ivan@161 39 % License, or (at your option) any later version. See the file
ivan@161 40 % COPYING included with this distribution for more information.
ivan@161 41 %% prompt user for images %%
ivan@161 42
ivan@161 43 % ask for source file name
ivan@161 44
ivan@161 45 TMPpath=pwd;
ivan@161 46 FS=filesep;
ivan@161 47 if ~ exist( 'src', 'var' ) || isempty(src)
ivan@161 48 [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m'));
ivan@161 49 cd([pathstr1,FS,'data',FS,'images']);
ivan@161 50 [filename,pathname] = uigetfile({'*.png;'},'Select a source image');
ivan@161 51 [pathstr, name, ext, versn] = fileparts(filename);
ivan@161 52 data.srcname=name;
ivan@161 53 src = imread(filename);
ivan@161 54 src = double(src);
ivan@161 55 end;
ivan@161 56
ivan@161 57 % ask for target file name
ivan@161 58
ivan@161 59 if ~ exist( 'trg', 'var' ) || isempty(trg)
ivan@161 60 [filename,pathname] = uigetfile({'*.png;'},'Select a target image');
ivan@161 61 [pathstr, name, ext, versn] = fileparts(filename);
ivan@161 62 data.trgname=name;
ivan@161 63 trg = imread(filename);
ivan@161 64 trg = double(trg);
ivan@161 65 end;
ivan@161 66 cd(TMPpath);
ivan@161 67
ivan@161 68 %% set parameters %%
ivan@161 69
ivan@161 70 maxval = 255;
ivan@161 71 if ~ exist( 'blocksize', 'var' ) || isempty(blocksize),blocksize = 5;end
ivan@161 72
ivan@161 73 if ~ exist( 'dictsize', 'var' ) || isempty(dictsize),
ivan@161 74 dictsize = (size(src,1)-blocksize+1)*(size(src,2)-blocksize+1);
ivan@161 75 patch_idx=1:dictsize;
ivan@161 76 else
ivan@161 77 num_blocks_src=(size(src,1)-blocksize+1)*(size(src,2)-blocksize+1);
ivan@161 78 patch_idx=1:floor(num_blocks_src/dictsize):dictsize*floor(num_blocks_src/dictsize);
ivan@161 79 end
ivan@161 80
ivan@161 81 p = ndims(src);
ivan@161 82 if (p==2 && any(size(src)==1) && length(blocksize)==1)
ivan@161 83 p = 1;
ivan@161 84 end
ivan@161 85
ivan@161 86
ivan@161 87 % blocksize %
ivan@161 88 if (numel(blocksize)==1)
ivan@161 89 blocksize = ones(1,p)*blocksize;
ivan@161 90 end
ivan@161 91 %%
ivan@161 92 %% create dictionary data %%
ivan@161 93
ivan@161 94 S=im2colstep(src,blocksize);
ivan@161 95
ivan@161 96 for j= 1:size(S,2)
ivan@161 97 S(:,j)=S(:,j)./norm(S(:,j));
ivan@161 98 end
ivan@161 99
ivan@161 100 %% create measurement matrix %%
ivan@161 101
ivan@161 102 T=im2colstep(trg,blocksize, blocksize);
ivan@161 103
ivan@161 104 %% output structure %%
ivan@161 105
ivan@161 106 data.imageSrc = src;
ivan@161 107 data.imageTrg = trg;
ivan@161 108 data.A = S(:,patch_idx);
ivan@161 109 data.b = T;
ivan@161 110 data.m = size(T,1);
ivan@161 111 data.n = size(T,2);
ivan@161 112 data.p = size(data.A,2);
ivan@161 113 data.blocksize=blocksize;
ivan@161 114 data.maxval=maxval;
ivan@161 115
ivan@161 116 % keep coefficients matrix in sparse form and do not convert it to full.
ivan@161 117 % getting around out of memory problem when converting big matrix from
ivan@161 118 % sparse to full... (check SMALL_solve function)
ivan@161 119 data.sparse=1;
ivan@161 120
ivan@161 121