I made this binary image such that a bigger circle has non overlapping random smaller circles. How can I remove those circles those are not complete.

댓글 수: 5

KSSV
KSSV 2023년 2월 21일
Show us your code which generates this image.
I cannot provide the code, but I have referred this open source code. It generates random non overlapping circles and stores the centre coordinates and radius of the small circles.
This code will generate circles in square domain after this you can run this. This will result in the image as uploaded. Now I need to remove those half circles and their stored data
% Initialize an image to hold one single big circle.
fontSize = 20;
bigCircleRadius = image_size/2;
bigCircleImage = zeros(image_size, image_size, 'double');
[x, y] = meshgrid(1:image_size, 1:image_size);
bigCircleImage((x - image_size/2).^2 + (y - image_size/2).^2 <= bigCircleRadius.^2) = 1;
clear('x', 'y'); % Release these variables, they're not needed anymore.
% Display it in the upper left plot.
figure(2)
imshow(bigCircleImage, []);
title('Big Circle Mask', 'FontSize', fontSize);
maskedByBigCircle = bigCircleImage .* Image;
figure(3)
imshow(maskedByBigCircle);
title('Many Points Masked by Big Circle', 'FontSize', fontSize);
Jan
Jan 2023년 2월 21일
편집: Jan 2023년 2월 21일
A cheaper version of:
bigCircleImage = zeros(image_size, image_size, 'double');
[x, y] = meshgrid(1:image_size, 1:image_size);
bigCircleImage((x - image_size/2).^2 + (y - image_size/2).^2 <= bigCircleRadius.^2) = 1;
is
v = ((1:image_size) - image_size / 2).^2;
bigCircleImage = double(v + v.' <= bigCircleRadius.^2);
It would be easy to omit the small circles during the calculation of their centers: Simply keep only circles, whos center is inside the circle with the radius bigCircleRadius - smallCircleRadius.
Creating the image at first an removing the cropped circles afterwards is far too indirect. So insert the rejection inside the code for placing the small circles. It would be inefficient, if the readers rewrite this code just to insert one additional IF condition.
Shubham
Shubham 2023년 2월 21일
yes I can give this condition at the begining when it is picking random co-ordinates.
Like, if the chosen co-ordinates are at a distance greater than small circle radius from the big circle pheriphery, then that is a valid co-ordinate but how to compare this using code
Jan
Jan 2023년 2월 21일
"how to compare this using code" - You must have some code, which prevent overlapping between the small circles already. The code to include only small circles inside a radius minus the radius of the small circle is trivial.
It would include something like: vecnorm(c - C) < bigR - smallR. As soon as you show your code, it would be very easy to insert this condition. But for posting a working answer, we have to guess, what you code is at first and rewrite it. This is not an efficient way to search for a solution.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2023년 2월 21일
편집: Jan 2023년 2월 21일

0 개 추천

w = 300; % Image size
bigR = 140;
smallR = 15;
wantN = 40; % Number of small circles
N = 0;
center = zeros(wantN, 2); % List of centers
iter = 0;
while N < wantN
c = rand(1, 2) * w;
collide = any(vecnorm(c - center, 2, 2) < 2 * smallR) | ...
vecnorm(c - w/2, 2, 2) >= bigR - smallR; % <- This is the needed condition
if ~collide
N = N + 1;
center(N, :) = c;
end
iter = iter + 1; % Better crash than run infinitely
if iter > 1e6
error('Cannot find a valid solution');
end
end
img = ones(w, w, 3);
img = drawCircle(img, [w/2, w/2], bigR, [1,0,0]);
img = drawCircle(img, center, smallR, [0,1,0]);
image(img);
function img = drawCircle(img, C, R, Color)
s = size(img);
mask = (((1:s(1)).' - reshape(C(:, 1), 1, 1, [])).^2 + ...
((1:s(2)) - reshape(C(:, 2), 1, 1, [])).^2) <= R^2;
mask = any(mask, 3);
img = reshape(img, [], 3);
img(mask, 1) = Color(1);
img(mask, 2) = Color(2);
img(mask, 3) = Color(3);
img = reshape(img, s);
end
This shutgun technique is fragile: It will run into an infinite loop if wantN is too high.

댓글 수: 5

Shubham
Shubham 2023년 2월 22일
This code is perfectly fine. If I have to implement it for more general shape like ellipse(instead of small circles) how to proceed, as in this case orientation will also matter.
Jan
Jan 2023년 2월 22일
Do the ellipses have different orientation? The determination of the overlapping is more difficult for ellipses than for circles. It is useful to mention such details directly in the question. An iterative addition of more details let the answering persons post code, which is not useful finally.
Shubham
Shubham 2023년 2월 22일
yes orientation is also random.
Jan
Jan 2023년 2월 22일
Do you have some example code, which creates random ellipses?
Shubham
Shubham 2023년 2월 23일
편집: Shubham 2023년 2월 24일
Reference code is the one given in original post. We can have the overlapping criteria as vecnorm(c - center, 2, 2) < 2*major axis and at the boundary as vecnorm(c - w/2, 2, 2) >= bigR - major_axis.
But I dont know how to plot these ellipses.
In the code they have used a,b and orientation(using rand) to plot random ellipses.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2023년 2월 22일

1 개 추천

What I would have done is to get a mask of the big circle using bwconvhull, then call imclearborder
mask = imread('circles.png') > 128; % Get original binary image from a file.
subplot(1,2,1);
imshow(mask);
% Get mask
mask = ~bwconvhull(mask) | mask;
% Remove blobs at border
mask = imclearborder(mask);
subplot(1,2,2);
imshow(mask);
Note: some blobs are touching to form a dumbbell-shaped blob, and are not circular. If those touch the border, the whole irregular shape will get removed.

댓글 수: 1

Matt J
Matt J 2023년 2월 22일
편집: Matt J 2023년 2월 22일
Note: some blobs are touching to form a dumbbell-shaped blob, and are not circular. If those touch the border, the whole irregular shape will get removed.
Here's a refinement that can fix some of that.
mask = imread('circles.png') > 128; % Get original binary image from a file.
subplot(1,2,1);
imshow(mask);
% Get mask
circ=circMask(size(mask));
se=strel('disk',4);
mask=imerode(mask,se);
circ=imdilate(~circ,se);
mask = circ|mask;
% Remove blobs at border
mask = imdilate( imclearborder(mask),se);
subplot(1,2,2);
imshow(mask);
function c=circMask(sz)
[m,n]=deal(sz(1),sz(2));
R=min(sz)/2;
[x,y]=deal((1:m)',1:n);
c=(x-mean(x)).^2+(y-mean(y)).^2<=R^2;
end

댓글을 달려면 로그인하십시오.

Matt J
Matt J 2023년 2월 21일
편집: Matt J 2023년 2월 21일

0 개 추천

load BWimage
BW0=BW;
BW=imerode(BW, strel('disk',3));
BW=bwpropfilt(BW,'Eccentricity',[0,0.3]);
BW=imdilate(BW, strel('disk',3));
immontage({BW0,BW},'Bord',[5,5],'Back','w')

카테고리

도움말 센터File Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

태그

질문:

2023년 2월 21일

편집:

2023년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by