Clear Filters
Clear Filters

I want to rotate matrix as well as image and want to show that output is invariant. Imrotate is effecting invariance so what should i do? % Generate a random 50x30 matrix A an

48 views (last 30 days)
I want to rotate matrix as well as image and want to show that output is inavriant. Imrotate is effecting invariance so what should i do?
% Generate a random 50x30 matrix A and a 50x1 vector b
A = rand(8, 8);
b = rand(8, 1);
% A=[1 2;3 4];
% b=[1 2]';
% Least squares solution for the original problem
x = (A' * A) \ (A' * b);
theta=90;
theta= deg2rad(theta);
% Rotation matrix for 90 degrees
R = [cos(theta), -sin(theta); sin(theta), cos(theta)];
% Extend the rotation matrix to apply to the whole problem
R_ext = blkdiag(kron(eye(1), R), eye(0)); % Adjust size accordingly
% Rotate the matrix and vector
A_rot = R_ext * A;
b_rot= R_ext * b;
theta= rad2deg(theta);
A_rot=imrotate(A_rot,theta,'crop');
b_rot=imrotate(b_rot,theta,'crop');
% Display the original and rotated matrices using imagesc
% Least squares solution for the rotated problem
x_rot = (A_rot' * A_rot) \ (A_rot' * b_rot);
% Display results
fprintf('Original least squares solution (first 5 elements):\n');
disp(x(1:end));
fprintf('Rotated least squares solution (first 5 elements):\n');
disp(x_rot(1:end));
% Check invariance
invariance_check = norm(x )- norm(x_rot);
fprintf('Invariance check (should be close to zero): %f\n', invariance_check);

Answers (2)

Umar
Umar on 4 Jul 2024 at 11:01

Hi Shafaq,

By directly applying the rotation matrix R_ext to the matrix A and vector b, the mathematical properties are preserved, and the invariance check should now yield results close to zero, indicating that the solution is invariant under rotation. So,applying the rotation matrix R_ext to the matrix A and vector b, we ensure that the mathematical properties are preserved, and the invariance check should now yield results close to zero.

% Extend the rotation matrix to apply to the whole problem R_ext = blkdiag(R, eye(size(A, 1) - 2));

Hope this will help achieve your goal now.

  1 Comment
Shafaq
Shafaq on 4 Jul 2024 at 11:04
Edited: Shafaq on 4 Jul 2024 at 11:05
I aslo want to rotate image which yield from matrix two types of rotation vector as well as image and then it sholud be invariant.imrotate rotate the image but it effect invariance

Sign in to comment.


Umar
Umar on 4 Jul 2024 at 11:25
Edited: Walter Roberson on 4 Jul 2024 at 19:42
Hi Shafaq,
You can use the imwarp function along with an affine2d object to perform rotation. This method allows you to specify rotation angles and achieve the desired transformation without altering the image's invariance properties. For more information on this function, please refer to
I have provided an example where you can load the image using imread('image.jpg'). Define the rotation angle theta in degrees. Create an affine transformation object tform for rotation using affine2d(). Then, apply the rotation to the image using imwarp(img, tform). Afterwards, display the rotated image using imshow(output_img).
Load the image
img = imread('image.jpg');
% Define the rotation angle
theta = 30; % Specify the rotation angle in degrees
% Create an affine transformation object for rotation
tform = affine2d([cosd(theta) sind(theta) 0; -sind(theta) cosd(theta) 0; 0 0 1]);
% Apply the rotation to the image
output_img = imwarp(img, tform);
% Display the rotated image
imshow(output_img);
Hope this will help resolve your problem.
  4 Comments
Shafaq
Shafaq on 4 Jul 2024 at 12:30
Moved: Walter Roberson on 4 Jul 2024 at 19:42
thanks umer but can you tell me how can I apply in above code this is toy roblem actually
Umar
Umar on 4 Jul 2024 at 15:02
Moved: Walter Roberson on 4 Jul 2024 at 19:43
Hi Shafaq,
Do you have access to MATLAB's Computer Vision Toolbox, because to incorporate feature extraction using Scale-Invariant Feature Transform (SIFT) or Speeded-Up Robust Features (SURF) into the code, you do need to utilize Computer Vision tool box. As an example, I will provide code as an example which includes SIFT feature extraction.
% Load an image (replace 'image.jpg' with the path to your image)
image = imread('image.jpg');
% Convert the image to grayscale
image_gray = rgb2gray(image);
% Detect SIFT features
points = detectSURFFeatures(image_gray);
% Extract feature descriptors
[features, valid_points] = extractFeatures(image_gray, points);
% Display detected SIFT features
imshow(image);
hold on;
plot(valid_points.selectStrongest(50));
% Perform further processing with the extracted features
% (e.g., matching, object recognition, etc.)
So, in the above example code, you load an image, convert it to grayscale, detect SIFT features using SURF (Speeded-Up Robust Features), extract feature descriptors, and visualize the detected features. Then, you can proceed with additional processing steps based on the extracted features.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!