annotate Problems/generatePierre_Problem.m @ 193:cc540df790f4 danieleb

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