Adding binary matrices generated in while loop

I am writing a program that, in a nutshell, detects multiple circular objects in an image and then classifies them based on their color. To identify the circles, I use:
[centers1, radii1] = imfindcircles(imgd,[8 20],'ObjectPolarity','dark','Sensitivity',0.92);
I then use a while loop to define each circle and create a binary mask that can be used to identify where each circle is. I do this by referencing the matrices that hold the radii and center values identified in the previous function.
[rr, cc] = meshgrid(1:600);
LoopCounter = 1;
while LoopCounter <= RadiusSize(1)
% Defines circles using centers and radii, generates circle template
maska = +(sqrt((rr-centers1(LoopCounter,1)).^2+(cc-centers1(LoopCounter,2)).^2) <= radii1(LoopCounter,1));
%
LoopCounter = LoopCounter + 1;
end
The while loop generates a series of binary matrices ("templates") for each circle. I want to be able to add these matrices together to create one master "template", which I can then multiply by the original image to eliminate the background around the identified circles.
Suggestions?

댓글 수: 1

Meghana Dinesh
Meghana Dinesh 2015년 11월 20일
편집: Meghana Dinesh 2015년 11월 20일
Using a while loop isn't a very efficient way. Could you share an example image? Do the circles overlap? If the circle circumference co-ordinates are known, You can just fill (with white) the circles using:
I2 = imfill(I,'holes')
Background will be black.

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

답변 (1개)

Thorsten
Thorsten 2015년 11월 20일
편집: Thorsten 2015년 11월 20일

1 개 추천

Just add the masks. And use a for loop instead of a while:
mask = zeros(size(rr));
for i = 1:numel(radii1)
maska = maska + sqrt((rr-centers1(i,1)).^2+(cc-centers1(i,2)).^2) <= radii1(i,1);
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Ana
2015년 11월 20일

편집:

2015년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by