Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting regions in matlab

Suppose now I have an image segmentation map S, whose indices range in 1 to k. Indices with same numbers belongs to the same segment. For example:

S = [1 1 1 2 2 2;
     1 1 1 2 2 2;
     2 2 2 2 1 1;
     2 2 2 2 1 1;
     2 2 2 2 1 1]

For this segmentation map S, I would like to get regions. For example, the '1' in S has two isolated parts, so I would like to get three regions--two with label 1 and one with label 2, and to distinguish two '1's, I would like to change one of it to be another label, say 3. So the final map is:

S = [1 1 1 2 2 2;
     1 1 1 2 2 2;
     2 2 2 2 3 3;
     2 2 2 2 3 3;
     2 2 2 2 3 3]

I'm thinking of using connected component to solve this problem, but it requires to formulate the affinity matrix. Is there a better way to do it?

like image 233
jrenzhile Avatar asked Dec 31 '25 21:12

jrenzhile


1 Answers

Simply get a logical matrix of all ones and then get all connected elements with bwlabel. You can them merge them together. To avoid problems of mixing two areas, just add the max of the original matrix For example:

mask           = (S == 1);
labeled        = bwlabel (mask);
labeled(mask)  = labeled(mask) + max (S(:)) - 1;
labeled         = S + labeled;

The only difference is that none of the regions that before had a value of "1" have that value now, but at least they are labeled different. If you want one of them to keep their number, it's just a matter of making another mask for the highest value, and use it to turn all them back to "1".

labeled(S == max(S(:))) = 1;
like image 127
carandraug Avatar answered Jan 03 '26 13:01

carandraug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!