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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

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: http://tinypic.com/view.php?pic=nmi053&s=6

The link for unregistered image: http://tinypic.com/view.php?pic=2zsbmuu&s=6

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

Vic on 27 Jan 2013

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);

%imshow(IMI);

%imshow(IMJ);

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

BWJremove = bwareaopen(IMJ, 5000);

%imshow(BWIremove);

%imshow(BWJremove);

[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)

Vic

Products

No products are associated with this question.

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.

11 Comments

Vic on 27 Jan 2013

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!!!

Image Analyst on 27 Jan 2013

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

Matt J on 27 Jan 2013

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.

Image Analyst
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.
     cI=vertcat(centroidsI.Centroid);
     cJ=vertcat(centroidsJ.Centroid);
     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.

0 Comments

Matt J

Contact us