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
topEdge = binaryImage(1, :);
leftEdge = binaryImage(:, 1);
rightEdge = binaryImage(:, end);
binaryImage(1, :) = false;
binaryImage(:, 1) = false;
binaryImage(:, end) = false;
binaryImage = imclearborder(binaryImage);
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.
Best Answer