Identify Individual Shape from Matrix Representing Multiple Shapes

5 ビュー (過去 30 日間)
Michelle De Luna
Michelle De Luna 2021 年 2 月 4 日
コメント済み: Matt J 2021 年 2 月 7 日
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!

採用された回答

Matt J
Matt J 2021 年 2 月 4 日
編集済み: Matt J 2021 年 2 月 4 日
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 件のコメント
Image Analyst
Image Analyst 2021 年 2 月 7 日
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.
Matt J
Matt J 2021 年 2 月 7 日
The OP has said that only one shape will fall inside the box.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 2 月 6 日
See my attached shape recognition demos. Adapt as needed.
  1 件のコメント
Michelle De Luna
Michelle De Luna 2021 年 2 月 7 日
Image Analyst, thank you for attaching the demos! They were very helpful!

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by