remove double boundary at image border (bwareaopen, bwperim)

I am using a Matlab example to find the contours in an image and outline them. This technique works very well for all structures on the inside of the image. If one of the structures (finger-like electrodes under the microscope) is cut at the image's border the routine tries to close it, resulting in a "double contour" of the finger.
I need to make the program realize that the structure is in fact at the border and the outer contour should be the only one detected.
Using imclearborder I can remove the contours of objects affected, but this would affect to many of the shapes I am trying to trace!
I tried uploading an snapshot of the problem, but my university seems to be blocking every site I know and tried, so I can only attach the code (pretty straight forward from the ML examples) and abused my profile photo for the image. Sorry for that.
function [ output_args ] = process_sem( I_sem, diamondsize, bwareaopensize, bwperimconnectors )
%Finds outlines in a grayscale image and displays them in overlay
J = adapthisteq(I_sem,'NumTiles', [8 8], 'clipLimit', 0.002,'Distribution', 'exponential');
h1 = fspecial('gaussian',5,5); % set up a Gaussain filter
Im1 = imfilter(J,h1,'replicate'); % filter the signal by Gaussian filter
Im2 = im2bw(Im1,graythresh(Im1)+0.1);
se=strel('diamond',diamondsize);
Im2=imdilate(Im2,se);
Im2=imerode(Im2,se);
%If border-connection should be removed uncomment the following line
%clearbordersconn=4;
%Im2=imclearborder(Im2,clearbordersconn);
se90 = strel('line', 3, 45);
se0 = strel('line', 3, 0);
Im2= imdilate(Im2, [se90 se0]);
Im3 = bwareaopen( Im2, bwareaopensize);
Im3=imfill(Im3, 'holes');
%final smoothing
seD = strel('diamond',1);
Im2 = imerode(Im2,seD);
Im2 = imerode(Im2,seD);
%final smoothing end
Im3outline = bwperim(Im3,bwperimconnectors);
Segout = I_sem;
Segout(Im3outline) = 255;
figure;imshow(Segout), title('outlined original image');
[imx,imy] = gradient(double(Im3outline),1.0);
output_args = [imx, imy, Im3 ];
Any help is very much appreciated!
Kind regards, Chris

 Réponse acceptée

Image Analyst
Image Analyst le 14 Jan 2013

0 votes

Why would imclearborder() change the contours of the remaining objects? Any object touching the border you want to remove because you don't have the entire object in the field of view. Unless you're talking about the insides of those rod-shaped things and you want to keep the inside even if the bright perimeter slightly touches the edge. It would be good for you to somehow find a way to upload an image. How about a site on your university's own server? It should not complain about that.

5 commentaires

Thank you for your help and for the help you posted on many other questions that helped me working with images in ML so far!
I managed to upload the image at my home. Red marks the current situation of contours that intersect the image border and that I have been unsuccessful to remove so far. Green is the contour I would like to get in the end.
So my goal is to extract a single, outer contour that I then can "blow up" by a defined pixel number (needed for further analysis).
best wishes, Chris
Image Analyst
Image Analyst le 15 Jan 2013
Modifié(e) : Image Analyst le 15 Jan 2013
Why do you want to analyze that region when part of the region is missing? The results won't be valid. It's tough in general unless you can say that all the regions will be continuous loops broken at the edge. But if there are 2, 3, or more breaks in the boundary, plus some interior ones that are closed loops, it gets ambiguous.
on a second thought are exactly right!
I did improve the results significantly using imopen and imclose and the disturbance is not really relevant in those regions!
Is there a way to calculate the shortes distance in pixels between the contour lines (from one contour to another)?
Thank you for your help!
kind regards, Chris
Sure - you have all the coordinates. Use norm(), hypot() or the Pythagorean theorem.
Thank you so much for your help! :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by