matlab - Separate connected objects and single objects from an image -
i working on image in matlab shown in link http://lyremjeni.files.wordpress.com/2013/08/tocleanblobs.jpg
i want separate connected/grouped cell blobs , single cell blobs image , display them in separate figures. kindly suggest how can achieve separation.
here 2 solutions use, 1st 1 using bwconncomp finds connected elements in binary image, , 2nd 1 using imfindcircles, believe or not finds circles! therefore in instances objects detect not circles 1st solution preferred.
i prefer first one, discriminates between single circles , clusters of circles, asked, whereas method using imfindcircles rather useful identifying individual circles, not clusters. here code:
1- bwconncomp
clear close clc = imread('circles.jpg'); bw = im2bw(a); %// convert binary threshold of 0.5. cc = bwconncomp(bw); %// find connected components.
this cc looks like, i.e. structure couple fields:
cc = connectivity: 8 imagesize: [256 256] numobjects: 15 pixelidxlist: {1x15 cell}
as see there 15 objects detected.
now loop throught objects , plot/draw them:
bw2 = zeros(size(bw)); %// create dummy image display purposes. imshow(bw2) hold on k = 1:cc.numobjects %// loop through each object , plot in white. can create individual figures each object. pixid = cc.pixelidxlist{k}; %// simpler understand if size(pixid,1) ==1 %// if 1 row, don't consider. continue else bw2(pixid) = 255; %figure(k) %// uncomment if want individual figures. imshow(bw2) pause(.5) %// display purposes. end end
if run code see creates 1 figure; demonstration if want each object in figure add figure
in loop noted create one.
2- imfindcircles
here principle simple, algorithm detects circles , stores radius , center coordinates. it's quite easy plot/crop them them individually. said not detect clusters of circles, thought know anyway.
here create figure each circle detected.
a = imread('circles.jpg'); [centers, radii, ~] = imfindcircles(a,[5 25],'sensitivity',.95,'objectpolarity','bright'); a_crop = cell(1,numel(radii)); k =1:numel(radii) %// since imcrop requires cropping rectangle, need fin x , y coordinates of lower-left corner, width , height of rectangle. x = centers(k,1); y = centers(k,2); r = radii(k); a_crop{k} = imcrop(a,[x-r y-r 2*r 2*r]); %// [x y width height] imshow(a_crop) %// new figure pause(0.5) end
if want see circles outlined, can use this:
imshow(a); hold on viscircles(centers, radii,'edgecolor','b'); hold off
giving following:
Comments
Post a Comment