Generating random circles/ellipses inside circular domain

3 vues (au cours des 30 derniers jours)
Shubham
Shubham le 12 Déc 2022
Commenté : Torsten le 13 Déc 2022
I am using this code for microstructure generation. But the issue is that this code generates circle/ellipses inside a square domain.
In my case the domain is circular in shape. I tried many times but I am not able to accurately change that square domain into a circular domain.
I have attached a sample microstructure.

Réponses (3)

Torsten
Torsten le 12 Déc 2022
Modifié(e) : Torsten le 12 Déc 2022
Maybe you could generate the circles/ellipses in a square domain with side length R (the radius of your circular shape) and finally cut away those that don't belong to your shape.
  5 commentaires
Image Analyst
Image Analyst le 13 Déc 2022
@Bruno Luong to be fair, he did not ask us to make changes in the code, though maybe that was the implication or you inferred that. He said it would be great if he, himself, could make the changes (not us) so that's why I referred him to the training web site in my comment.
@Shubham, Bruno is right. If you don't want to learn MATLAB, then you should ask the original author. In general we don't, in this forum, help you modify or debug File Exchange submissions. But the best solution is you become skilled enough yourself so that you can do things yourself.
Torsten
Torsten le 13 Déc 2022
It is possible but this will affect the area ratio.
Can you elaborate ?

Connectez-vous pour commenter.


Askic V
Askic V le 12 Déc 2022
Since you provided an example in which all small circles have the same radius, perhaps the following code will suit your needs:
clear
clc
close all
% Main circle
R = 5; % Radius
Xc = 2; % x center coordinate
Yc = 2; % y center coordinate
t = linspace(0,2*pi);
tx = cos(t);
ty = sin(t);
x = Xc + R*tx;
y = Yc + R*ty;
% Plot main circle
plot(x,y, 'LineWidth',2);
axis equal;
grid on;
hold on;
% Random small circles range
rangex = [Xc-R; Xc+R];
rangey = [Yc-R, Yc+R];
% Number of circles
N = 100;
r = 0.2*ones(N,1);
xc = (rangex(2)-rangex(1))*rand(N,1)+rangex(1);
yc = (rangey(2)-rangey(1))*rand(N,1)+rangey(1);
points = [xc, yc, r];
Center = [Xc,Yc];
is_in_circle = sum((Center-points(:,1:2)).^2,2) < R^2;
% Add vector is_incircle to existing points in order to filter it
new_points = [points is_in_circle];
% Remove points that are outside the circle
new_points(new_points(:,4)==0, :) = [];
% %
% Check if circles are intersecting
for ii = 1:size(new_points,1)
xout = circcirc(Xc,Yc,R,new_points(ii,1),new_points(ii,2),...
new_points(ii,3));
if ~isnan(xout)
new_points(ii,4) = 0;
end
end
% Remove circles that are outside the main circle
new_points(new_points(:,4)==0, :) = [];
% Now check and remove overlapping
% Remove overlapping circles
for ii = 1: size(new_points,1)
for jj = 1:size(new_points,1)
if ii ~= jj
xout = circcirc(new_points(ii,1),new_points(ii,2),new_points(ii,3),...
new_points(jj,1),new_points(jj,2),new_points(jj,3));
if ~isnan(xout)
new_points(ii,4) = 0;
end
end
end
end
% Remove from the matrix
new_points(new_points(:,4)==0, :) = [];
% Now plot small circles
for ii = 1: size(new_points,1)
x = new_points(ii,3)*tx+new_points(ii,1);
y = new_points(ii,3)*ty+new_points(ii,2);
patch(x,y,'r');
end
  2 commentaires
Shubham
Shubham le 13 Déc 2022
Thanks for the help. But this is not what I am looking for. If I could make changes in the same code that would be great.
Image Analyst
Image Analyst le 13 Déc 2022
That would be great wouldn't it? Here's how you can do it: invest 2 hours of your time here:

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 12 Déc 2022
% M-file to place multiple points inside a big circle.
% Clean up
close all;
clc;
fontSize = 15;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Initialize some parameters.
numberOfPoints = 25; % Number of small circles
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
bigCircleRadius = 250; % big circle radius
% Initialize an image to hold one single big circle.
bigCircleImage = zeros(bigImageHeight, bigImageWidth, 'uint8');
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <= bigCircleRadius.^2) = 1;
clear('x', 'y'); % Release these variables, they're not needed anymore.
% Display it in the upper left plot.
subplot(2,2, 1);
imshow(bigCircleImage, []);
title('Big Circle Mask', 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Initialize an output image to hold many small overlapping circles.
pointsImage = zeros(bigImageHeight, bigImageWidth, 'uint8');
% Get linear indexes of 500 randomly located in the rectangle.
numberOfPointsToPlace = 5000;
linearIndexes = randi(numel(pointsImage), numberOfPointsToPlace, 1);
% Set those points in the image
pointsImage(linearIndexes) = 255;
% Get locations in terms of row and columns:
[rows, columns] = ind2sub(size(pointsImage), linearIndexes);
% Display it in the lower left plot.
subplot(2,2, 2);
imshow(pointsImage);
title('Many Points', 'FontSize', fontSize);
% Multiply the big circle mask by the points image to clip
% those points that lie outside the big circle.
maskedByBigCircle = bigCircleImage .* pointsImage;
% Display it in the lower right plot.
subplot(2,2, 3);
imshow(maskedByBigCircle);
title('Many Points Masked by Big Circle', 'FontSize', fontSize);
  2 commentaires
Shubham
Shubham le 13 Déc 2022
Thanks for the help. But this is not what I am looking for.
Image Analyst
Image Analyst le 13 Déc 2022
OK, good luck though.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by