examples

Contents

clear
close all

example 1

T1 = [3 0 0; 0 2 0; 0 0 1];
tform1old = maketform('affine', T1);
vistformfwd(tform1old, [0 100], [0 100]);
tform1 = affine2d(T1);
vistformfwd(tform1, [0 100], [0 100]);

example 2

T2 = [1 0 0; 0.2 1 0; 0 0 1];
tform2 = affine2d(T2);
vistformfwd(tform2, [0 100], [0 100]);

example 3

Tscale = [1.5 0 0; 0 2 0; 0 0 1];
Trotation = [cos(pi/4) sin(pi/4) 0;
	-sin(pi/4) cos(pi/4) 0;
	 0 0 1];
Tshear = [1 0 0; .2 1 0; 0 0 1];
T3 = Tscale * Trotation * Tshear;
tform3 = affine2d(T3);
vistformfwd(tform3, [0 100], [0 100])

warp image (old style)

close all
f = checkerboard(50);
s = 0.8;
theta = pi/6;
T = [s*cos(theta) s*sin(theta) 0;
    -s*sin(theta) s*cos(theta) 0;
    0 0 1 ];
tform = maketform('affine',T);
g1 = imtransform(f,tform);
imshow(g1);
figure
g2 = imtransform(f,tform,'FillValue',0.5);
imshow(g2);

warp image (new style)

close all
f = checkerboard(50);
s = 0.8;
theta = pi/6;
T = [s*cos(theta) s*sin(theta) 0;
    -s*sin(theta) s*cos(theta) 0;
    0 0 1 ];
tform = affine2d(T);
[g1,ref]  = imwarp(f,tform);
imshow(g1);
ref
figure
g2 = imwarp(f,tform,'FillValues',0.5);
imshow(g2);
ref = 

  imref2d with properties:

           XWorldLimits: [-160.2895 277.7105]
           YWorldLimits: [0.1105 438.1105]
              ImageSize: [438 438]
    PixelExtentInWorldX: 1
    PixelExtentInWorldY: 1.0000
    ImageExtentInWorldX: 438
    ImageExtentInWorldY: 438.0000
       XIntrinsicLimits: [0.5000 438.5000]
       YIntrinsicLimits: [0.5000 438.5000]

projective transform (old style)

close all
I = imread('cameraman.tif');
udata = [0 1];  vdata = [0 1];  % input coordinate system
tform = maketform('projective',[ 0 0;  1  0;  1  1; 0 1],...
                               [-4 2; -8 -3; -3 -5; 6 3]);
[B,xdata,ydata] = imtransform(I,tform,'bicubic','udata',udata,...
                              'vdata',vdata,...
                              'size',size(I),...
                              'fill',128);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(B)

projective transform (new style)

close all
I = imread('cameraman.tif');
inpts = [ 0 0;  1  0;  1  1; 0 1];
outpts = [-4 2; -8 -3; -3 -5; 6 3];
tform = fitgeotrans(inpts*255,outpts*255,'projective');
[B, ref1] = imwarp(I,tform); % ,'interp','bicubic');
ref1
xy = transformPointsForward(tform,inpts*255)
imshow(B);
ref1 = 

  imref2d with properties:

           XWorldLimits: [-2.0436e+03 1.5504e+03]
           YWorldLimits: [-1.2886e+03 762.3951]
              ImageSize: [2051 3594]
    PixelExtentInWorldX: 1
    PixelExtentInWorldY: 1
    ImageExtentInWorldX: 3594
    ImageExtentInWorldY: 2051
       XIntrinsicLimits: [0.5000 3.5945e+03]
       YIntrinsicLimits: [0.5000 2.0515e+03]


xy =

   1.0e+03 *

   -1.0200    0.5100
   -2.0400   -0.7650
   -0.7650   -1.2750
    1.5300    0.7650

Warning: Image is too big to fit on screen; displaying
at 25% 
imageSize = [400 700];
xWorldLimits = [-2000 1500];
yWorldLimits = [-1200 800];
ref = imref2d(imageSize,xWorldLimits,yWorldLimits)
[B, ref2] = imwarp(I,tform,'OutputView',ref,'interp','bicubic','FillValues',128);
 ref2
% [B,xdata,ydata] = imtransform(I,tform2,'bicubic','udata',udata,...
%                               'vdata',vdata,...
%                               'size',size(I),...
%                               'fill',128);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(B)
ref = 

  imref2d with properties:

           XWorldLimits: [-2000 1500]
           YWorldLimits: [-1200 800]
              ImageSize: [400 700]
    PixelExtentInWorldX: 5
    PixelExtentInWorldY: 5
    ImageExtentInWorldX: 3500
    ImageExtentInWorldY: 2000
       XIntrinsicLimits: [0.5000 700.5000]
       YIntrinsicLimits: [0.5000 400.5000]


ref2 = 

  imref2d with properties:

           XWorldLimits: [-2000 1500]
           YWorldLimits: [-1200 800]
              ImageSize: [400 700]
    PixelExtentInWorldX: 5
    PixelExtentInWorldY: 5
    ImageExtentInWorldX: 3500
    ImageExtentInWorldY: 2000
       XIntrinsicLimits: [0.5000 700.5000]
       YIntrinsicLimits: [0.5000 400.5000]