Rotating a meshgrid by 30 degrees?
5 次查看(过去 30 天)
显示 更早的评论
Is there a better way to do this in matlab other than multiplying it by a rotation matrix? I was also not sure on how to do so since the only rotation matrices I know are of sizes 2 x 2 or 3 x 3. I made a meshgrid of input 64 and the output was a very large vector array.
clear all;close all;clc;
%% (a) resize images with or without anti-aliasing filter
A = imread('filename');
B = imresize(A, [256, 256]);
N = 64;
[xx,yy] = meshgrid(1:N);
0 个评论
回答(1 个)
Walter Roberson
2021-1-27
For that particular angle:
syms XX YY
r32 = sqrt(3)/2;
rx = XX*r32 + YY/2;
ry = -XX/2 + YY*r32;
However, this assumes rotation around (0,0), which if you were using image() or imshow() and defaults, would correspond to the top left corner. If you wanted to rotate around the middle, by far the easiest way to calculate it would be with a rotation matrix:
N = 64;
syms XX YY
M = makehgtform('translate',[-N/2 -N/2 0], 'zrotate', pi/6, 'translate', [N/2 N/2 0]);
rot_formula = (M * [XX;YY;0;1]).';
[xx, yy] = ndgrid(1:N);
rx_formula(XX,YY) = rot_formula(1);
ry_formula(XX,YY) = rot_formula(2);
rx = double(rx_formula(xx,yy));
ry = double(ry_formula(xx,yy));
scatter(xx(:), yy(:), 'k');
hold on
axis equal
scatter(rx(:), ry(:), 'b');
hold off
3 个评论
Walter Roberson
2021-1-27
Habit. ndgrid is more consistent than meshgrid as you go into higher dimensions. The first output for ndgrid have values from the first input to ndgrid, but for meshgrid, the first output for meshgrid has values from the second input to meshgrid.
There are a few older functions that need meshgrid style meshes, such as surf(), but a lot of the time it does not matter whether you use meshgrid or ndgrid for 2D or 3D data. For more then 3D then you must use ndgrid() as meshgrid does not generalize to 4 or more dimensions.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!