Rotation of matrices and image formed from it

I have a mtrix now I want to rotate at angle 45 in such a way that image formed from matrix is also rotated but norm of matrix before and afer should be same. I am using imrotate though it provide me rotated image but norm of matrix changed. What can I do to obtain desire rsults.

1 Comment

I want all norm(t) should be same. But image should be rotated but here I am getting rotating image but rotated matrices norm are not same which effect furthr calculation and outut. Basically t is input in form of x and y matrices
x=[0.0517 0.0281 0.0122 0.0100
0.0505 0.0344 0.0100 0.0006
0.0513 0.0443 0.0122 0.0006
0.0513 0.0443 0.0152 0.0034];
y=[-0.0118 -0.0012 0.0016 -0.0025
-0.0268 -0.0076 -0.0026 -0.0026
-0.0344 -0.0337 -0.0026 -0.0025
-0.0352 -0.0329 -0.0214 -0.0130];
figure
imagesc(sqrt(x.^2+y.^2))
m=length(x);
x2=reshape(x,[],1);y2=reshape(y,[],1);
t=zeros(2*length(x2),1);
t(1:2:end)=x2;
t(2:2:end)=y2;
norm(t)
R=[cos(Set.angle) sin(Set.angle) ; -sin(Set.angle) cos(Set.angle)];
for i=1:m
for j=1:m
ts=[x(i,j) y(i,j)]';
ts=R'*ts; % Transpose: No frame change, but rotation of vector
%ts=R*ts;
% if ts(1) >= 1 && ts(1) <= i && ts(2) >= 1 && ts(2) <= j
x(i,j)=ts(1);
y(i,j)=ts(2);
% end
end
end
norm(t)
xR = imrotate(x, Set.angle*180/pi, 'bicubic', 'crop');
yR = imrotate(y, Set.angle*180/pi, 'bicubic', 'crop');
x1=reshape(xR,[],1);y1=reshape(yR,[],1);
t1=zeros(2*length(x1),1);
t1(1:2:end)=x1;
t1(2:2:end)=y1;
figure
imagesc(sqrt(xR.^2+yR.^2))
norm(t1)

Sign in to comment.

Answers (1)

Hi @Shafaq,

To achieve the desired rotation of a matrix while preserving its norm, you need to consider a few key points. The imrotate function in MATLAB is indeed useful for rotating images, but it does not maintain the norm of the matrix due to the introduction of new pixel values (typically zeros) in the output image. To maintain the norm, you can follow these steps:

*Rotate the image using imrotate.

*Calculate the norm of the original image.

*Scale the rotated image to ensure that its norm matches that of the original image.

Here is a detailed MATLAB code example that implements this approach and displays the original and rotated images side by side using subplots.

% Create a sample matrix (image)
I = imread('/MATLAB Drive/Sample image.jpg'); % Load an example image
I = im2double(I); % Convert to double for precision
% Display the original image
figure;
subplot(1, 2, 1);
imshow(I);
title('Original Image');
% Rotate the image by 45 degrees
angle = 45;
J = imrotate(I, angle, 'bilinear', 'crop'); % Rotate the image
% Calculate the norms
original_norm = norm(I(:)); % Norm of the original image
rotated_norm = norm(J(:)); % Norm of the rotated image
% Scale the rotated image to match the original norm
scaling_factor = original_norm / rotated_norm; % Calculate scaling factor
J_scaled = J * scaling_factor; % Scale the rotated image
% Display the scaled rotated image
subplot(1, 2, 2);
imshow(J_scaled);
title('Rotated Image with Preserved Norm');
% Display the norms for verification
disp(['Original Norm: ', num2str(original_norm)]);
disp(['Rotated Norm: ', num2str(norm(J_scaled(:)))]);

Please see attached.

