I am plotting random circles using the plot function on a white background, how to convert this plot to a binary matrix?

조회 수: 1 (최근 30일)
So I am trying to simulate some random circles on an image.
I am creating a white background of the appropriate resolution by:
%Create blank white image
xres = 2304;
yres = 3456;
img = ones(xres,yres);
%plot
figure, imshow(img);
hold on
Then generating a bunch of random circles using a custom function by:
%% Generate random bubble, based on above distribution
randomDiam = lognrnd(mu,sigma,nBubbles);
randomX = yres*rand(nBubbles,1);
randomY = xres*rand(nBubbles,1);
for ii = 1:nBubbles
generateCircle(randomX(ii),randomY(ii),randomDiam(ii));
end
My generateCircle function simply uses plot to plot the cricles:
function generateCircle(x,y,d)
%Resolution of circle set by 0.01 right now
theta = 0:0.01:2*pi;
%convert to circle plot
xp = (d/2)*cos(theta);
yp = (d/2)*sin(theta);
%plot
plot(x+xp,y+yp,'k');
end
This works great and creates the following:
untitled.jpg
However, I need to do further analysis on this. How do I convert this to an actual logical matrix?
The way I'm doing it now, the circles don't actually register in my img matrix.
Any ideas??
  댓글 수: 5
Image Analyst
Image Analyst 2019년 2월 2일
You can try my shape recognition code where I put up random shapes. Adapt the attached code as needed.

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

채택된 답변

Mark Lepage
Mark Lepage 2019년 2월 2일
Hey everyone,
I got around this by using
imageOut = insertShape(img,'FilledCircle',M,'Color','black','Opacity',1);
where img is the white background I created, and M is an [x,y,r] matrix for the circles position and radii.
Thanks

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 2월 2일
If you want solid circles, you can use poly2mask() to burn each circle into a binary image. Then OR that binary image from that one circle into your "master" image that contains ALL the circles.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by