For some reason imtransform
function ignores translation part by default.
If I add additional space with XData
and YData
as it said in manual, I will handle only simple cases (i.e. translation only).
So, how to apply full-featured affine transform in Matlab?
I = imread('cameraman.png');
imshow(I);
% does not translate
xform = [1 2 0; 2 1 0; 100 0 1];
T = maketform('affine',xform);
I2 = imtransform(I,T);
figure, imshow(I2)
% translates but cuts some portion of an image
xform = [1 2 0; 2 1 0; 100 0 1];
T = maketform('affine',xform);
I2 = imtransform(I,T,'XData',[1 size(I,2)+xform(3,1)],'YData',[1 size(I,1)+xform(3,2)]);
figure, imshow(I2)
So I found I should apply transform to image range too.
After that I can decide, what to do if an image corner is not at the beginning of coordinates.
I = imread('cameraman.png');
XData=[1 size(I,2)];
YData=[1 size(I,1)];
imshow(I);
xform = [1 2 0; 2 1 0; 100 0 1];
T = maketform('affine',xform);
[XData1, YData1] = tformfwd(T, XData, YData);
if XData1(1)>1
XData1(1)=1;
end
if YData1(1)>1
YData1(1)=1;
end
I2 = imtransform(I,T,'XData',XData1,'YData',YData1);
figure, imshow(I2)