Removing background and measuring features of an image in MATLAB -
i'm trying measure areas of each particle shown in image:
i managed general shape of each particle using mser shown here:
but i'm having trouble removing background. tried using matlab's imfill, doesn't fill particles because cut off @ edges. tips on how rid of background or find areas of particles other way? cheers.
edit: imfill
looks like:
edit 2: here code used outline. used this mser.
%compute region seeds , elliptial frames. %mindiversity = how similar parent mser region %maxvariation = stability of region %brightondark used void dark. prevents dark %patches in void being detected. [r,f] = vl_mser(i,'mindiversity',0.7,... 'maxvariation',0.2,... 'delta',10,... 'brightondark',1,'darkonbright',0) ; %plot region frames, not used right %f = vl_ertr(f) ; %vl_plotframe(f) ; %plot msers m = zeros(size(i)) ; %m = no of overlapping extremal regions x=r' s = vl_erfill(i,x) ; m(s) = m(s) + 1; end %display region boundaries figure(1) ; clf ; imagesc(i) ; hold on ; axis equal off; colormap gray ; %create contour plot using values %0:max(m(:))+.5 no of contour levels. level 0 needed %[0 0] used. [c,h]=contour(m,[0 0]) ;; set(h,'color','r','linewidth',1) ; %retrieve image data contour image f = getframe; i2 = f.cdata; %convert image binary; red outlines while while rest %is black. i2 = all(bsxfun(@eq,i2,reshape([255 0 0],[1 1 3])),3); i2 = imcrop(i2,[20 1 395 343]); imshow(~i2);
proposed solution / trick , code
it seems can work m
here. 1 trick can employ here pad zeros across boundaries of image m
, fill holes. take care of filling blobs touching boundaries before, there won't blob touching boundaries because of zeros padding.
thus, after have m
, can add code -
%// binary version of m m_bw = im2bw(m); %// pad zeros across grayscale image padlen = 2; %// length of zeros padding m_pad = padarray(m_bw,[padlen padlen],0); %// fill holes m_pad_filled = imfill(m_pad,'holes'); %// background mask after holes gone background_mask = ~m_pad_filled(padlen+1:end-padlen,padlen+1:end-padlen); %// overlay background mask on original image show have %// working background mask use i(background_mask) = 0; figure,imshow(i)
results
input image -
foreground mask (this ~background_mask
) -
output image -
Comments
Post a Comment