How to translate an image?

I want to know how it would be possible to use a loop to translate an image. I am not looking for full written out code, just something that allows my brain to wrap around so I can at least be given a starting point. Thanks.

1 Comment

Why would you want do it with a loop, as opposed to a matrix transform?

Sign in to comment.

Answers (2)

Presuming you are not talking about optical character recognition and natural language processing:
set the XData and YData properties of the image() handle to move the image on the screen.
Or if you are working with matrices, then construct an output matrix of appropriate size and assign the input matrix (or a suitable portion of it) to the position within the output matrix.
OutMatrix = zeros(512, 512, 3);
OutMatrix(183:214, 7:51, :) = InMatrix;
tranlates InMatrix by 182 in one dimension and by 6 in the other dimension.

6 Comments

Is there a way I could do a loop of this function. I was hoping to be able to translate an image zigzag. As in the person who runs the code could see the translation. My prof's code was too difficult to manipulate to allow me to zigzag. His code is here and I have tried changing all sorts of signs from positive to negative to get the picture to move the other direction, but nothing has worked so far. The example he showed moved an image from the upper left to the lower right, but I do not see any coordinates in his code, unlike yours. I think if I generalize yours and put it in a loop it might work.
[row col pg] = size(cm);
inc = 4; % step size
rep = 30; % number repeats
for j =1:rep
canv2 = canv;
del = j*inc;
canv2(1+del:row+del,1+del:col+del,:) =cm;
filter = find(canv2 > 0);
canv3 = bg;
canv3(filter) = canv2(filter);
image(canv3)
pause(0.05)
end
Is it mandatory to do it by creating a composted image? It is a lot easier to just have two images, the smaller on top of the bigger, and set() the coordinates of the smaller image so that it moves.
Well I am sure it would be even better if I do not use his code. My code for putting my small image over my back ground image is this:
x=900
y=100
sceneFile = 'wind_turbines.jpg'; % this image from Mac OS desktop images
scene = fLoadImageFile(sceneFile);
objFile = 'airplanes.jpg';
obj = fLoadImageFile(objFile);
gs = 0;
im=fPlaceImageOnScene(scene,obj,gs,x,y);
X=image(im)
Where fPlaceImageOnScene is this:
function compImage = fPlaceImageOnScene(bg,obj,gs,x,y)
[brows bcols bpg] = size(bg);
[orows ocols opg] = size(obj);
pg = zeros(brows,bcols,'uint8');
switch gs
case 0
% leave black
case 255
% change to white
pg = pg+255;
otherwise
disp('can only handle white and black backgrounds')
end
canv = cat(3,pg,pg,pg);
canv(1+y : orows+y, 1+x : ocols+x, :) = obj;
switch gs
% get indices of where obj is on canv
case 0
filter = find(canv > 0);
case 255
filter = find(canv < 255);
otherwise
disp('can only handle white and black backgrounds')
end
% put obj from canv onto copy of bg
compImage = bg;
compImage(filter) = canv(filter);
end
I have tried using the line X=image(im) as my image to move around using his code for translation but an error keeps on showing up involving ambiguous dimensions.
Sample:
bgh = image(bg);
objh = image(obj);
for K = 1 : 50
newx = 1 + floor(rand * size(bg, 1));
newy = 1 + floor(rand * size(bg, 2));
maxx = newx + size(obj, 1) - 1;
maxy = newy + size(obj, 2) - 1;
set(objh, 'XData', [newx maxx], 'YData', [newy maxy]);
pause(1);
end
I tried running the sample, with my files and even though I did not get any error messages, the figure window only showed a white graph with axes that equal the size of my object. I also get some black boxes that flash near the bottom right of the figure window. I added the code to define obj and bg as imread('file.jpg') before running your code.
Here's my code that results in the figure window with the black boxes.
x=900
y=100
sceneFile = 'wind_turbines.jpg';
scene = fLoadImageFile(sceneFile);
objFile = 'airplanes.jpg';
obj = fLoadImageFile(objFile);
gs = 0;
im=fPlaceImageOnScene(scene,obj,gs,x,y);
image(im)
bgh=image(scene)
objh=image(obj)
for K = 1:5
newx = 1+floor(rand*size(bg,1));
newy = 1+floor(rand*size(bg,2));
maxx = newx + size(obj,1)-1;
maxy = newy + size(obj,2)-1;
set(objh, 'XData', [newx maxx], 'YData', [newy maxy]);
pause(1);
image(????)
end

Sign in to comment.

Image Analyst
Image Analyst on 4 Dec 2012
Edited: Image Analyst on 4 Dec 2012

0 votes

Of course if you need your " brain to wrap around" then maybe you also need the image to wrap around and you could use circshift. Search Answers for circshift because I recently posted code to do translation with circshift().

Asked:

on 4 Dec 2012

Community Treasure Hunt

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

Start Hunting!