rotate a matrix with a fixed grid position

5 次查看(过去 30 天)
Hi,
I have a matrix of values A with their corresponding coordinate contained within the matrices X and Y. I can visualise the data by simply typing pcolor(X,Y,A). Visually, the data seems rotated around its centre. So, I tried to rotate A using imrotate or affine2d but the resulting matrix B has different dimensions and I can no longer relate it to the coordinate system X,Y without changing it also. It is possible to rotate A without changing X,Y ? Maybe I have to interpolate?
Thank you very much, this is quite confusing for me.
  2 个评论
Image Analyst
Image Analyst 2018-1-20
Attach your data and code, and a screenshot.
Daniel Mella
Daniel Mella 2018-1-20
B=imrotate(A,1) produces a bigger matrix filled with zeros and I cannot relate it to [X,Y] With B=imrotate(A,1,'crop') I lose information on the corners introducing zero values.
Basically, I need to change the orientation of the matrix A without losing information and without losing its correspondence with the grid [X,Y]

请先登录,再进行评论。

回答(1 个)

Akanksha
Akanksha 2025-3-1
The following code changes the orientation of matrix A by the desired angle (here 1 degrees) while being in correspondence with grid [X,Y] :
%% Grid of X and Y coordinates from -10 to 10 (200 points each)
[X, Y] = meshgrid(linspace(-10, 10, 200), linspace(-10, 10, 200));
% Matrix A
A = exp(- (X.^2 + Y.^2) / 50);
% Visualizing the original data
figure;
pcolor(X, Y, A);
shading interp;
colorbar;
title('Original Data');
%%Defining the Rotation Transformation
theta = 1; % rotation angle in degrees
tform = affine2d([cosd(theta) -sind(theta) 0;
sind(theta) cosd(theta) 0;
0 0 1]);
%% Creating the Spatial Referencing Object
% The imref2d object defines the world coordinate limits for A.
Rin = imref2d(size(A), [min(X(:)), max(X(:))], [min(Y(:)), max(Y(:))]);
%% Applying the Transformation with imwarp
% Use the 'OutputView' parameter to force the output to have the same coordinate grid as Rin.
B = imwarp(A, tform, 'OutputView', Rin);
%% Visualizing the Rotated Data
figure;
pcolor(X, Y, B);
shading interp;
colorbar;
title('Rotated Data');
I have run the above code locally, and successfully produced the required output. I will attach the original and transformed data visualizations for your reference below -
Also find the links to required documentation for further reference :
Hope this helps. Thanks.

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by