individual pixel translations in an image

Hi, I would like to translate pixels within an image individually by using an external image for the translations. I only need horizontal translations. I have managed to write the following, where the pixels are removed accordingly but not updated into the new locations. If you run this example, the square inside the sphere is removed but not updated 50 pixels to the right...
Can anybody help here?
Thanks
clear
% create sphere
sphere = zeros(256,256);
[X,Y] = meshgrid(-127:128,-127:128);
[THETA,RHO] = cart2pol(X,Y);
sphere(RHO<20) = 10;
%create displacements
Tx = zeros(size(sphere));
Tx(126:130,126:130) = Tx(126:130,126:130) - 50;
%Move pixels by linear interpolation
I2 = sphere;
[x y]= meshgrid(0:size(I2,1)-1,0:size(I2,2)-1);
% Calculate the Transformed coordinates
Tlocalx = x+Tx;
% All the neighborh pixels involved in linear interpolation.
xBas0=floor(Tlocalx);
xBas1=xBas0+1;
% limit indexes to boundaries
check_xBas0=(xBas0<0)|(xBas0>(size(I2,1))-1);
xBas0(check_xBas0)=0;
check_xBas1=(xBas1<0)|(xBas1>(size(I2,1))-1);
xBas1(check_xBas1)=0;
% Linear interpolation constants (percentages)
xCom=Tlocalx-xBas0;
perc1=xCom;
perc0=1-xCom;
% Get the transform intensities
a = I2(xBas0 + y*size(I2,1)+1).*perc0;
b = I2(xBas1 + y*size(I2,1)+1).*perc1;
Iout=(a+b);
imtool(Iout,[])
imtool(sphere,[])
clear RHO THETA X Y a b I2 perc1 perc0 xBas0 xBas1 xCom check_xBas1 check_xBas0 Tlocalx x y

Answers (1)

If you want to shift an image, see my attached demo.

Asked:

mb
on 20 Jan 2015

Answered:

on 20 Jan 2015

Community Treasure Hunt

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

Start Hunting!