Adding binary matrices generated in while loop
Afficher commentaires plus anciens
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 commentaire
Meghana Dinesh
le 20 Nov 2015
Modifié(e) : Meghana Dinesh
le 20 Nov 2015
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.
Réponses (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
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!