matlab初学者在这里,非常感谢你。我在使用现有程序时遇到了问题:
该项目的目的是在背景上构建(6)层随机大小和方向的椭圆,然后将每个层状态保存为单独的图像(即只有背景的 image1,带有 bg 和 1 椭圆的 image3,带有 bg 和 2 的 image3椭圆等...)。到目前为止,该程序可以工作,但有时如果后面的对象之一大于前面的对象,则某些椭圆会隐藏在最终图片中(有 6 个椭圆)。我认为一个可能的解决方案是将椭圆值保存在数组中,但不将它们导出为图像。全部保存后,计算每个区域的面积,然后使用排序函数将它们按降序排列,然后绘制和导出它们。
这是代码:
xdim=1280; ydim=1024; % screen dimensions - these should match the touch screen dimensions
scene=zeros(xdim,ydim,3);
for scene_counter=1:1 %amount of scenes generated
scene_counter
figure;
ell_size_min=100; ell_size_max=800; % range of axis lengths for ellipses/pacman
min_color_separation=0.15; % this could theoretically take values between 0 and sqrt(3)=1.73ish
% if you increase this, it will take the program longer to find an acceptable set of colors
% the probability that a set of colors fails the separation test & it has to get a new one is
% about 10% for 0.1; about 50% for 0.15 and about 70% for 0.2
%-------------------------------------------
scene=scene.*0;
p = randperm(4); % creates random permutation of (1-4)
bg_count=p(1)+2; % selects first integer in sequence, e.g. random 1-4
% number of bg elements, min 3, max 6.
%-------------------------------------------
% this bit spaces out the colors of background and foreground elements
% it checks that ALL combinations have a min separation in color space using Pythagoras' theorum
% here color space is defined as 0 < R,G,B < 1
% it picks 11 spaced colors for each scene -
% 7 possible ellipses/pacmen + 3 letters (s+,s-,big letter) + background 'field' ie the very background
color_check_flag=1;
while color_check_flag==1
colors=rand(11, 3);
color_check=...
[colors-repmat(colors(1,:),11,1);...
colors-repmat(colors(2,:),11,1);...
colors-repmat(colors(3,:),11,1);...
colors-repmat(colors(4,:),11,1);...
colors-repmat(colors(5,:),11,1);...
colors-repmat(colors(6,:),11,1);...
colors-repmat(colors(7,:),11,1);...
colors-repmat(colors(8,:),11,1);...
colors-repmat(colors(9,:),11,1);...
colors-repmat(colors(10,:),11,1);...
colors-repmat(colors(11,:),11,1) ];
color_check_flag=sum(sqrt(sum((color_check+(color_check==0)).^2,2))<min_color_separation)>0;
end;
%-------------------------------------------
% this bit makes the background of the scene
% first, it makes a matrix the size of the screen and lets the elements be 1 if they fall in the ellipse or zero otherwise
% then, it takes out a bite if the bg element should be a pacman (random, p=0.5)
% then, it converts the matrix defining the shape into a 3d matrix, the size of the screen x 3 for RGB intensities
% that matrix defines this bg element, or 'layer' fully
% it combines layers in another screen size x 3 matrix -
% to do this is blanks (makes then =0) any elements in the scene which should be overwriten by the new bg element
% and then adds the new bg element to the scene
im = zeros(xdim,ydim);
[x,y] = ind2sub(size(im),1:numel(im));
bg=reshape([ones(xdim,ydim).*rand,ones(xdim,ydim).*rand,ones(xdim,ydim).*rand,],xdim,ydim,3);
imwrite(permute(bg,[2 1 3]),['scene_' int2str(scene_counter) '_' int2str(1) '.jpg'], 'jpg');
for i=1:6
a=rand*(ell_size_max-ell_size_min)+ell_size_min; % a is one axis of ellipse
b=rand*(ell_size_max-ell_size_min)+ell_size_min; % b is the other axis
centre = [rand*xdim rand*ydim]; % background elements can be anywhere on screen
ellipse = (x-centre(1)).^2 / a^2 + (y-centre(2)).^2/b^2 < 1; % define which pixels fall in the ellipse
% this bit makes the ellipse into pacman
if rand<0.5
bite_start=(rand*2*pi)-pi;
bite_stop=(rand*2*pi)-pi;
[theta,rho] = cart2pol(y-centre(2),x-centre(1)); % generate polar coords for doing the pacman mouth
if bite_stop>bite_start
pacman=(bite_start<theta).*(theta<bite_stop);
else
pacman=(theta>bite_start)+(bite_stop>theta);
end
ellipse=(reshape(ellipse.*pacman,xdim,ydim));
end
layer=reshape([ellipse.*colors(i,1) ellipse.*colors(i,2) ellipse.*colors(i,3)],xdim, ydim,3); % make a colored image for this bg element
scene=(scene.*(layer==0))+layer; % add the bg element to the scene by blanking the scene where this ellipse/pacman should go, then adding the ellipse/pacman to the scene
scene_temp=scene+((scene==0).*bg); % color in the remaining bits of screen (background 'field')
% following bit creates jpegs for levels 1-5 of oneplace as the bg
% elements are added to the image. i = number of bg elements
% format 'scene_number_level'
if i == 1
imwrite(permute(scene_temp,[2 1 3]),['scene_' int2str(scene_counter) '_' int2str(3) '.jpg'], 'jpg');end;
if i == 2
imwrite(permute(scene_temp,[2 1 3]),['scene_' int2str(scene_counter) '_' int2str(4) '.jpg'], 'jpg');end;
if i == bg_count
imwrite(permute(scene_temp,[2 1 3]),['scene_' int2str(scene_counter) '_' int2str(5) '.jpg'], 'jpg');end
end
image(scene);
%-------------------------------------------
% this bit defines coordinates for the s+, s- and big background letter, which get drawn in Presentation
spacing_flag=1;
while spacing_flag>0
letter_centres = [ round(rand*(xdim-300))+150-(0.5*xdim) round(rand*(ydim-300))+150-(0.5*ydim); % big background letter can go anywhere more than 150 pixels from the edge
round(round((rand*15)+0.5).*(xdim./16)-0.5*xdim)
round(round((rand*11)+0.5).*(ydim./12)-0.5*ydim);
round(round((rand*15)+0.5).*(xdim./16)-0.5*xdim) round(round((rand*11)+0.5).*(ydim./12)-0.5*ydim)]; % using 11x15 grid of possible target & distractor positions to match old program
spacing_flag=sqrt(sum((letter_centres(2,:)-letter_centres(3,:)).^2))<(round(xdim*(427/800))); % min spacing is from old scenes, as determined by Phil
end
%-------------------------------------------
% define characters and fonts
letter_indices=[round((rand*62)+0.5) round((rand*62)+0.5) round((rand*62)+0.5)];
font_indices= [round((rand*10)+0.5) round((rand*10)+0.5) round((rand*10)+0.5)];
%-------------------------------------------
scene_info(scene_counter,:)=[scene_counter bg_count letter_indices font_indices...
letter_centres(1,:) letter_centres(2,:) letter_centres(3,:)...
round(colors(9,:).*255) round(colors(10,:).*255) round(colors(11,:).*255)];
end
dlmwrite('scene_info.txt',scene_info);
如果我能澄清任何代码,请告诉我,再次,我非常感谢任何帮助!