how can i select labeled images that are touching the x axis?

i have some labeled objects in an image,is it possible to select only the ones which are connected to the x axis?forexample in this picture the ones i want to choose are the 2 orange ones which are touching the x axis

 Accepted Answer

To get rid of blobs that are touching the edge of the image you can do this:
binaryImage = imclearborder(binaryImage);
That would give the green and blue blobs. Now if you wanted to get just the orange ones and not the green and blue ones, then you have to subtract that from the original image
binaryImage = binaryImage - imclearborder(binaryImage);
or, you can use the xor() function
binaryImage = xor(binaryImage, imclearborder(binaryImage));
This gets extracts the blobs touching any border. If you want only those on the bottom edge, then there is a few more steps. Let me know if that's required.

5 Comments

yes,if you can please tell me,i should detedt only the ones touching the x axis,in some images the one touches the x axis,touch the other axis too,the one which is pararell with x axis(not y axis),at the top,but in some pictures,it broke,so i want only the one touch x axis,
If you want to keep ones touching the left, right, and top edges, you need to call regionprops() and ask for PixelList. Then, for each blob, you need to see if any coordinates on the bottom border are in the list.
[rows, columns] = size(binaryImage);
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'PixelList');
for k = 1 : numberOfBlobs
xy = measurements(k).PixelList;
% See if any of the y coordinates are the last possible y
onBottom(k) = any(xy(:, 2) == rows);
end
% Extract only those blobs that are on the bottom.
% Get a new binary image.
output = ismember(labeledImage, onBottom) > 0;
% Remeasure just those on the bottom
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'PixelList');
Or something like that. I didn't test it, so I can't be sure. Attach a binary image with blobs on all 4 sides if you want me to write code to demonstrate it to you.
okay sure,i will try it,and if it didnt give me result i will attach it,thank you so much
hi,is this image good to use for removing or selecting blobs which are touching the borders.?this is jpg,when i use im2bw and save the results,it gives me a picture whit white extra borders,so i post the original jpg image,which can be binarized by im2bw,.in some cases i want to detect the one touches an specific border,forexample x-axis,or the pararel axis to it,or y-axis or the pararel axis to it,and in some cases i want to remove the ones whom are touching these axis,and also is it possible to selet the one which touches 2 axis at the same time,mostly in my case there are some labeled images which are touching both x-axis and its pararrel axis(like one of the blobs in the picture,,so im going to select them,and segment them once more,after removing the ones whom only touch x-axis,and in case of margines,im goint to remove some labels whom touch the y-axis or its pararell axis,and x-axis at the same time,so if you can tell me how i can select the one whom touching each border seperately,and also selecting the one touching 2 border at the same time,im so sorry for this much late answer,i was busy segmenting,and just use imclearborder to see are they going to be removed,but in some cases i just want to remove one touches a specific border,and not all borders,thank you so much, i will post this as a seperate question too,thanks
To get rid of objects touching just one border is simple. Simply store the 3 edges of the image that you want to keep. Then zero out those 3 edges and call imclearborder(). Then restore the 3 edges that you zeroed out. Let's say you wanted to delete blobs touching the bottom edge only. Then
% Save 3 edges.
topEdge = binaryImage(1, :);
leftEdge = binaryImage(:, 1);
rightEdge = binaryImage(:, end);
% Zero 3 edges.
binaryImage(1, :) = false;
binaryImage(:, 1) = false;
binaryImage(:, end) = false;
% Get rid of blob(s) touching bottom edge
binaryImage = imclearborder(binaryImage);
% Restore the 3 edges.
binaryImage(1, :) = topEdge;
binaryImage(:, 1) = leftEdge ;
binaryImage(:, end) = rightEdge ;
You could build all that into a function where you pass in the edge(s) you want to remove from.

Sign in to comment.

More Answers (0)

Asked:

on 12 Apr 2015

Commented:

on 30 May 2015

Community Treasure Hunt

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

Start Hunting!