8
\$\begingroup\$

Currently, I have this code snippet, but it's a little bit slow:

% input image
input_matrix = imread('placa.bmp');
[rows cols] = size(input_matrix);
% rotation
degree = 30;
radians = (pi * degree) / 180; 
theta = radians;
% output matrix
t_matrix = input_matrix;
t_matrix(t_matrix == input_matrix) = NaN;
% transformation matrix
T = [cos(theta) -sin(theta) 0; sin(theta) cos(theta) 1; 0 0 1];
% loop over each input_matrix coordinate
for n = 1:numel(input_matrix)
 % current coordinate
 [x y] = ind2sub([rows cols], n);
 % transpose
 v = [x y 1]';
 % homogeneous coordinate
 v = T*v;
 % transponse again
 v = v';
 % only integer values
 a = floor(v(1));
 b = floor(v(2));
 if a > 0 && b > 0
 % replace in t_matrix
 t_matrix(a,b) = input_matrix(x,y);
 end
end
% get only a part of t_matrix
t_matrix = t_matrix(1:rows, 1:cols);
figure; imshow(input_matrix); % original image
figure; imshow(t_matrix)

How can I improve it?

Condition: keep using this matrix:

% transformation matrix
T = [cos(theta) -sin(theta) 0; sin(theta) cos(theta) 1; 0 0 1];
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Sep 23, 2012 at 0:46
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Rather than looping over all x and y values you could put them into a vector, that should speed things up considerably.

y=repmat(1:cols,rows,1)
x=repmat((1:rows)',cols,1)
M=[x(:) y(:) ones(rows*cols,1)];

Now you just need to multiply M or M' with T or T' and you have subsitituted that part of the code.

answered Nov 14, 2012 at 10:28
\$\endgroup\$
2
  • \$\begingroup\$ It would help if you posted an example along with an explanation instead. \$\endgroup\$ Commented Nov 14, 2012 at 11:01
  • \$\begingroup\$ Added the improved code part \$\endgroup\$ Commented Nov 14, 2012 at 12:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.