Rotation of matrices and image formed from it
1 Comment
Answers (1)
0 votes
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
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.
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
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!
Categories
Find more on Computer Vision Toolbox 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!
