creating multiple circles inside an array
조회 수: 22 (최근 30일)
이전 댓글 표시
Hi - this is some code that I found of the matlabs wiki. I am trying to modify it to suit my use and am struggling greatly to achieve the required outcome.
Basically this creates 1 circle within an array. The circle is designated as 1s and everything outside the circle are 0s.
However, I have an unknown amount of circles (similar radi but different X and Y for centers)
What I want to do is to create multiple circles within this array. Eventually hopefully, I will return a final array that has a 1 where a circle overlaps and 0 where no circle has overlapped.
Would the following code be modifiable or would I need a different approach?
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
imageSizeX = 640;
imageSizeY = 480;
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
centerX = 320;
centerY = 240;
radius = 100;
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Now, display it.
image(circlePixels) ;
colormap([0 0 0; 1 1 1]);
title('Binary image of a circle');
댓글 수: 4
채택된 답변
Image Analyst
2020년 3월 25일
편집: Image Analyst
2020년 3월 25일
Here is a little demo you might find useful. It determines which, if any, circles overlap/intersect:
% Demo to create some circles with random radii and center locations and look for overlaps.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
% Create some circles randomly.
% Some might overlap, or possibly none overlap (since it's random).
numCircles = 7;
xy = rand(numCircles, 2); % Randomly located centers.
radii = 0.1 * rand(1, numCircles) + 0.05; % Random radii in the range 0.05 to 0.2
% Draw circles.
viscircles(xy, radii);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Label them so we know which circle is which.
% Put the circle number at the center.
for k = 1 : numCircles
text(xy(k, 1), xy(k, 2), num2str(k), 'FontWeight', 'bold');
end
grid on;
axis('equal');
% Get a list of separations center-to-center.
distances = pdist2(xy, xy)
% Determine the sum of the radii.
[r1, r2] = meshgrid(radii, radii)
sumOfRadii = r1 + r2
% If there is an overlap, the sum of the radii will be bigger than the distance between them.
overlapping = sumOfRadii > distances;
% Zero out the diagonal because we don't care that
% circle 1 overlaps circle 1, circle 2 overlaps circle 2, etc.
overlapping(logical(eye(numCircles))) = false
% Make up information message for plot title and command window:
numberOfOverlaps = sum(any(overlapping, 2));
overlappedIndexes = find(any(overlapping, 2));
if numberOfOverlaps > 0
caption = sprintf('%d circles are overlapped:', numberOfOverlaps);
for k = 1 : length(overlappedIndexes)
caption = sprintf('%s %d', caption, overlappedIndexes(k));
end
else
caption = sprintf('%d circles are overlapped', numberOfOverlaps);
end
fprintf('%s.\n', caption); % Print to command line.
title(caption, 'FontSize', fontSize); % Title over the plot.
% Report which circles overlap by determining how many rows have at least one 1 in them.
for row = 1 : numCircles
% See which other circles overlap with the kth circle.
overlappedCircleIndexes = find(overlapping(row, :));
if ~isempty(overlappedCircleIndexes)
% If any overlap with it, print them out.
for k2 = 1 : length(overlappedCircleIndexes)
fprintf('Circle %d overlaps with circle %d.\n', ...
row, overlappedCircleIndexes(k2));
end
end
end
For that random set, I see:
distances =
0 0.30561 0.64619 0.32995 0.25392 0.45259 0.70397
0.30561 0 0.54554 0.19313 0.35021 0.29865 0.39959
0.64619 0.54554 0 0.36275 0.41575 0.24748 0.68878
0.32995 0.19313 0.36275 0 0.21992 0.12872 0.49327
0.25392 0.35021 0.41575 0.21992 0 0.2886 0.70892
0.45259 0.29865 0.24748 0.12872 0.2886 0 0.4981
0.70397 0.39959 0.68878 0.49327 0.70892 0.4981 0
sumOfRadii =
0.2034 0.20737 0.16735 0.2079 0.22118 0.19434 0.23533
0.20737 0.21134 0.17132 0.21188 0.22515 0.19832 0.2393
0.16735 0.17132 0.1313 0.17186 0.18513 0.1583 0.19928
0.2079 0.21188 0.17186 0.21241 0.22569 0.19885 0.23983
0.22118 0.22515 0.18513 0.22569 0.23896 0.21213 0.25311
0.19434 0.19832 0.1583 0.19885 0.21213 0.18529 0.22627
0.23533 0.2393 0.19928 0.23983 0.25311 0.22627 0.26725
overlapping =
7×7 logical array
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 1 0 0 1 1 0
0 0 0 1 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
4 Circles overlap.
Circle 2 overlaps with circle 4.
Circle 4 overlaps with circle 2.
Circle 4 overlaps with circle 5.
Circle 4 overlaps with circle 6.
Circle 5 overlaps with circle 4.
Circle 6 overlaps with circle 4.
댓글 수: 1
Harrison Vidler
2020년 7월 31일
Would it be difficult to change this to other shapes such as hexagons or ellipses?
추가 답변 (2개)
Image Analyst
2020년 3월 25일
Search the forum for the tag "circle packing" and you'll find code. CLICK HERE
댓글 수: 0
darova
2020년 3월 25일
Try imdilate
I0 = false(1000); % create logical matrix
ind = randperm(1000^2,7); % random position to place
I0(ind) = 1; % place 'one'
I1 = imdilate(I0,circlePixels); % place circles
imshow(I1)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!