Identify Individual Shape from Matrix Representing Multiple Shapes

9 views (last 30 days)
Hi all,
I have a matrix which represents multiple shapes. (Picture an image with a black background and several white circles on it.) The black part of the image is represented by NaN's, while the white circles or shapes are represented by numbers. I have created a random rectangle, which essentially serves as a bounding box, and I would like to see which specific circle/shape falls within the boundaries of the rectangle. The circles/shapes are well spread apart, so only one circle/shape will fulfill the boundary requirement. How could I identify only the one shape that has some part of itself in the rectangle while ignoring the other shapes? In other words, how could I isolate the shape and create a new matrix with only the shape that fulfills my requirement? I would then have to apply this same method to other matrices representing more shapes while keeping the location of the rectangular bounding box the same.
Any tips would be greatly appreciated! Thanks in advance!

Accepted Answer

Matt J
Matt J on 4 Feb 2021
Edited: Matt J on 4 Feb 2021
You could make a binary image B of the box and a label matrix L of the shapes. Then the label index i of the intersected shape is,
i=max(B.*L,'all');
  3 Comments
Image Analyst
Image Analyst on 7 Feb 2021
Wouldn't you use unique() instead of max? Like if you had circles or shapes represented by numbers (1, 2, 3, etc), then you could make a labeled image, which ignores the nans:
L = shapesImage;
L(isnan(L)) = 0;
% Find out what values are in the box
blobLabels = unique(L(B)) % You'll get zeros to, but just ignore that since it's background that used to be nan.
L(B) is a list (column vector) of all pixel values of L that lie within the "TRUE" regions of B. So if shapes with ID # 4 and 7 were inside the box, blobLabels would be [0, 4, 7]. I think max() would only give you one number.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 6 Feb 2021
See my attached shape recognition demos. Adapt as needed.

Community Treasure Hunt

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

Start Hunting!