The code begins by loading an example image and converting it to double precision for accurate calculations. The original image is displayed using imshow within a subplot and then it is rotated by 45 degrees using imrotate. The bilinear method is chosen for interpolation, and crop is used to maintain the original image size. The norms of both the original and rotated images are calculated using the norm function. A scaling factor is computed to adjust the rotated image's norm to match that of the original image. The rotated image is then scaled accordingly. The scaled rotated image is displayed in the second subplot, and the norms are printed to the console for verification.

This approach effectively rotates the image while ensuring that the norm remains unchanged. By scaling the rotated image, you can maintain the integrity of the original matrix's properties.

Hope this helps.

Please let me know if you have any further questions.

8 Comments

but my point is that I have matrix there is no involvement of image in beginning. I have matrix I want to rotate that matrix but the image form from from matrix is also rotated and norm should be persevered further I need that rotated matrix to perform further calculation in such a way that my final output should be same but in rotated direction. In other words I am working on invariance with respect to rotation at 45 degree or 60 degree.

Hi @Shafaq,

You are currently using imrotate to rotate your image, but this method alters the norm of the matrices, which affects further calculations. The challenge is to rotate the matrices while keeping the norm unchanged. This requires a careful approach to ensure that the transformation is applied without introducing any scaling effects. After going through your comments and analysis of your code, you have already defined the rotation matrix R. However, you need to ensure that after applying this rotation, the resulting vectors still have the same magnitude as before. After rotating, normalize the resulting vectors back to their original norms. Here is an updated version of your code:

% Given matrices
x = [0.0517, 0.0281, 0.0122, 0.0100;
   0.0505, 0.0344, 0.0100, 0.0006;
   0.0513, 0.0443, 0.0122, 0.0006;
   0.0513, 0.0443, 0.0152, 0.0034];
y = [-0.0118, -0.0012, 0.0016, -0.0025;
   -0.0268, -0.0076, -0.0026, -0.0026;
   -0.0344, -0.0337, -0.0026, -0.0025;
   -0.0352, -0.0329, -0.0214, -0.0130];
% Combine x and y into a single vector t
x2 = reshape(x, [], 1);
y2 = reshape(y, [], 1);
t = zeros(2 * length(x2), 1);
t(1:2:end) = x2;
t(2:2:end) = y2;
% Calculate initial norm
initial_norm = norm(t);
% Rotation angle in radians
angle = pi / 4; % 45 degrees
R = [cos(angle), sin(angle); 
  -sin(angle), cos(angle)];
% Rotate each point and normalize back to original norm
for i = 1:numel(x)
  ts = [x(i); y(i)];
  ts_rotated = R * ts; % Rotate
  % Normalize to maintain original norm
  ts_rotated = (ts_rotated / norm(ts_rotated)) * norm(ts); 
  x(i) = ts_rotated(1);
  y(i) = ts_rotated(2);
end
% Combine rotated x and y into new vector t1
xR = reshape(x, [], 1);
yR = reshape(y, [], 1);
t1 = zeros(2 * length(xR), 1);
t1(1:2:end) = xR;
t1(2:2:end) = yR;
% Final results
final_norm = norm(t1);
% Display results
disp(['Initial Norm: ', num2str(initial_norm)]);
disp(['Final Norm: ', num2str(final_norm)]);
figure;
imagesc(sqrt(x.^2 + y.^2));
colorbar; % Add color bar for better visualization
title('Rotated Image');

Please see attached.

In the above provided code snippet, the rotation logic has been retained but now includes a normalization step after rotation. Each rotated vector is normalized back to its original magnitude using norm(ts). The final image display uses imagesc to visualize the resultant matrix. For more information and guidance on this function, please refer to

https://www.mathworks.com/help/matlab/ref/imagesc.html

By normalizing after rotation, you ensure that calculations based on these matrices remain consistent with your initial conditions. Now, if you are dealing with larger matrices or require faster processing times in practice scenarios (e.g., image processing in real-time applications), consider vectorizing operations instead of using loops for improved performance.

This updated approach should help you achieve your goal of rotating the matrices while preserving their norms effectively!

Please let me know if you have any further questions.

