how can i select labeled images that are touching a specific axis,like x-axis,y-axis,and their paralel axis,also the one touches 2 axis at the same time,forexample touches x-axis and its paralel axis at the same time

in some cases i want to detect the one touches an specific border,forexample x-axis,or the parallel axis to it,or y-axis or the parallel axis to it,and in some cases i want to remove the ones whom are touching these axis,and also is it possible to select 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 parallel 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 parallel 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,
i want to detect blobs which are shown in the picture seperately,once select the one touching the x-axis...imclearborder remove all blobs touching all 4 borders,

 Accepted Answer

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.

7 Comments

thank you so much ,i get what you said,i should work on it,how about the one touching 2 border at the same time?
Put that into a function. Pass in an array with the edges you want to remove, like [2,4] if you want to remove from edges 2 and 4 (top and bottom). So in the function you would loop over the vector. Inside the loop you'd have 4 if blocks to do the code I gave you. Here's some untested code I wrote for you:
% Remove blobs touching edge(s).
% 1 = left, 2 = top, 3 = right, 4 = bottom.
% edgesToRemove can be a single number, or an array of edge numbers
function binaryImage = RemoveEdgeBlobs(binaryImage, edgesToRemove)
% Save all edges.
topEdge = binaryImage(1, :);
bottomEdge = binaryImage(end, :);
leftEdge = binaryImage(:, 1);
rightEdge = binaryImage(:, end);
for k = 1 : length(edgesToRemove)
thisEdge = edgesToRemove(k);
if thisEdge == 1
% Remove blobs touching left edge.
% Zero the other 3 edges.
binaryImage(1, :) = false;
binaryImage(end, :) = false;
% binaryImage(:, 1) = false;
binaryImage(:, end) = false;
% Get rid of blob(s) touching left edge
binaryImage = imclearborder(binaryImage);
% Restore the 3 edges.
binaryImage(1, :) = topEdge;
binaryImage(end, :) = bottomEdge;
% binaryImage(:, 1) = leftEdge;
binaryImage(:, end) = rightEdge;
elseif thisEdge == 2
% Remove blobs touching top edge.
% Zero the other 3 edges.
% binaryImage(1, :) = false;
binaryImage(end, :) = false;
binaryImage(:, 1) = false;
binaryImage(:, end) = false;
% Get rid of blob(s) touching top edge
binaryImage = imclearborder(binaryImage);
% Restore the 3 edges.
% binaryImage(1, :) = topEdge;
binaryImage(end, :) = bottomEdge;
binaryImage(:, 1) = leftEdge;
binaryImage(:, end) = rightEdge;
elseif thisEdge == 3
% Remove blobs touching right edge.
% Zero the other 3 edges.
binaryImage(1, :) = false;
binaryImage(end, :) = false;
binaryImage(:, 1) = false;
% binaryImage(:, end) = false;
% Get rid of blob(s) touching right edge
binaryImage = imclearborder(binaryImage);
% Restore the 3 edges.
binaryImage(1, :) = topEdge;
binaryImage(end, :) = bottomEdge;
binaryImage(:, 1) = leftEdge;
% binaryImage(:, end) = rightEdge;
elseif thisEdge == 4
% Remove blobs touching bottom edge.
% Zero the other 3 edges.
binaryImage(1, :) = false;
% binaryImage(end, :) = 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(end, :) = bottomEdge;
binaryImage(:, 1) = leftEdge;
binaryImage(:, end) = rightEdge;
end
end
oh thank you so much,i really appreciate you so much,thank you for the code,i will work on it,thank you
when i use the first code on an image,like this rice png,i use
binaryimage=im2bw(i.0.48);
kh=binaryImage(1, :);
figure,imshow(kh);
which is the top border,it only shows the first row of top border,not labeled images that are touching the top border,the same is about left and righ border,only gives me a vertical line,which is the first column of left border,. and for the second code which is a fuction,i saved it as a function,and then write this.
hg=RemoveEdgeBlobs(binaryImage, edgesToRemove);
figure,imshow(hg);
gives no error,but no answers too
what i want is like imclearborder,but not all borders at the same time,once selecting or removing labeled touching buttom border,once top border,
So why did you get the top row of the binary image into kh? What was the purpose of that? Of course it's not a labeled image. You don't even need a labeled image. Do you see anything in my code about a labeled image?
sara, I didn't even have to modify the function I just wrote off the top of my head. It worked beautifully. Here, see the attached test2 m-file for a complete demo to read in rice and threshold and remove the blobs touching the top border.
yes it worked so perfectly,thank you very much,really helped me so much,thanks

Sign in to comment.

More Answers (1)

hello,can i change this code for removing a blob which only touches top edge,or selecting the one which only touches the buttom edge,cause when i use the code topEdge = binaryImage(1, :); then if i remove it,it will remove the one which touches the x-axis and top edge at the same time,but sometimes i want to remove the one which only touches the top edge,and keep the others which touch x-axis and top edge at the same time,so i can segment them once more.

3 Comments

I'm not really sure what you're asking. Yes, you can change the code to remove blobs that touch any edge.
in the image i attached there are 5 blobs,4 of them are touching top,buttom,righ,left edge,.but the fifth one which is the biggest one,is touching both the top and buttom ,so when i want to remove the blobs which are just connected to top,and not buttom,the one which is touching both top and buttom,will be delete too,can i change the code,in such a way,that when i want to remove the one only touches the top,it wont remove the one touches both buttom and top,like the largest blob here,
The only way I know how to do that is to use the code I gave to delete those touching the top, then find the ones touching the top. Then do the same for the bottom. Then AND those two images to get the blobs that touch BOTH the top and bottom and OR it in with the image to replace it.
Another option is to ask regionprops for the PixelList and examine it for row values of 1 and the last row.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!