How can I uniformly disperse circles in an array?
조회 수: 5 (최근 30일)
이전 댓글 표시
I am trying to generate an BW image that includes uniformly dispersed circles. The number of circles is known (441), dimater of circles (7pixel) and the frame is also known (800x1000 pixel^2), but ofcourse I should be able to change each of them. I tried to locate the spheres centers with linspace on a vector and then reshape vector into an array; however, I got diagonal placement. If you can help me to get BW image similar to the attached image, it is greatly appreciated.
댓글 수: 0
채택된 답변
DGM
2022년 2월 10일
편집: DGM
2022년 2월 12일
This does not require any Computer Vision Toolbox functions and outputs a raster image.
d = 7; % dot diameter
s = [800 1000]; % image size [y x]
t = [21 21]; % tiling [y x]
m = s./(t+1)/2; % margin [y x]
% create circular strel
szf = ceil((d-1)/2)*2+1; % odd ceil()
[xx yy] = meshgrid(1:szf);
fk = (((xx-ceil(szf/2))/(szf/2)).^2 + ((yy-ceil(szf/2))/(szf/2)).^2) < 1;
% you could use strel, but it's a pita since it only accepts integer radii
%fk = strel('disk',floor(d/2),0);
A = false(s);
y = round(linspace(1+m(1),s(1)-m(1),t(1)));
x = round(linspace(1+m(2),s(2)-m(2),t(2)));
A(y,x) = true; % the dots are at the intersection of the subscript vectors
B = ~imdilate(A,fk); % dilate
% add a border around the image so it shows up against the white website bg
% this is just for display purposes; omit if undesired
B = padarray(B,[3 3],0,'both');
imshow(B)
댓글 수: 0
추가 답변 (2개)
yanqi liu
2022년 2월 10일
I = uint8(255*ones(800, 1000));
sz = size(I);
th = 7;
[x, y] = meshgrid(linspace(1,sz(2),th), linspace(1,sz(1),th));
x = x(2:end-1,2:end-1);
y = y(2:end-1,2:end-1);
position = [x(:) y(:) 25*ones(length(x(:)),1)];
I2 = insertShape(I,'FilledCircle',position,'Color','black');
I2 = im2bw(I2);
figure; imshow(I2);
댓글 수: 0
David Hill
2022년 2월 9일
편집: David Hill
2022년 2월 9일
[x,y]=meshgrid(1:21);
scatter(x,y,5,'o','MarkerFaceColor','k','MarkerEdgeColor','k');%change marker size for different size circles
ax = gca;
ax.XLim = [0 22];
ax.YLim = [0 22];
ax.Box='on';
ax.XTick=[];
ax.YTick=[];
ax.XTickLabel='';
ax.YTickLabel='';
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!