Now norm is same but origionl and rotated images are same means no rotation in images. image and matrices should be in rotated form but norm should exactly same. that what i need
Thanks umar I have done with scaling now I solved this problem but my output still has issue I think there is another issue in code . Can you give me your email adress. If you dont mind
Hi @Shafaq,
Thank you for your update regarding the scaling issue. I am glad to hear that you've made progress, but I understand that you're still encountering some challenges with the output. Regarding your request for my email address, at Mathworks forms, we are not allow to share personal email, please feel free to reach out through this platform with any specific questions or details about the code issues you're facing. I would be more than happy to assist you further. Looking forward to hearing from you soon.
Pixels are discrete values. You need to expect that if you rotate by anything other than a multiple of 90 degrees, that due to interpolation, the norm of the rotated images will not end up exactly the same. And that's even excluding the effects of boundary pixels.
Shafaq
Shafaq on 29 Oct 2024
Edited: Shafaq on 29 Oct 2024
@Umar actually data is confidential I have a least square problem and after scaling of input matrices (which you mentiond (norm(rotated)/norm(origional))now norm of matrices are same with rotated direction. but output which is diviatoric tensor but that diviatoric is roated but not same. There is issue in rotation. Is there any other way of rotation which rotate the matrices and formed image from that matirces is also rotated but output of divitorice stress tensor is same

Hi @Shafaq,

I appreciate your detailed explanation of the current situation with your matrix rotations. It seems you have made some significant strides in maintaining the norms of your matrices post-rotation, which is great to hear! However, I understand that you are still facing challenges regarding the output of your deviatoric stress tensor. The deviatoric stress tensor represents the distortion or shape change in a material under stress. It is essential to ensure that any rotation applied to the original tensor does not affect its inherent characteristics beyond spatial transformation. Given your requirements, let me consider a couple of strategies: You have already defined a rotation matrix R. This matrix should be applied uniformly across all components of your tensor. If maintaining specific properties of tensors (like deviatoric nature) is critical, you might want to explore polar decomposition methods. These allow you to separate a matrix into a rotation and a symmetric positive definite matrix, which could help preserve certain characteristics while rotating.

As Walter noted, when you rotate by angles other than multiples of 90 degrees, pixel interpolation can lead to discrepancies in the output. To mitigate this, use Bilinear Interpolation or Bicubic Interpolation methods when transforming images or matrices.

https://www.mathworks.com/matlabcentral/fileexchange/5219-bilinear-interpolation

https://www.mathworks.com/matlabcentral/answers/405846-bicubic-interpolation-direct-interpolation-formula-matlab-source-code

These methods can provide smoother transitions and might yield results closer to what you expect. Make sure that your pixel grid is adequately represented during transformations; sometimes resizing before rotation can help maintain fidelity. Here is an updated conceptual approach for your code:

   % Define rotation and normalization
   for i = 1:numel(x)
       ts = [x(i); y(i)];
       ts_rotated = R * ts; % Rotate
       % Normalize back to original magnitude
       ts_rotated = (ts_rotated / norm(ts_rotated)) * norm(ts); 
       x(i) = ts_rotated(1);
       y(i) = ts_rotated(2);
   end
   % Calculate deviatoric stress tensor based on rotated       coordinates
   deviatoric_tensor = calculateDeviatoricTensor(xR, yR); 
   % Define this function based on your needs

If your goal is to achieve a visual representation that aligns closely with the theoretical expectations from your tensors, consider using techniques like Fourier Transform for image processing, which can help manipulate images in frequency space while retaining structural integrity.

https://www.mathworks.com/help/images/fourier-transform.html#

When implementing any form of transformation or interpolation, ensure that numerical stability is maintained throughout computations to avoid unintended artifacts.

Please feel free to share specific snippets or details about how you're calculating the deviatoric tensor if you would like more tailored advice on that front. I am here to help you through this!

Sign in to comment.

Categories

Asked:

on 25 Oct 2024

Commented:

on 29 Oct 2024

Community Treasure Hunt

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

Start Hunting!