MATLAB: Clearing border components from an imfreehand ROI

bwconncompimage processingImage Processing Toolboximclearborderimfreehandroi

I have a binary image with an ROI defined using imfreehand function; and I want to remove object touching the ROI border. imclearborder is a great function to remove border objects but it works for rectangular ROI. What is the less-costly method to do this? I tried using the freehand ROI border and "bwconncomp" function but it requires multiple loops which is costly for a big image.

Best Answer

Thanks for the help. I dug a bit more and found a trick to get what I wanted. This is what I have used:
% Build a location array for the pixels on the edge of the freehand curve
% lassoxy.i and lassoxy.j are the index positions of the freehand curve.
% Img is the binary image produced by masking the original image by imfreehand.
locationxy=(lassoxy.j-1)*size(Img,1)+lassoxy.i;
% Use flood-fill operation to remove blobs touching the boundary (inside) of the freehand curve (negative binary image)
BW2 = imfill(~Img,locationxy);
Img_Bord = ~BW2; % Img_Bord is the binary image with all blobs touching the ROI edge removed.