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

How to translate an image?

Asked by Billy on 4 Dec 2012

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?

Billy

Products

No products are associated with this question.

2 Answers

Answer by 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

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

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.

Billy on 4 Dec 2012

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
Walter Roberson
Answer by Image Analyst on 4 Dec 2012
Edited by Image Analyst on 4 Dec 2012

Try Sean's imtranslate: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A93134

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().

0 Comments

Image Analyst

Contact us