2
votes

I am trying to write some code to rotate an image in MATLAB, that is, equivalent to imrotate. I use matrix multiplication to perform an inverse mapping of the new image to the input image. However, it takes a lot longer than explicitly writing out the equivalent equation. Is there a better way to perform this multiplication?

I would prefer to use matrix multiplication, because I could use the same code for other transformations by replacing the transformation matrix, RT.

im1 = imread('file.jpg');
[h, w, p] = size(im1);
theta = -pi/6;
hh = round( h*cos(theta) + w*abs(sin(theta)));
ww = round( w*cos(theta) + h*abs(sin(theta)));

R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
T = [w/2; h/2];
RT = [inv(R) T; 0 0 1];
for z = 1:p
    for x = 1:ww
        for y = 1:hh
            % Using matrix multiplication
            i = zeros(3,1);
            i = RT*[x-ww/2; y-hh/2; 1];

            %Using explicit equations
            %i(1) = ( (x-ww/2)*cos(theta)+(y-hh/2)*sin(theta)+w/2);
            %i(2) = (-(x-ww/2)*sin(theta)+(y-hh/2)*cos(theta)+h/2);

            %% Nearest Neighbour
            i = round(i);
            if i(1)>0 && i(2)>0 && i(1)<=w && i(2)<=h
                im2(y,x,z) = im1(i(2),i(1),z);
            end
        end
    end
end

%Revised code
im1 = imread('file.jpg');
[h, w, p] = size(im1);
theta = (pi)/3;
hh = round(h*abs(cos(theta)) + w*abs(sin(theta)));
ww = round(w*abs(cos(theta)) + h*abs(sin(theta)));
im2 = zeros([hh,ww,p], class(im1));

R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
T = [w/2; h/2];
RT = [inv(R) T; 0 0 1];

x=1:ww;
y=1:hh;

[X, Y] = meshgrid(x,y);
orig_pos = [X(:)' ; Y(:)' ; ones(1,numel(X))];
orig_pos_2 = [X(:)'-(ww/2) ; Y(:)'-(hh/2) ; ones(1,numel(X))];

new_pos = round(RT*orig_pos_2); % Round to nearest neighbour

% Check if new positions fall from map:
valid_pos = new_pos(1,:)>=1 & new_pos(1,:)<=w & new_pos(2,:)>=1 & new_pos(2,:)<=h;

orig_pos = orig_pos(:,valid_pos);
new_pos = new_pos(:,valid_pos);

siz = size(im1);
siz2 = size(im2);

% Expand the 2D indices to include the third dimension.
ind_orig_pos = sub2ind(siz2,orig_pos(2*ones(p,1),:),orig_pos(ones(p,1),:), (1:p)'*ones(1,length(orig_pos)));
ind_new_pos  = sub2ind(siz, new_pos(2*ones(p,1),:), new_pos(ones(p,1),:), (1:p)'*ones(1,length(new_pos)));

im2(ind_orig_pos) = im1(ind_new_pos);
imshow(im2);
1

1 Answers

1
votes

You should vectorize the for loops, it doesn't seem that hard. It'll gain you a lot. Trickiest is the position calculation and ignoring positions that fall off the image after rotation.

Solution:

x=1:ww
y=1:hh

[X, Y] = meshgrid(x,y);
orig_pos = [X(:)' ; Y(:)' ; ones(1,numel(X))];

new_pos = round(RT*orig_pos); % round to nearest neighbour

% check if new positions fall from map:
valid_pos = new_pos(1,:)>=1 & new_pos(1,:)<=w & new_pos(2,:)>=1 & new_pos(2,:)<=h;

You can do the assignment to the resulting matrix in a for loop to deal with positions to ignore, or just remove them from the source matrix and do the assignment in one blow:

orig_pos = orig_pos(:,valid_pos);
new_pos = new_pos(:,valid_pos);

siz = size(im1);
im2 = zeros(siz,class(im1));

% expand the 2d indices to include the third dimension
ind_orig_pos = sub2ind(siz,orig_pos(2*ones(siz(3),1),:),orig_pos(ones(siz(3),1),:), (1:siz(3))'*ones(1,N));
ind_new_pos  = sub2ind(siz, new_pos(2*ones(siz(3),1),:), new_pos(ones(siz(3),1),:), (1:siz(3))'*ones(1,N));

im2(ind_orig_pos) = im1(ind_new_pos);

Your code is probably also slow, because you don't initialize im2 so it is expanded in runtime as needed.

As reference: using 'peppers.png' as source image, this whole block of code took 0.12s on my pc, your code took several minutes.. end result was the same.