Matrix / image rotation
3 views (last 30 days)
Show older comments
Dear MATLAB community,
I am trying to rotate gridded data (x, y, z) each the size of 1401x1401.
It seems that during rotation the data is cut so that data gets lost. To circumvent that, I create larger matrices Mx, My, Mz.
load test.mat
Mx=nan(2000,2000);
My=nan(2000,2000);
Mz=nan(2000,2000);
Mx(400:1401+400-1,400:1401+400-1)=x;
My(400:1401+400-1,400:1401+400-1)=y;
Mz(400:1401+400-1,400:1401+400-1)=z;
Then, I rotate Mx, My, Mz by the angle phi:
phi = 77;
x_r = imrotate(Mx, -phi,'crop');
y_r = imrotate(My, -phi,'crop');
z_r = imrotate(Mz, -phi,'crop');
When I do imshow(x_r), imshow(y_r), imshow(z_r) it seems to look alright.
However, doing surf(x_r, y_r, z_r, 'FaceAlpha',0.5,'edgecolor','none') or mesh(x_r, y_r, z_r, 'FaceColor','flat','FaceAlpha','0.5','EdgeAlpha','0.5') it looks like that no rotation was performed.
Also, I noticed that data along x=0 and y=0 got removed.
I want to do the rotation to merge x_r, y_r, z_r with other data.
I tried a lot of different other things, e.g., ‘loose’ instead of ‘crop’, replacing zeros with NaNs but I am stuck here. I appreciate any hint or solution.
Best!
3 Comments
Matt J
on 5 May 2023
I think I should not rotate z but only x and y. Does that make sense?
Rotating z only by phi or rotating x,y only by -phi should have the same effect if you use the myRotate function in my comment below. However, I discourage the latter. Life is always easier if you have a fixed domain space for your functions and surfaces.
Accepted Answer
Matt J
on 4 May 2023
Edited: Matt J
on 4 May 2023
Rotate the z data only.
load test.mat
phi = 45;
z_r=imrotate(z,-phi,'crop');
map=~imrotate(~isnan(z),-phi,'crop');
z_r(map)=nan;
surf(z_r,'FaceColor','flat','EdgeColor','none')
2 Comments
Matt J
on 5 May 2023
Edited: Matt J
on 5 May 2023
You can rotate them in the same manner. It's just not appropriate to do so if you want to see a rotated surface plot.
x_r=myRotate(x,phi);
y_r=myRotate(y,phi);
z_r=myRotate(z,phi);
function q_r=myRotate(q,phi)
q_r=imrotate(q,-phi,'crop');
map=~imrotate(~isnan(q),-phi,'crop');
q_r(map)=nan;
end
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots 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!