【发布时间】:2017-03-26 16:43:10
【问题描述】:
完成这些循环大约需要 2 分钟,有没有人知道除了更改颜色数量或使用的单元格数量之外,我还能如何加快速度/更快地创建图像?矢量化有帮助吗?如果有,我应该怎么做?基本上它会在每个循环中创建一个动画帧
见下面的代码
clear all,clf reset,tic,clc,clf
rgb1 = jet(256);
len1 = size(rgb1,1);
RGB1 = permute(rgb1,[3 1 2]);
figure; imshow(RGB1); %bar of colors
len2 = 200;
num2use=numel(num2str(length(rgb1)))+1 %count how many values and add 1. can use R also
lead_zeros=strcat('%0',num2str(num2use),'d'); %creates variable needed
for ii=0:length(rgb1)
ii
rgb1_shift=circshift(rgb1,[ii,:]);
t = linspace(0,4*pi,len2);
x = t.*cos(t);
y = t.*sin(t);
%rgb2 = interp1(1:len1,rgb1,linspace(1,len1,len2));
rgb2 = interp1(1:len1,rgb1_shift,linspace(1,len1,len2));
[xg,yg] = meshgrid([-t(end:-1:2) t],[-t(end:-1:2) t]);
RGB2 = zeros([size(xg) 3]);
% interpolate for the desired coordinates
for c = 1:3
RGB2(:,:,c) = griddata(x,y,rgb2(:,c),xg,yg);
end
imwrite(RGB2,strcat('/tmp/img2/',sprintf(lead_zeros, ii),'_spiral','.png')); %will create file without borders and use any resize in repmat
end
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);
Ps:我使用的是 Octave 4.0,类似于 matlab
【问题讨论】:
-
您可以将
t = linspace(0,4*pi,len2);和[xg,yg] = meshgrid([-t(end:-1:2) t],[-t(end:-1:2) t]);等常量移出循环。在没有imwrite的情况下测量时间,因为它是 I/O 密集型的。对于griddata方法,Matlab 给出了提示:“考虑将griddata替换为scatteredinterpolant以获得更好的性能”。 (八度音阶缺失)。 -
使用 Octave 分析器查看瓶颈在哪里。我怀疑这是 for 循环...
标签: matlab for-loop octave vectorization