我创建了一个程序,它接收一个图像并在其中心旋转一定程度,裁剪它,然后用最近的像素填充任何空白区域。它适用于较小的图像,但随着图像变大,它可能需要几分钟才能完全运行。我想知道我能做些什么可以让它更有效率。这是我的代码:
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
任何帮助都会很精彩,谢谢。