Thread Subject: Translate and pad image

Subject: Translate and pad image

From: Rajesh Acharya

Date: 13 May, 2008 08:32:03

Message: 1 of 8

hello friends,
 I have two images. mirror image of each other. but the
center of the images are not exactly alligned. I need to
find the misalignment and translate one of the image to
match the other exactly....

I read the images in matlab then took their mean then
substracted the two then squared this difference then
summed all the elements of this square of difference...

shifted the image by 1 column to left repeat all steps

plot the sum of square of difference to find minima.
image corrousponding to minima is my alligned image...
right???

the code written below works for me but it is too big and
is not doing that big a job...also takes longer (0.4 sec)
its just a part of a bigger code....

my questions:
 any simple alternative??? readymade function??

 in this case i have shifted the movable image to left one
pixel at a time ... in case i dont know how do i decide
which direction to shift the image left or right ..... or
should i keep going in both directions hunting for a
minima....

 here theres only tranlstional misallignment what if the
central axis is tilted??? (rotational misalignment) how
can i correct for that??? any suggestions

Thank you for your time!!

my code:

% subtract mirror images

I=imread('F:\Rajesh\Matlab\GUI_1\Actual...
Images\teapot1.tif');
I=double(I);
Iavg=mean(I);
% taken mean to reduce calculation time
[row,column]=size(Iavg);
IM=imread('F:\Rajesh\Matlab\GUI_1\Actual...
Images\teapot180.tif');
IM=fliplr(double(IM));
pad=mean(IM);
for i=1:column/2,
%{
opened the image above the for loop... just traslating the
image here and padding with avg of the last row of
reference image... any other suggestion for padding
welcome... any other short method for translating
and padding would be welcome..... :)
%}

N=i-1;
se = translate(strel(1), [0 -N]);
IMavg = imdilate(IM,se);
%to translate by inteeger pixel value.....
h = fspecial('gaussian');
%low pass filer ..image is almost same
IMavg = imfilter(IMavg,h,'replicate');
%replicate doesnt seem to work i get last column dark :(
IMavg = mean(IMavg);
if N>0,
IMavg(1,(column-N):column)= pad(1,column);
end
Idiff=Iavg-IMavg;
Diff(i)=sum(Idiff.^2);

if N>0,
if Diff(N)<Diff(i), break; end
end
% above logic is also not strong to find minima any
suggestions here??

end
disp(N); % to know the total shift.


Subject: Translate and pad image

From: Rajesh Acharya

Date: 13 May, 2008 11:37:02

Message: 2 of 8

....contd..
Can i convert the element number (N in the code) in the
image matrix to pixel or subpixel number so as to move the
image by that many pixels??
I have to move series of such images total 90 numbers from
0deg to 180 deg applying this correction of image
translation incrementally...
thanks again..


Subject: Translate and pad image

From: ImageAnalyst

Date: 13 May, 2008 15:23:05

Message: 3 of 8

On May 13, 4:32=A0am, "Rajesh Acharya" <rv_acha...@yahoo.com> wrote:
> hello friends,
> =A0I have two images. mirror image of each other. but the
> center of the images are not exactly alligned. I need to
> find the misalignment and translate one of the image to
> match the other exactly....
>
> I read the images in matlab then took their mean then
> substracted the two then squared this difference then
> summed all the elements of this square of difference...
>
> shifted the image by 1 column to left repeat all steps
>
> plot the sum of square of difference to find minima.
> image corrousponding to minima is my alligned image...
> right???
>
> the code written below works for me but it is too big and
> is not doing that big a job...also takes longer (0.4 sec)
> its just a part of a bigger code....
>
> my questions:
> =A0any simple alternative??? readymade function??
>
> =A0in this case i have shifted the movable image to left one
> pixel at a time ... in case i dont know how do i decide
> which direction to shift the image left or right ..... or
> should i keep going in both directions hunting for a
> minima....
>
> =A0here theres only tranlstional misallignment what if the
> central axis is tilted??? (rotational misalignment) how
> can i correct for that??? any suggestions
>
> Thank you for your time!!
>
> my code:
>
> % subtract mirror images
>
> I=3Dimread('F:\Rajesh\Matlab\GUI_1\Actual...
> Images\teapot1.tif');
> I=3Ddouble(I);
> Iavg=3Dmean(I);
> % taken mean to reduce calculation time
> [row,column]=3Dsize(Iavg);
> IM=3Dimread('F:\Rajesh\Matlab\GUI_1\Actual...
> Images\teapot180.tif');
> IM=3Dfliplr(double(IM));
> pad=3Dmean(IM);
> for i=3D1:column/2,
> %{
> opened the image above the for loop... just traslating the
> image here and padding with avg of the last row of
> reference image... any other suggestion for padding
> welcome... any other short method for translating
> and padding would be welcome..... =A0:)
> %}
>
> N=3Di-1;
> se =3D translate(strel(1), [0 -N]);
> IMavg =3D imdilate(IM,se);
> %to translate by inteeger pixel value.....
> h =3D fspecial('gaussian'); =A0
> %low pass filer ..image is almost same
> IMavg =3D imfilter(IMavg,h,'replicate');
> %replicate doesnt seem to work i get last column dark :(
> IMavg =3D mean(IMavg);
> if N>0,
> IMavg(1,(column-N):column)=3D pad(1,column);
> end
> Idiff=3DIavg-IMavg;
> Diff(i)=3Dsum(Idiff.^2);
>
> if N>0,
> if Diff(N)<Diff(i), break; end
> end
> % above logic is also not strong to find minima any
> suggestions here??
>
> end
> disp(N); % to know the total shift.
----------------------------------------------------------------------------=
------------------------------------------
Rajesh:
Look up "registering an image" in the help file. You can use cross
correlation to align them (I think this is essentially what you were
describing), so look up cross corellation in the help file also. Make
sure you get the right/left mirroring figured out before you begin or
else it won't work right. I think for large images, it can be done
faster in the Fourier domain.
Regards,
ImageAnalyst

Subject: Translate and pad image

From: Rajesh Acharya

Date: 14 May, 2008 09:07:02

Message: 4 of 8

Thanks ImageAnalyst, it was informative indeed

but my problem is not solved as yet because...

1.
I have n number of images to be alligned 200/250/350/400
etc. from 0 to 360 deg. so i cannot select control points
for each... would like matlab to do it for me instead...

once i know the maximum shift which would be there between
image at 0 and 180 i can distribute this shift evenly over
all the images over 360 deg.

2.
 the correction that i have found above in the code is in
total number of columns reqd to be shifted ...thats fine
but not accurate.. i want to operate at subpixel level for
higher accuracy...

 how to convert this no of columns of the image variable
into pixels/subpixel in the image???



Subject: Translate and pad image

From: ImageAnalyst

Date: 14 May, 2008 14:25:33

Message: 5 of 8

On May 14, 5:07=A0am, "Rajesh Acharya" <rv_acha...@yahoo.com> wrote:
> Thanks ImageAnalyst, it was informative indeed
>
> but my problem is not solved as yet because...
>
> 1.
> I have n number of images to be alligned 200/250/350/400
> etc. from 0 to 360 deg. so i cannot select control points
> for each... would like matlab to do it for me instead...
>
> once i know the maximum shift which would be there between
> image at 0 and 180 i can distribute this shift evenly over
> all the images over 360 deg.
>
> 2.
> =A0the correction that i have found above in the code is in
> total number of columns reqd to be shifted ...thats fine
> but not accurate.. i want to operate at subpixel level for
> higher accuracy...
>
> =A0how to convert this no of columns of the image variable
> into pixels/subpixel in the image???
-------------------------------------------------------
Rajesh:
Are you now saying that you need to rotate the images to align them
instead of just translating them like your subject line says)?
Regards,
ImageAnalyst

Subject: Translate and pad image

From: Rajesh Acharya

Date: 15 May, 2008 04:56:02

Message: 6 of 8

Dear ImageAnalyst:
on second thoughts I think your idea of image registration
is fine and workable...
what i was thinking though was something different ...
I think i am not stating my problem properly and adequately.
let me start from begining...
I am taking images of an object(specimen/phantom) placed on
a turn table at various angles from 0deg to 360 deg. (like
they do in CT/MRI etc.) The necessary condition here is to
match the center of rotation of the turn table (axis of
rotation) with the verticle center line of the image. this
condition is difficult to fulfill manually while grabbing
image....
however, we can computerise this. how? well, irrespective
of the shape and size of the object we are sure that the
images at 0 deg and 180 deg are mirror images of each other.
so what I do is I flip the image at 180 deg. and subtract
it from 0 deg ref. image. ideally the Background and
specimen being same i should get zeros if the aforesaid
condition matches...practically speaking there would be
temporal statistical variations (ignorable) and variations
if there's mismatch in the two axis (ie if the object is
rotating eccentrically). therefore, I simply translate the
image matrix of 180 deg image one column at a time and find
the difference with ref image as stated in the original
post...the point where the diff is minimum gives me the
mismatch between the two axes....

 the advantage is i am not involving the user in this
process to feed control points ...

 coming back to my point.
Firstly, here i see that if i move my 180 deg image matrix
by 13 columns to left i get minimum difference bet ref
image (0 deg) and this one. Can i refine this difference
(13 columns) even further at subpixel level...
just to elaborate, this difference of 13 col will have to
be appropriately distributed in all the images between 0deg
and 180 deg images to allign all of them...

Secondly, there may be a chance that the axis of rotation
is not exactly verticle, this induces angular misalignment
in all the images... how do i correct this misalignment in
all the images
thanks yet again..

Subject: Translate and pad image

From: ImageAnalyst

Date: 15 May, 2008 20:43:59

Message: 7 of 8

On May 15, 12:56=A0am, "Rajesh Acharya" <rv_acha...@yahoo.com> wrote:
> Dear ImageAnalyst:
> on second thoughts I think your idea of image registration
> is fine and workable...
> what i was thinking though was something different ...
> I think i am not stating my problem properly and adequately.
> let me start from begining...
> I am taking images of an object(specimen/phantom) placed on
> a turn table at various angles from 0deg to 360 deg. (like
> they do in CT/MRI etc.) The necessary condition here is to
> match the center of rotation of the turn table (axis of
> rotation) with the verticle center line of the image. this
> condition is difficult to fulfill manually while grabbing
> image....
> however, we can computerise this. how? well, irrespective
> of the shape and size of the object we are sure that the
> images at 0 deg and 180 deg are mirror images of each other.
> so what I do is I flip the image at 180 deg. and subtract
> it from 0 deg ref. image. ideally the Background and
> specimen being same i should get zeros if the aforesaid
> condition matches...practically speaking there would be
> temporal statistical variations (ignorable) and variations
> if there's mismatch in the two axis (ie if the object is
> rotating eccentrically). therefore, I simply translate the
> image matrix of 180 deg image one column at a time and find
> the difference with ref image as stated in the original
> post...the point where the diff is minimum gives me the
> mismatch between the two axes....
>
> =A0the advantage is i am not involving the user in this
> process to feed control points ...
>
> =A0coming back to my point.
> Firstly, here i see that if i move my 180 deg image matrix
> by 13 columns to left i get minimum difference bet ref
> image (0 deg) and this one. Can i refine this difference
> (13 columns) even further at subpixel level...
> just to elaborate, this difference of 13 col will have to
> be appropriately distributed in all the images between 0deg
> and 180 deg images to allign all of them...
>
> Secondly, there may be a chance that the axis of rotation
> is not exactly verticle, this induces angular misalignment
> in all the images... how do i correct this misalignment in
> all the images
> thanks yet again..

-------------------------------------
Rajesh:
The way to "correct" for angular misalignment is to just eliminate it
in the first place, or, as you know, you'll get artifacts. To do
this, you construct a "bead" phantom. Get a tube (like a rigid
drinking or something better) and fill it with ball bearings. Mount
it vertically on yoru platform sticking up perpendicular to the
platform. Then spin it and snap pictures. You'll see a series of
crossing horizontal lines (like X's) if it's not perfectly
perpendicular to your platform. It doesn't even have to be in the
center of the axis of rotation. Being off-center just affects how
long the lines are. If it is perfectly aligned (using tip/tilt on
your platform), then you will see no crossing X's and just perfectly
straight lines (with the lines being longer the farther they are away
from the axis of rotation).
This is the alignment method used by Hytec to align their industrial
CT systems.
http://www.hytecinc.com/category.php?action=3Dexpand&id=3D17
I think your method finds the center column of pixels - the one that
corresponds to the vertical axis of rotation - but doesn't help you
otherwise align your system. And it doesn't help you find the center
of your image, just the vertical axis. You'd be better off fastening
a bed of nails to your sensor and looking at the shadows (where they
all point to) to find the center of your x-ray image in both vertical
and horizontal directions rather than just the vertical direction like
you're doing.
Regards,
ImageAnalyst

By the way, there is a nice microCT short course at SPIE Symposium in
San Diego this August 2008.
http://spie.org//app/program/index.cfm?fuseaction=3DCOURSE&export_id=3Dx1310=
2&ID=3Dx12770&redir=3Dx12770.xml&course_id=3DE0865949&event_id=3D840091&prog=
ramtrack_id=3D844382

Subject: Translate and pad image

From: Rajesh Acharya

Date: 16 May, 2008 05:33:02

Message: 8 of 8

Gud morning Imageanalyst,
yes i'll try all those sugestions...thank you.
but still feel like incorporating all these corrections in
the software...to be able to live with real world aligning
problems...
hey your inputs were insightful...

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
ct Rajesh Acharya 13 May, 2008 04:35:14
mri Rajesh Acharya 13 May, 2008 04:35:14
image processing Rajesh Acharya 13 May, 2008 04:35:14
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com