Mykje
Mykje

Reputation: 135

Matlab - neighbouring clusters (in two different matrices)

Given two matrices A and B (same size - both containing only 1 and 0) and the associated structures from using bwconncomp on both of them.

How can I determine if clusters (position of which is contained in CC.PixelIdcList) in matrix A have neighbouring pixels with a position which matches the position of pixels in one of the clusters in matrix B?

I would like to make a list which contains id of each cluster in matrix A and the id of clusters neighbouring it (in matrix B) as well as the position of the neighbouring pixels that has position matching clusters in matrix B.

ID cluster (from A) - ID clusters (from B) - positions

Upvotes: 0

Views: 299

Answers (1)

carandraug
carandraug

Reputation: 13091

I think it may be simpler to drop the use of bwconncomp and instead use bwlabel (or bwlabeln if this is N dimensional).

[labelA, numA] = bwlabel (mA);
[labelB, numB] = bwlabel (mB);

inter = labelA & labelB;

cluster_ind = unique (labelA(inter));

match_cluster = repmat ({[]}, numA, 1);
match_ind     = repmat ({[]}, numA, 1);
for idx = cluster_ind
  %% index with IDs from A to get...
  %% ... an array with IDs from B that intersect it
  match_clusters{idx} = unique (labelB(labelA == idx));
  %% ... linear indices for the elements that match something in B 
  match_ind{idx} = find (labelA === idx);
end

I'm assuming you have the original images before giving them to bwconncomp, if not, should be trivial to reconstruct a label image from its output.

Upvotes: 0

Related Questions