Why is the rotate function not working for images?

4 views (last 30 days)
The documentation for the rotate function says that it works for images, but I am not getting the results I expect.
For example, in the following code, I’m trying to rotate both a line and an image object 45 degrees around the x-axis, but while the line is rotating the image is remaining in the x-y plane.
x = 100:100:1000;
y = randi(10,1,10)*50;
figure;
ax = axes;
h = plot(x,y);
rotate(h,[1 0 0],45)
hold on
im = imread('football.jpg');
i = image(im);
rotate(i,[1 0 0],45)
view(3)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Jul 2023
Edited: MathWorks Support Team on 3 Jul 2023
The issue with using the rotate command on images is that the Image objects do not have ZData.
Normally, when working with something like a patch or surface, the rotate command collects the XData, YData, and ZData from the object, applies the requested transformation to the data, then updates the XData, YData, and ZData on the patch or surface.
This doesn’t work with images because Image objects don’t have ZData. Instead, when querying the data from Image objects, the rotate command assumes that the ZData is all zeros. It then applies the requested transformation, but because there is no ZData property on Image objects, it can only update the XData and YData properties, and the image remains in the x-y plane.
If you want to rotate images, you should use the hgtranform and makehgtform commands instead. For example, the following code will rotate both the line and image 45 degrees around the x-axis.
x = 100:100:1000;
y = randi(10,1,10)*50;
figure;
ax = axes;
ht = hgtransform(ax);
h = plot(ht, x,y);
im = imread('football.jpg');
i = image(ht, im);
ht.Matrix = makehgtform('xrotate', pi/4)
view(3)
the hgtranform and makehgtform commands instead. For example, the following code will rotate both the line and image 45 degrees around the x-axis.
Note: You can also see an alternative solution in the following link.
- How do I insert an image in my 2-D and 3-D plots in MATLAB 8.2 (R2013b)?

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!