1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
w= 5;
q = 1000;
name = 'corral';
n = 128;
M0 = load_image(name,n);
M0 = rescale( crop(M0,n) );
%%% matrices representing the X and Y position of the pixel to extract
p = n-w+1;
% location of pixels
[Y,X] = meshgrid(1:p,1:p);
% offsets
[dY,dX] = meshgrid(0:w-1,0:w-1);
% location of pixels to extract
X = reshape(X, [1 1 p p]);
Y = reshape(Y, [1 1 p p]);
X = repmat(X, [w w 1 1]) + repmat(dX, [1 1 p p]);
Y = repmat(Y, [w w 1 1]) + repmat(dY, [1 1 p p]);
%% extract all patches
P0 = M0(X + (Y-1)*n);
P0 = reshape(P0,w,w,p*p);
sel = randperm(size(P0,3)); sel = sel(1:q);
P0 = P0(:,:,sel);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Image Patch-wise Projection
n = 128;
M = rand(n);
ofx = 2;
ofy = 1;
%% patch location
[Y,X] = meshgrid(1:w:n, 1:w:n);
p = size(X,1);
[dY,dX] = meshgrid(0:w-1,0:w-1);
X = reshape(X, [1 1 p p]);
Y = reshape(Y, [1 1 p p]);
X = repmat(X, [w w 1 1]) + repmat(dX, [1 1 p p]);
Y = repmat(Y, [w w 1 1]) + repmat(dY, [1 1 p p]);
Xs = mod(X+ofx-1, n)+1;
Ys = mod(Y+ofy-1, n)+1;
P = M(Xs + (Ys-1)*n);
% Replace each patch by its closest match.
for i=1:p*p
% distance to current patch
d = sum(sum( (P0 - repmat(P(:,:,i), [1 1 q])).^2 ) );
% best match
[tmp,s] = min(d);
% replace the patch
P(:,:,i) = P0(:,:,s);
end
% reconstruction
Mp = M;
Mp(Xs + (Ys-1)*n) = P;
figure;imageplot(M,'Input', 1,2,1);
imageplot(Mp,'Projected', 1,2,2); |
Partager