How can i check a 3D object is located inside or outside of another 3D object?

조회 수: 4 (최근 30일)
May
May 2018년 9월 28일
댓글: Rik 2018년 9월 29일
I have two 3D objects. One is a small object and another is a big one. Both of them have the same dimension as I attached. I would like to check the small object is located inside, outside or attached boundary of the big object. Please kindly advise me to solve it. Thank you.

채택된 답변

Rik
Rik 2018년 9월 28일
편집: Rik 2018년 9월 28일
Because your data is two logical arrays, you can easily test if the smaller is inside the larger. Finding out if the smaller object is touching the edge is slightly more tricky: we reduce the big object to only its shell, and then we do the same thing again.
%2D test cases
%case 1: completely inside
SmallObj=[0 0 0;0 1 0;0 0 0];
BigObj=true(3,3);
%case 2: edge
SmallObj=[0 0 0;0 1 0;0 1 0];
BigObj=true(3,3);
%case 3: outside
SmallObj=[0 0 0;0 1 0;0 1 0];
BigObj=[0 0 0;0 1 0;0 0 0];
%convert to logical
BigObj=logical(BigObj);
SmallObj=logical(SmallObj);
clc
s=load('data.mat');SmallObj=s.SmallObj;BigObj=s.BigObj;
temp_small=SmallObj;
temp_small=temp_small(~BigObj);
if any(temp_small(:))
disp('some part of SmallObj is outside BigObj')
else
temp_big = bwperim(BigObj);%get the shell voxels
temp_small=SmallObj;
temp_small=temp_small(temp_big);
if any(temp_small(:))
disp('some part of SmallObj along the edge of BigObj')
else
disp('SmallObj is completely inside BigObj')
end
end
  댓글 수: 2
May
May 2018년 9월 29일
Thank you so much for your answer. When I tested the attached data according to your code, it results "some part of SmallObj is outside BigObj". But, when I plot these two objects (green is BigObj and red is SmallObj as in the following image), it seems totally inside of the BigObj. In the actual case, it should be totally inside too. Please assist me to solve it.
Rik
Rik 2018년 9월 29일
How did you plot this?
It does look indeed inside, but I also notice some edges on the inside(?) of the green shape. If there is a small gap that passes through the small object, this code will flag it as being partially outside. If you want to prevent this, you could do a binary close operation on the big object to close those gaps.
My test cases work, so I don't think my code is broken.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by