2-D spatial transformation.

1 次查看(过去 30 天)
Adam McNamara
Adam McNamara 2016-12-8
Hi, I'm stumped, I thought this would be easy but it hasn't turned out that way, at least for me.
I have an image and a rectangular patch which can reside anywhere within an image. I need to first transform the patch image onto a quadrilateral and subsequently transfer the rest of the image onto the same plane as the patch. The first part was easy using the projective transformation available from imtransform, ( https://uk.mathworks.com/help/images/ref/imtransform.html ) however I cannot figure out how to transform the rest of the image so that it follows the perspective determined by the transform of the patch.
Using the example on the matlab site, I essentially need to be able to add the full image to subplot(1,3,3) after inferring the vertices of the larger quadrilateral from the smaller one.
if true
% Set up an input coordinate system so that the input image
% fills the unit square with vertices (0 0),(1 0),(1 1),(0 1).
I = imread('cameraman.tif');
udata = [0 1]; vdata = [0 1];
XCoordinates=[31 140];
YCoordinates=[51 150];
P=I(YCoordinates(1):YCoordinates(2),XCoordinates(1):XCoordinates(2),:);
% Transform to a quadrilateral with vertices (-4 2),(-8 3),
% (-3 -5),(6 3).
tform = maketform('projective',[ 0 0; 1 0; 1 1; 0 1],...
[-4 2; -8 -3; -3 -5; 6 3]);
% Fill with gray and use bicubic interpolation.
% Make the output size the same as the input size.
[B,xdata,ydata] = imtransform(P, tform, 'bicubic', ...
'udata', udata,...
'vdata', vdata,...
'size', size(P),...
'fill', 128);
subplot(1,3,1), imshow(I,'XData',udata,'YData',vdata), ...
axis on
subplot(1,3,2), imshow(B,'XData',xdata,'YData',ydata), ...
axis on
subplot(1,3,3), imshow(B,'XData',xdata,'YData',ydata), ...
axis on
end
As background, the irregular polygon is a piece of wall on a photo taken at an angle. That part of the wall is a known size and shape and with a height width ratio equal to the patch in the image. However the rest of the wall size is not known so I need to use the transform and position of the patch in the image to determine the transform for the entire image. Unless there is some simpler approach?
Many thanks for at least reading this far.. pff today has been a rough day.
Cheers Adam

回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by