How to translate an image?
Show older comments
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
Jurgen
on 4 Dec 2012
Why would you want do it with a loop, as opposed to a matrix transform?
Answers (2)
Walter Roberson
on 4 Dec 2012
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
Billy
on 4 Dec 2012
Walter Roberson
on 4 Dec 2012
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.
Billy
on 4 Dec 2012
Walter Roberson
on 4 Dec 2012
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
Billy
on 4 Dec 2012
Billy
on 4 Dec 2012
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().
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!