If I select an element in a matrix, say MAT(2,2), how to find how many elements of the same value of MAT(2,2) in the matrix are "connected" to it.

조회 수: 3 (최근 30일)
For example, Mat=[1 2 3 4 2;2 2 2 2 0;2 0 4 2 1;1 3 4 2 8] and there're 7 elements (namely MAT(1,2), MAT(2,1), MAT(2,3), MAT(2,4), MAT(3,1), MAT(3,4), MAT(4,4)). I found that the function "bwareaopen" can be used to remove connected pixels whose size is smaller than the given threshold; I thought it must have used a similar trick, but I failed to figure it out.

답변 (2개)

Massimo Zanetti
Massimo Zanetti 2016년 10월 25일
편집: Massimo Zanetti 2016년 10월 25일
You need bwconncomp
For example:
Mat=[1 2 3 4 2;2 2 2 2 0;2 0 4 2 1;1 3 4 2 8]
%checks for the elements of Mat that equals Mat(2,2)
CC = bwconncomp(Mat==Mat(2,2));
connComponents = labelmatrix(CC)
since Mat(2,2)=2 you get
Mat =
1 2 3 4 2
2 2 2 2 0
2 0 4 2 1
1 3 4 2 8
connComponents =
0 1 0 0 1
1 1 1 1 0
1 0 0 1 0
0 0 0 1 0

Andrei Bobrov
Andrei Bobrov 2016년 10월 25일
[~,out] = bwlabel(Mat(2,2)==Mat)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by