0

I have created a program that takes an image and rotates it at its center by a certain degree, crops it, then fills in any empty space with the nearest pixel. It works for smaller images, but as the image gets bigger, it can take up to several minutes for it to run completely. I was wondering if there was anything I could do with my could that could make it more efficient. Here is my code:

function R = Rotate(image, degrees)
rad = (degrees/180)*pi;    
[r, c] = size(image);
x_cen = (c+1)/2;
y_cen = (r+1)/2;

%new_image same size as old 
new_image = zeros([r,c]);

%list of datapoints after rotation
new_datapoints = [r*c, 2];

%shade of datapoints
shade_data = [r*c, 1];
length_new = 1;


%rotation
for i = 1:c
for j = 1:r
shade = image(j,i);
x = i - x_cen;
y = y_cen - j;

x_bar = [x;y];
const = [cos(rad), sin(rad); -sin(rad), cos(rad)];
x_prime = const*x_bar;

fill_x = x_prime(1,1);
fill_y = x_prime(2,1);

new_x = fill_x + x_cen;
new_y = -fill_y + y_cen;

new_datapoints(length_new, :) = [new_x, new_y];
shade_data(length_new, :) = shade;
length_new = length_new + 1;

new_y = round(new_y);
new_x = round(new_x);

if(new_x <= c)&&(new_x > 0)
    if(new_y <= r)&&(new_y > 0)
        new_image(new_y, new_x) = shade;
    end   
end
end
end

unused_datapoints = [0,0];
length_un = 1;
%finds all unused data points
for i = 1:c
for j = 1:r
   if new_image(j,i) == 0
       unused_datapoints(length_un, :) = [i,j];
       length_un = length_un + 1;
   end
end
end

%fills in unused datapoints with closest pixel
for i = 1:length_un-1
x_un = unused_datapoints(i, 1);
y_un = unused_datapoints(i, 2);
dist = sqrt(r^2+c^2);

for j = 1:length_new-1
x_new = new_datapoints(j, 1);
y_new = new_datapoints(j, 2);
new_dist = sqrt((y_new-y_un)^2+(x_new-x_un)^2);

if new_dist < dist
    dist = new_dist;
    new_image(y_un, x_un) = shade_data(j, 1);
end
end
end
R = new_image;

end

Any help would be wonderful, thanks.

Lokesh A. R.
  • 2,326
  • 1
  • 24
  • 28

0 Answers0