Rotation of matrices and image formed from it

52 Ansichten (letzte 30 Tage)
Shafaq
Shafaq am 25 Okt. 2024 um 11:35
Kommentiert: Umar am 29 Okt. 2024 um 14:51
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 Kommentar
Shafaq
Shafaq am 25 Okt. 2024 um 15:33
Bearbeitet: Matt J am 25 Okt. 2024 um 16:44
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)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Umar
Umar am 25 Okt. 2024 um 12:53

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 Kommentare
Shafaq
Shafaq am 29 Okt. 2024 um 11:13
Bearbeitet: Shafaq am 29 Okt. 2024 um 14:24
@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
Umar
Umar am 29 Okt. 2024 um 14:51

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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Geometric Transformation and Image Registration finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by