Moving an image up and down
Show older comments
I have seen a code online on how to move an image, but it keeps coming up with an error message saying
"Error using image
Color data must be numeric or logical values."
what is the color data in my code?
clc;
clearvars;
close all;
workspace;
fontSize = 33;
grayImage = imread('cameraman.tif');
for angle = 0 : 10 : 360
rotatedImage = imrotate(grayImage, angle);
imshow(rotatedImage);
axis on;
caption = sprintf('Angle = %.1f', angle);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
drawnow;
pause(.5);
end
msgbox('Done with demo');
5 Comments
KSSV
on 4 Jan 2019
I suspect the image cameraman.tif is not inbuilt image.....you are using some other image...can you tell us what does which cameraman.tif shows up?
Walter Roberson
on 4 Jan 2019
cameraman.tif has been supplied with the Image Proecessing Toolbox for a long time.
KSSV
on 4 Jan 2019
I meant to say....the one he is using is not from inbuilt...;p
Ritzy Nanda
on 4 Jan 2019
Walter Roberson
on 5 Jan 2019
Once, before the loop, call imshow(nan) and assign the result to a variable, say H. Then make the axes a fixed size, and enforce this with xlim and ylim. You probably also want to axis image
Now inside your loop, do not call imshow() again. Instead, set() the CData property of H to be the new image to be displayed (such as the rotated image.) To move the image, set() the XData and YData properties of H. XData and YData are each two-element vectors. XData(1), YData(1) gives the coordinate at which to display the center of the lowest left hand corner of the image, and XData(end), YData(end) gives the coordinate at which to display the center of the upper right hand corner of the image.
Example:
for K = 1 : 10
set(H, 'XData', [K 20+K], 'YData', [20-K 40-K])
drawnow()
end
would have the image drift rightward and downward.
XData and YData are in data coordinates, so take some care about potential differences between data coordinates and pixels.
Answers (0)
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!