How do I remove blobs connected to border?

I want to remove objects connected to the border of AOI (Area of interest). I'm not sure what I should do to solve this problem. I'm aware of the imclearborder() function, but this only removes objects along the border of the image itself. I want to remove the red rice shown below:
Any tips or advice would greately appreciated. Thank you!

 採用された回答

DGM
DGM 2022 年 11 月 15 日
編集済み: DGM 2022 年 11 月 15 日

0 投票

This is one way. Use the ROI mask to connect the truncated blobs to the image border. Then you can use imclearborder().
% an image
A = imread('rice.png');
% binarize the rice somehow
mkrice = bwareaopen(A>130,100);
% create an ROI mask somehow
imshow(A);
ROI = images.roi.Polygon(gca);
ROI.Position = [88 28;41 84;36 159;140 211;228 124;220 28;141 62];
mkroi = createMask(ROI);
% show what we have
imshow([mkrice mkroi])
% combine the two and discard
mkout = imclearborder(mkrice | ~mkroi);
imshow(mkout)

4 件のコメント

Image Analyst
Image Analyst 2022 年 11 月 15 日
And the count is???
DGM
DGM 2022 年 11 月 15 日
What count? Nobody said anything about counting. That might be part of the assignment, but it's up to the person asking a question about homework to decide which parts of the task they need help with.
Image Analyst
Image Analyst 2022 年 11 月 15 日
Don't know what I was thinking. Maybe that he wanted to find the red rice - I don't remember. Sorry, never mind.
DGM
DGM 2022 年 11 月 15 日
It's okay. Some things get repeated so often I'm already starting to get flashbacks of similar cases myself.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 11 月 15 日

0 投票

You've probably figured it out by now but I'd use bwperim and imreconstruct.
Let me know if you can't figure it out and I'll give you the solution tomorrow.

製品

リリース

R2022b

質問済み:

2022 年 11 月 14 日

コメント済み:

DGM
2022 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by