How do I use IMTRANSFORM to return a registered image with the same dimensions as my base image?

2 views (last 30 days)
If I perform a projective transformation on 'westconcordaerial.png' with 'westconcordorthophoto.png' as my base image, my resulting image ('registered') is bigger than my base image ('westconcordorthophoto.png'):
orthophoto = imread('westconcordorthophoto.png');
unregistered = imread('westconcordaerial.png');
input_points=[315.9797 81.5895; 179.9029 192.4000; ...
134.6591 308.4211;321.1603 219.4947; 46.9345 70.4737];
base_points=[ 342.3770 132.3825; 213.8397 208.2421; ...
149.4108 303.1474;328.9142 254.0807; 109.9842 77.5053];
tform = cp2tform(input_points,base_points,'projective');
registered = imtransform(unregistered,tform);
subplot(1,2,1);imshow(orthophoto); %display the images in the same figure
subplot(1,2,2);imshow(registered);
If I type 'whos' at the MATLAB command line, I see that the first and second dimensions of the registered image are not quite the same size as the base image. If I want to overlay the two images together (ie: add them or subtract them) they must have the same dimensions.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
What IMTRANSFORM does when it gets a TFORM with no other spatial information is to try to determine a rectangle in the TFORM's output space that contains all, or at least most of, the input image ('westconcordaerial.png' in this case). Then it defines output image coordinates within this rectangle. The idea is to follow the input image to wherever the TFORM translates it, so that the output image will contain something interesting. Otherwise, it could wind up being all, or mostly, fill values.
The output will overlay the base if you just set the additional parameters of IMTRANSFORM, 'Xdata' and 'Ydata', and 'XYScale' to be [1 size(base,2)], [1 size(base,1)], and [1 1] respectively. For example:
% Set up the problem
orthophoto = imread('westconcordorthophoto.png'); % base image
unregistered = imread('westconcordaerial.png');
input_points=[315.9797 81.5895; 179.9029 192.4000; ...
134.6591 308.4211;321.1603 219.4947; 46.9345 70.4737];
base_points=[ 342.3770 132.3825; 213.8397 208.2421; ...
149.4108 303.1474;328.9142 254.0807; 109.9842 77.5053];
tform = cp2tform(input_points,base_points,'projective');
registered = imtransform(unregistered,tform);
% Display the problem in the same figure
figure;
subplot(1,2,1);imshow(orthophoto);
subplot(1,2,2);imshow(registered);
% Now get the location of the base so we can specify the location of the output image
% Note that we can assume the base has pixels with unity width and height, so
% each pixel maps to 1 unit of position in the input space
[rows cols]=size(orthophoto);
% Define the location of the image in the output space
xd = [1 cols];
yd = [1 rows];
% Define the width and height of each pixel with respect to the output space
% Since we assumed that the base has a 1-1 pixel size to input space unit mapping,
% we'll define the ouput to be the same mapping so that the output image will
% have the same size and position as the base
xys = [1 1];
% Perform the transformation and show it
registered2 = imtransform(unregistered, tform, 'Xdata', xd, 'Ydata', yd, 'XYScale', xys);
figure;subplot(1,2,1);imshow(orthophoto);
subplot(1,2,2);imshow(registered2);
Note that parts of the input image may not appear in the base image. For example, the edges of an aerial image may view areas on the ground not included in the base image. Conversely, the base image may cover a very large area of ground, and the input image may cover only a small part in the middle of it. In this case, there will be extensive fill around the periphery of the output image. Truncation and fill often happen at the same time -- whenever the input image covers an area that intersects the edge of the area covered by the base image.
You should now be able to add the two images together. Here is an example that calculates the edges of the registered image and adds the edges of the registered image to the base image:
% Detect the edges of the output image
corners = edge(rgb2gray(registered2),'canny');
% Add the output edges to the original "base" image and show it
added=imadd(uint8(200*corners),orthophoto);
figure;imshow(added)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!