I want to calculate the affine transformation matrix (rotation, translation and scaling) for the binary image

11 views (last 30 days)
I want to calculate this matrix consisting of rotation, translation and scaling of binary image, how can I calculate this matrix using Matlab in one loop
Thanks in advance

Answers (1)

Umeshraja
Umeshraja on 17 Sep 2024
Edited: Umeshraja on 21 Sep 2024
I understand that you wanted to perform an affine transformation on a binary image. You can achieve this using the 'affinetform2d' and 'imwarp' functions of MATLAB's Image Processing Toolbox..
Below is a demonstration of how you can calculate the transformation matrix that includes rotation, scaling, and translation, and then apply it to your binary image.
close all;
% Load a grayscale image
img = imread('cameraman.tif');
% Convert the image to binary using a threshold
% Here, we use a threshold of 128 for demonstration
I = img > 128;
% Define transformation parameters
Theta = 90; % angle of rotation
Scale = 1.5;
a = 2; % translation along x-axis
b = 4; % translation along y-axis
% Calculate the transformation matrix
%Rotation
Thrad = Theta * pi / 180;
R = [ cos(Thrad) -sin(Thrad) 0;
sin(Thrad) cos(Thrad) 0;
0 0 1 ];
%Scaling
S = [Scale 0 0;
0 Scale 0;
0 0 1 ];
%Transform
T = [ 1 0 a;
0 1 b;
0 0 1 ];
% Combine the transformations
RST = R * S * T;
% Create affine transformation object
t_aff = affinetform2d(RST);
% Apply transformation to the binary image
binaryImage_affine = imwarp(I, t_aff);
figure;
imshow(I)
title("Original")
% Display the transformed binary image
figure;
imshow(binaryImage_affine);
title("Transformed Binary Image");
For more information, refer to the following documentation:
I hope it helps!

Categories

Find more on Read, Write, and Modify Image 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!