Filter centroids to find only those of interest in binary image

1 回表示 (過去 30 日間)
Lauren
Lauren 2021 年 2 月 18 日
コメント済み: Image Analyst 2021 年 2 月 19 日
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?

採用された回答

Image Analyst
Image Analyst 2021 年 2 月 18 日
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) = [];
  2 件のコメント
Lauren
Lauren 2021 年 2 月 18 日
Thank you for the help! I was able to make a few changes to your code to fit my purposes. This is what I ended up using:
xCentroids = CentroidsInsideROI(:, 1);
yCentroids = CentroidsInsideROI(:, 2);
badIndexes = yCentroids >= 300 & yCentroids <= 600 ;
for c = 1:length(badIndexes)
if badIndexes(c) == 0
idx = c;
Centroids = CentroidsInsideROI(idx,:);
plot(Centroids(:,1),Centroids(:,2),'m*')
end
end
and this was my final result!
Image Analyst
Image Analyst 2021 年 2 月 19 日
Instead of that for loop, you could have just inverted the badIndexes and used that as a logical index to the centroids to plot only those good locations:
xCentroids = CentroidsInsideROI(:, 1);
yCentroids = CentroidsInsideROI(:, 2);
badIndexes = yCentroids >= 300 & yCentroids <= 600 ;
plot(xCentroids(~badIndexes), yCentroids(~badIndexes), 'm*')

サインインしてコメントする。

その他の回答 (1 件)

Lauren
Lauren 2021 年 2 月 18 日
I'm not sure the mask is working correctly. I used the following and it shows props as empty and mask as all zero values.
mask(800:1145, 300:600) = false;
props = regionprops(mask, 'Centroid');
xy = vertcat(props.Centroid);
xCentroids = xy(:, 1);
yCentroids = xy(:, 2);
badIndexes = xCentroids >= 300 & xCentroids <= 600 & yCentroids >= 800 & yCentroids <= 1145;
% Delete the bad ones.
prop(badIndexes) = [];
I think overlaying the mask and deleting the bad indices is the way I would prefer to do it.

Community Treasure Hunt

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

Start Hunting!

Translated by