Matrix / image rotation

8 次查看(过去 30 天)
PA
PA 2023-5-4
评论: Matt J 2023-5-5
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 个评论
PA
PA 2023-5-5
I am still confused and not able to make it work the way I want it.
I think I should not rotate z but only x and y. Does that make sense? It worked like this for other datasets, but the data were more distributed like a circle which made the rotation work.
If I do rotate x and y data gets cut which of course I do not want.
That is why I used ‘crop’ in ‘imrotate’.
However, then z has a different size than x_r and y_r.
Because this did not work, I created the larger matrices Mx, My and imbedded x and y there, so that ‘crop’ does only cut away NaNs and no actual data. However, it did not help, because I needed to do that for Mz also and rotate Mz to have equal sizes.
I did also x_r_single = [x_r(:)]’ and y_r_single = [y_r(:)] and plot(x_r_single, y_r_single). But it looks also like no rotation was performed. So, for me it seems it has not necessarily something to do with using ‘surf’.
Matt J
Matt J 2023-5-5
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.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2023-5-4
编辑:Matt J 2023-5-4
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 个评论
PA
PA 2023-5-5
Hi,
thanks for your answer. That helps for sure but I also need a correctly rotated x and y. Any ideas how to get those?
Matt J
Matt J 2023-5-5
编辑:Matt J 2023-5-5
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

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by