idamnjanovic@10: function data=Pierre_Problem(src, trg, blocksize, dictsize); idamnjanovic@10: %%% Generate Pierre Problem idamnjanovic@21: % idamnjanovic@21: % Centre for Digital Music, Queen Mary, University of London. idamnjanovic@21: % This file copyright 2010 Ivan Damnjanovic. idamnjanovic@21: % idamnjanovic@21: % This program is free software; you can redistribute it and/or idamnjanovic@21: % modify it under the terms of the GNU General Public License as idamnjanovic@21: % published by the Free Software Foundation; either version 2 of the idamnjanovic@21: % License, or (at your option) any later version. See the file idamnjanovic@21: % COPYING included with this distribution for more information. idamnjanovic@10: % idamnjanovic@10: % Pierre_Problem is a part of the SMALLbox and generates the problem idamnjanovic@10: % suggested by Professor Pierre Vandergheynst on the SMALL meeting in idamnjanovic@10: % Villars. idamnjanovic@10: % The function takes as an input: idamnjanovic@10: % - src - source image matrix (if not present function promts user for idamnjanovic@10: % an image file) , idamnjanovic@10: % - trg - target image matrix (if not present function promts user for idamnjanovic@10: % an image file) , idamnjanovic@10: % - blocksize - block (patch) vertical/horizontal dimension (default 8), idamnjanovic@10: % - dictsize - dictionary size (default - all patches from target idamnjanovic@10: % image). idamnjanovic@10: % idamnjanovic@10: % The output of the function is stucture with following fields: idamnjanovic@10: % - srcname - source image name, idamnjanovic@10: % - imageSrc - source image matrix, idamnjanovic@10: % - trgname - target image name, idamnjanovic@10: % - imageTrg - Target image matrix, idamnjanovic@10: % - A - dictonary with patches from the source image, idamnjanovic@10: % - b - measurement matrix (i.e. patches from target image to be idamnjanovic@10: % represented in dictionary A, idamnjanovic@10: % - m - size of patches (default 25), idamnjanovic@10: % - n - number of patches to be represented, idamnjanovic@10: % - p - dictionary size, idamnjanovic@10: % - blocksize - block size (default [5 5]), idamnjanovic@10: % - maxval - maximum value (default - 255) idamnjanovic@10: % - sparse - if 1 SMALL_solve will keep solution matrix in sparse form, idamnjanovic@10: % due to memory constrains. idamnjanovic@10: idamnjanovic@10: %% prompt user for images %% idamnjanovic@10: idamnjanovic@28: % ask for source file name idamnjanovic@10: idamnjanovic@10: TMPpath=pwd; idamnjanovic@10: FS=filesep; idamnjanovic@10: if ~ exist( 'src', 'var' ) || isempty(src) idamnjanovic@10: [pathstr1, name, ext, versn] = fileparts(which('SMALLboxSetup.m')); idamnjanovic@10: cd([pathstr1,FS,'data',FS,'images']); idamnjanovic@10: [filename,pathname] = uigetfile({'*.png;'},'Select a source image'); idamnjanovic@10: [pathstr, name, ext, versn] = fileparts(filename); idamnjanovic@10: data.srcname=name; idamnjanovic@10: src = imread(filename); idamnjanovic@10: src = double(src); idamnjanovic@10: end; idamnjanovic@10: idamnjanovic@28: % ask for target file name idamnjanovic@10: idamnjanovic@10: if ~ exist( 'trg', 'var' ) || isempty(trg) idamnjanovic@10: [filename,pathname] = uigetfile({'*.png;'},'Select a target image'); idamnjanovic@10: [pathstr, name, ext, versn] = fileparts(filename); idamnjanovic@10: data.trgname=name; idamnjanovic@10: trg = imread(filename); idamnjanovic@10: trg = double(trg); idamnjanovic@10: end; idamnjanovic@10: cd(TMPpath); idamnjanovic@10: idamnjanovic@10: %% set parameters %% idamnjanovic@10: idamnjanovic@10: maxval = 255; idamnjanovic@10: if ~ exist( 'blocksize', 'var' ) || isempty(blocksize),blocksize = 5;end idamnjanovic@10: idamnjanovic@10: if ~ exist( 'dictsize', 'var' ) || isempty(dictsize), idamnjanovic@10: dictsize = (size(src,1)-blocksize+1)*(size(src,2)-blocksize+1); idamnjanovic@10: patch_idx=1:dictsize; idamnjanovic@10: else idamnjanovic@10: num_blocks_src=(size(src,1)-blocksize+1)*(size(src,2)-blocksize+1); idamnjanovic@10: patch_idx=1:floor(num_blocks_src/dictsize):dictsize*floor(num_blocks_src/dictsize); idamnjanovic@10: end idamnjanovic@10: idamnjanovic@10: p = ndims(src); idamnjanovic@10: if (p==2 && any(size(src)==1) && length(blocksize)==1) idamnjanovic@10: p = 1; idamnjanovic@10: end idamnjanovic@10: idamnjanovic@10: idamnjanovic@10: % blocksize % idamnjanovic@10: if (numel(blocksize)==1) idamnjanovic@10: blocksize = ones(1,p)*blocksize; idamnjanovic@10: end idamnjanovic@10: %% idamnjanovic@10: %% create dictionary data %% idamnjanovic@10: idamnjanovic@10: S=im2col(src,blocksize,'sliding'); idamnjanovic@10: idamnjanovic@10: for j= 1:size(S,2) idamnjanovic@10: S(:,j)=S(:,j)./norm(S(:,j)); idamnjanovic@10: end idamnjanovic@10: idamnjanovic@10: %% create measurement matrix %% idamnjanovic@10: idamnjanovic@10: T=im2col(trg,blocksize, 'distinct'); idamnjanovic@10: idamnjanovic@10: %% output structure %% idamnjanovic@10: idamnjanovic@10: data.imageSrc = src; idamnjanovic@10: data.imageTrg = trg; idamnjanovic@10: data.A = S(:,patch_idx); idamnjanovic@10: data.b = T; idamnjanovic@10: data.m = size(T,1); idamnjanovic@10: data.n = size(T,2); idamnjanovic@10: data.p = size(data.A,2); idamnjanovic@10: data.blocksize=blocksize; idamnjanovic@10: data.maxval=maxval; idamnjanovic@10: idamnjanovic@28: % keep coefficients matrix in sparse form and do not convert it to full. idamnjanovic@28: % geting around out of memory problem when converting big matrix from idamnjanovic@28: % sparse to full... (check SMALL_solve function) idamnjanovic@10: data.sparse=1; idamnjanovic@10: idamnjanovic@10: