find the neighbor region

2 views (last 30 days)
Tran Tuan Anh
Tran Tuan Anh on 25 Jun 2014
Edited: Image Analyst on 26 Jun 2014
I have two binary image, A and B ( A and B are same size but have different object). There are many regions in each image. Now I want to find the neighbor of any regions in A. I use imdilate - dilation function in matlab. Because after dilation the regions of A will be increase so this may be connected with a region in B. I will add the region of B to A and continue to do it. I will stop when we can't add any region of B to A.
I don't know how to code this ? can you help me. thank you so much ? will it take a lot of time to calculate?

Answers (1)

Image Analyst
Image Analyst on 26 Jun 2014
So basically you want to keep growing A until it completely engulfs B. I'm not sure why/where this would be useful. But anyway, you can do this (pseudocode)
AorB = A; % Initialize
while true
if sum(A(:)) == sum(AorB(:))
break; % B is completely within A
end
A = imdilate(A, true(3)); % Grow A by one layer
AorB = A | B; % "OR" the growing A with the constant B image.
end
Basically keep growing until adding B to the growing A will not add any more pixels and the sizes are the same.
  1 Comment
Image Analyst
Image Analyst on 26 Jun 2014
Edited: Image Analyst on 26 Jun 2014
Actually, you may be able to quit before A completely engulfs B, as long as all B are "connected" to A. Is that what you want? If that's true, label the image
[L, blobCount] = bwlabel(A);
and stop when blobCount = 1:
while true
A = imdilate(A, true(3)); % Grow A by one layer
AorB = A | B; % "OR" the growing A with the constant B image.
[L, blobCount] = bwlabel(AorB);
if blobCount == 1
break;
end
end
It will keep growing until all blobs in B are attached to A, then quit. It will not keep growing to engulf all blobs in B like the first code did. I think this is more like what you described.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!