MATLAB Answers

Finding number of connected components of a single value of pixel

3 ビュー (過去 30 日間)
Ankit Sahay
Ankit Sahay 2020 年 8 月 18 日
コメント済み: Image Analyst 2020 年 8 月 23 日
I have an image in the form of a matrix:
A zoomed-in portion of the upper right part is:
I would like to remove the disconnected black region, i.e. set that particular region's pixel values equal to that of the white region around it. I tried finidng the number of connected components using
CC = bwconncomp(filled_win);
but I am getting only 1 connected component.
CC.Connectivity = 8, CC.ImageSize = [416, 349]
CC.NumObjects = 1, CC.PixelIdxList = 1 X 1 cell
I have attached the relevant .mat file for reference. Can someone please help me with this?

  1 件のコメント

Ankit Sahay
Ankit Sahay 2020 年 8 月 18 日
The filled_win.mat matrix is not binarized. It can be binarized using
filled_win = imbinarize(filled_win);
Even after doing this,
CC = bwconncomp(filled_win);
doesn't work. Maybe my concept of connected components is wrong, or maybe I am not using the command in the right way. Please help.

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

採用された回答

jonas
jonas 2020 年 8 月 18 日
編集済み: jonas 2020 年 8 月 19 日
If you want to find the two black (false) components, then you need to pass the complement and use 4-connectivity (no diagonal connections).
filled_win = imbinarize(filled_win);
CC = bwconncomp(~filled_win,4);
filled_win(CC.PixelIdxList{2}) = true;
What you found before was the single white component that is the white backgrund and the "boundary", returned as one object (as they are actually connected diagonally).

  3 件のコメント

Ankit Sahay
Ankit Sahay 2020 年 8 月 21 日
Thanks a lot! This worked perfectly. I am extremely sorry about the late reply.
jonas
jonas 2020 年 8 月 23 日
My pleasure. Dont forget to accept the answer ;)
Ankit Sahay
Ankit Sahay 2020 年 8 月 23 日
Oh yes..sure!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 8 月 23 日
There are multiple ways to do it. You could just use bwareafilt() to extract the largest blob:
filled_win = bwareafilt(filled_win, 1, 4); % Take largest 4-connected blob.
If the interior blob was the largest blob (not your case) then that could take the interior blob. A different way is to assume that the interior blob has no pixels to the left of the containing blob. Then just take the left most blob, which will have a label of 1
labeledImage = bwlabel(filled_win, 4);
filled_win = ismember(labeledImage, 1); % Take outer blob.

  1 件のコメント

Image Analyst
Image Analyst 2020 年 8 月 23 日
Don't forget to invert your mask! In MATLAB all the functions operate on the white blobs, not the black blobs.

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

Community Treasure Hunt

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

Start Hunting!

Translated by