MATLAB: Removing some connected component

imclearborderremove connected component

Hello everyone,
In Image Processing, ''imclearborder'' is used to remove connected components that touch the borders of the image.
But i want to remove some specific, for example in my condition it will be like this '' if (49,90) pixel is in any connected component then remove it like imclearborder does.
And also sometimes i may not want to remove all components but only those which touch right border of the image or up border etc.
How can i make my special adjustments? Is it concerned with imclearborder(IM,4) OR imclearborder(IM,8)?
Thanks in advance.

Best Answer

Hi Selim,
if your input is a label image, you can use the following syntax:
img(img == ind) = 0;
This will detect which pixels are labeled with label IND, and set them to 0, which is the usual label for background. You can convert binary image to label image by using the bwlabel function.
If you want to remove the region around the point (40, 50):
ind = img(50, 40); % beware of index ordering
img(img == ind) = 0;
If you want to remove labels touching right border:
inds = unique(img(:, end));
inds(inds==0) = 0;
img(ismember(img, inds)) = 0;
Not that the bwconncomp function may be useful too. The output structure is different, and may be more efficient in some cases.
Regards