MATLAB: Filter centroids to find only those of interest in binary image

binary imagefilter centroids

I have defined the following centroids within a region of interest (blue rectangle) for my binary image. How can I remove or centroids within the red boxed region?

Best Answer

If you know the rows and columns outlining your box you can erase the image inside that box before computing the centroids
mask(row1:row2, col1:col2) = false;
props = regionprops(mask, 'Centroid');
Or you can keep the mask as-is, and just remove the measurements from the output of regionprops():
xy = vertcat(props.Centroid);
xCentroids = xy(:, 1);
yCentroids = xy(:, 2);
badIndexes = xCentroids >= col1 & xCentroids <= col2 & yCentroids >= row1 & yCentroids <= row2;
% Delete the bad ones.
prop(badIndexes) = [];