Check if a binary image is within a particular binary area

I have a binary image BW_large, and another binary image BW_small
How do I check if BW_small is within BW_large?

 Accepted Answer

I'm assuming your images are the same size, but contain objects of differing size.
The intersection of two binary images A and B is
intAB = A & B;
If the intersection is equal to one of the two images, then that image is wholly contained within the other.
BinA = isequal(B,intAB);
Otherwise, if you have different size images which contain similarly-sized and oriented copies of the same object, you might look at normxcorr() or something.

6 Comments

Sir, whatever i give, the answer i get is 0
I have attached 3 matfiles
I want to check if BW1, BW2, BW3 lies within BW
BW1 - does not lie inside BW
BW2 - lies within BW and
BW3 - the circle towards the bottom does not lie inside BW
I wrote the below code, but for all the cases, the answer is zero
intAB = BW & BW1;
BinA1 = isequal(BW1,intAB)
intAB = BW & BW2;
BinA2 = isequal(BW2,intAB)
intAB = BW & BW3;
BinA3 = isequal(BW3,intAB)
In case of BW1 and BW2 if it doesnt lie within BW i wanted to ignore than region
and in case of BW3 i wanted to fill the circle that doesnt come in the region of BW
Well, it's close, but not completely inside. You might want to change it to allow for a little error. This example checks to see if the intersection area is at least 95% of the area of BW1, BW2.
load BW.mat
load BW1.mat
load BW2.mat
tol = 0.95;
intAB1 = BW & BW1;
BinA1 = nnz(intAB1)/nnz(BW1) >= tol
intAB2 = BW & BW2;
BinA2 = nnz(intAB2)/nnz(BW2) >= tol
% these are pixels that aren't part of the intersection
subplot(2,1,1)
imshow(BW1 ~= intAB1)
subplot(2,1,2)
imshow(BW2 ~= intAB2)
Sir I did that, and one region (one red dot) is outside the white area in BW3, so how can i fill with black or delete only that region
I didn't see BW3.
The intersection of BW3 is only 89.6% of its area. You could choose to set the tolerance value lower to get it to pass, or if you want to remove that one blob manually, you could.
newBW3 = intAB3; % return only the blobs belonging to the intersection
Thank you Sir, thats working. Sir one more doubt, how did you find tolerance?
This gives the ratio of the intersection area to the area of BW3
nnz(intAB3)/nnz(BW3)

Sign in to comment.

More Answers (1)

If you just want to know if ANY part of it is within the other blob, then you can make up your marker image and main image, and call imreconstruct(). You have years of image processing experience now so you should be able to figure it out.

Asked:

on 24 Apr 2022

Commented:

DGM
on 25 Apr 2022

Community Treasure Hunt

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

Start Hunting!