Sunday 15 July 2012

Separate two overlapping circles in an image using MATLAB -



Separate two overlapping circles in an image using MATLAB -

how separate 2 connected circles in image below, using matlab? have tried using imerode, not give results. eroding not work, because in order erode plenty separate circles, lines disappear or become mangled. in other starting pictures, circle , line overlap, isolating overlapping objects won't work either.

the image shows objects identified bwboundaries, each object painted different color. can see, 2 lite bluish circles joined, , want disjoin them, producing 2 separate circles. thanks

i recommend utilize circular hough transform through imfindcircles. however, need version 8 of image processing toolbox, available version r2012a , onwards. if don't have this, unfortunately won't work :(... let's go assumption have it. however, if using older r2012a, dev-il in his/her comment above linked code on matlab's file exchange on implementation of this, created before circular hough transform available: http://www.mathworks.com/matlabcentral/fileexchange/9168-detect-circles-with-various-radii-in-grayscale-image-via-hough-transform/

this special case of hough transform trying find circles in image rather lines. beauty able find circles when circle partially completed or overlapping.

i'm going take image provided above , post-processing on it. i'm going convert image binary, , remove border, white , contains title. i'm going fill in holes result of objects filled in solid white. there residual quantization noise after step, i'm going little opening 3 x 3 square element. after, i'm going close shapes 3 x 3 square element, see there noticeable gaps in shapes. therefore:

therefore, straight reading in image you've posted it:

im = imread('http://s29.postimg.org/spkab8oef/image.jpg'); %// read in image im_gray = im2double(rgb2gray(im)); %// convert grayscale, [0,1] out = imclearborder(im_gray > 0.6); %// threshold using 0.6, clear border out = imfill(out, 'holes'); %// fill in holes out = imopen(out, strel('square', 3)); out = imclose(out, strel('square', 3));

this image get:

now, apply circular hough transform. general syntax is:

[centres, radii, metric] = imfindcircles(img, [start_radius, end_radius]);

img binary image contains shapes, start_radius , end_radius smallest , largest radius of circles want find. circular hough transform performed such find circles within range (in pixels). outputs are:

centres: returns (x,y) positions of centres of each circle detected radii: radius of each circle metric: measure of purity of circle. higher values mean shape more probable circle , vice-versa.

i searched circles having radius between 30 , 60 pixels. therefore:

[centres, radii, metric] = imfindcircles(out, [30, 60]);

we can demonstrate detected circles, radii combination of plot , viscircles. therefore:

imshow(out); hold on; plot(centres(:,1), centres(:,2), 'r*'); %// plot centres viscircles(centres, radii, 'edgecolor', 'b'); %// plot circles - create border bluish

here's result:

as can see, overlapping circles towards top, circular hough transform able observe 2 distinct circles in shape.

edit - nov 16th, 2014

you wish ensure objects separated before bwboundaries. bit tricky do. way can see if don't utilize bwboundaries @ , yourself. i'm assuming you'll want analyze each shape's properties after of this, suggest iterate through every circle have, place each circle on new blank image, regionprops phone call on shape, append separate array. can maintain track of of circles having separate array adds circles 1 @ time array.

once you've finished of circles, you'll have construction array contains of measured properties of measured circles have found. utilize array contains circles above, utilize these , remove them original image lines. you'd phone call 1 more regionprops on image info lines , append final construction array.

here's first part of procedure outlined above:

num_circles = numel(radii); %// number of circles struct_reg = []; %// save shape analysis per circle / line here %// creating our circle in temporary image [x,y] = meshgrid(1:size(out,2), 1:size(out,1)); %// storing of our circles in image circles_img = false(size(out)); idx = 1 : num_circles %// each circle have... %// place our circle within temporary image r = radii(idx); cx = centres(idx,1); cy = centres(idx,2); tmp = (x - cx).^2 + (y - cy).^2 <= r^2; % // save in master circle image circles_img(tmp) = true; %// regionprops on image , save struct_reg = [struct_reg; regionprops(tmp)]; end

the above code may bit hard swallow, let's go through slowly. first figure out how many circles have, looking @ how many radii have detected. maintain separate array called struct_reg append regionprops struct each circle , line have in our image. utilize meshgrid determine (x,y) co-ordinates respect image containing our shapes can draw 1 circle onto blank image @ each iteration. this, need find euclidean distance respect centre of each circle, , set pixels true if location has distance less r. after doing operation, have created 1 circle , filtered of them out. utilize regionprops on circle, add together our circles_img array, contain circles, go on rest of circles.

at point, have saved of our circles. circles_img looks far:

you'll notice circles drawn clean, actual circles in original image bit jagged. if tried remove circles clean image, residual pixels along border , won't remove circles themselves. illustrate mean, image looks if tried remove circles circles_img itself:

... not good, right?

if want remove circles, morphological reconstruction through imreconstruct can utilize image seed image, , specify original image we're working on. job of morphological reconstruction flood fill. specify seed pixels, , image want work on, , job of imreconstruct these seeds, flood fill white until reach boundaries of objects seed pixels resided in. therefore:

out_circles = imreconstruct(circles_img, out);

therefore, our final reconstructed circles image:

great! now, utilize , remove circles original image. 1 time this, run regionprops 1 time again on final image , append struct_reg variable. obviously, save re-create of original image before doing this:

out_copy = out; out_copy(out_circles) = false; struct_reg = [struct_reg; regionprops(out_copy)];

just sake of argument, image looks circles removed:

now, have analyzed of our shapes. bear in mind did total regionprops phone call because don't know want in analysis... decided give everything.

hope helps!

matlab image-processing image-segmentation

No comments:

Post a Comment