MATLAB: How can i select labeled images that are touching the x axis

Image Processing Toolboxx axis labeling

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

Best 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.