MATLAB Answers


How can I align an image according to another image?

Asked by Vic
on 27 Jan 2013

I am trying to align an image according to a base image and according to the position of 2 circles on the left of each page.

The link for base image:

The link for unregistered image:

The code I have so far is listed below but the only thing I have a problem with is how to use imtransfrom function to register the image.

  1 Comment

I1 = imread('C:\Users\Victoras\Desktop\answertest.bmp');

J1= imread('C:\Users\Victoras\Desktop\studenttest.bmp');

I2= rgb2gray(I1); %convert colour images to grayscale

J2= rgb2gray(J1);

BWI = im2bw(I2);% convert grayscale images to binary

BWJ = im2bw(J2);

IMI = imcomplement(BWI); % invert colours, black to white and white to black

IMJ = imcomplement(BWJ);



BWIremove = bwareaopen(IMI, 5000); % remove objects less that 4500 pixels

BWJremove = bwareaopen(IMJ, 5000);



[LI, numI] = bwlabel(BWIremove);

[LJ, numJ] = bwlabel(BWJremove);

centroidsI = regionprops(LI,'Centroid');

centroidsJ = regionprops(LJ,'Centroid');

% Create a transformation structure for an affine % transformation.

t_concord = cp2tform(centroidsI,centroidsJ,'affine');

% Get the width and height of the orthophoto and perform % the transformation.

info = imfinfo(I1);

registered = imtransform(IMJ,t_concord,'XData',[1 info.Width], 'YData',[1 info.Height]);

figure, imshow(registered)

Log in to comment.

2 Answers

Answer by Image Analyst
on 27 Jan 2013
 Accepted Answer

Have you tried imregister()? It will condense all that code down into a single line of code. Go ahead, make it easy on yourself.


That is true Image Analyst. It does a very good job and I am quite happy for your help. I didn't get any help for my current code but it is great to have this solution.. Thank you again!!!

That said, if you could figure out a transform, tformfwd() would probably be faster than calling imregister() three times.

Seems like a fluke to me. What if all the 'A' answers had been filled out in the first image and all the 'D' Answers had been filled out in the second? The alignment of the blue channels would have been heavily inconsistent with the other channels.

Log in to comment.

Answer by Matt J
on 27 Jan 2013
Edited by Matt J
on 27 Jan 2013

Here's a modification of the original approach using ABSOR, available here

    [LI, numI] = bwlabel( imfill(BWIremove,'holes') );
    [LJ, numJ] = bwlabel( imfill(BWJremove,'holes')  );
    centroidsI = regionprops(LI,'Centroid');
    centroidsJ = regionprops(LJ,'Centroid');
    % Create a transformation structure for an affine % transformation.
     S=absor(cJ.', cI.');
     t_concord=maketform('affine', S.M(1:2,:).');
     registered = imtransform(IMJ,t_concord,....);

Because you're only using 2 landmarks to perform the registration, the alignment will probably not be as perfect as with imregister, although as I've said, I have my doubts about how well imregister would work across a larger population of questionnaires.

There are things you could do to improve the tilt angle estimate though, like find more landmarks, or use regionprops(...,'Orientation') on the answer boxes.


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!