How to built a 3D binary volume array from set of X,Y,Z coordinates?

36 views (last 30 days)

Hi Guys,

Could I get some help with building a single binary 3D matrix array from a set of 3D field coordinates data please

I have compiled the X,Y,Z field coordinates of the 1100 nodes into a single array. where it can be written as 3 column vectors each of size (1100 x 1).

>> Node_X = XYZ{1};
>> Node_Y = XYZ{2};
>> Node_Z = XYZ{3};
>> scatter3(Node_X,Node_Y,Node_Z)

I tried using scatter3 to visualize the 3D image and it gives the image. But I would like to represent this 3D image as a 3D binary matrix array I also tried using meshgrid, but there seems to be an error stating the size of the array to be too large.

I thought of converting all the 517 x 517 (XY slices) along the Z coordinate to a binary matrix.(slice by slice) and eventually combining the slices into a 3D binary volume.

    Z_unique=unique(Node_Z(:));
    Slice_bin= zeros (517,517);
XY=[Node_X, Node_Y];
for n=Z_unique
        k=1;
      for i=1:1189
          if Node_Z(i)==n
              n_slice(k,:)=[Node_X(i), Node_Y(i)];
              k=k+1;
          end
      end
  	Slice_bin(n_slice{1}, n_slice{2})=1;
  end 
     % Stack these 2-D binary matrices into a single 3D matrix
  I=cat(length(n),Slice_bin{1},Slice_bin{2});
  % Display image
%imshow3Dfull function (Maysam Shahedi,2018)
  Image = squeeze(I); 
figure, 
imshow3Dfull(Image)

The binary image slices don't seem to show up using the above code. Is there any error in my coding? or any other way, I could build a 3D matrix from set of x,y,z field coordinates?

Any feedback/insights are greatly appreciated!

Thanks

  3 Comments
Sophie
Sophie on 2 Oct 2018
Yup, I tried working on it-converting it to binary slices by slices. But seems to be having errors as well
any ideas?
jonas
jonas on 2 Oct 2018
Edited: jonas on 2 Oct 2018
I'm still trying to understand what you are trying to do. When I ran your code I obtained about 374 unique z-values. This means that you have at a minimum 374 (unequally spaced) xy-slices. Where does 517 come from?
Let's say you make one plane for each slice. Each slice will then only have have about 2 white elements, the rest being zeros. If you display one of those slices as an image, it will just appear dark with 1-2 bright pixels. Moreover, you cannot treat such a slice as an image (nor display it with imshow), because an image is usually laid out on a grid with equal spacing between pixels, whereas you want to retain the XYZ-data.

Sign in to comment.

Accepted Answer

jonas
jonas on 2 Oct 2018
Edited: jonas on 3 Oct 2018
Still not 100% sure what you are looking for, but you said you want to display your scattered points as a solid, so that leads me to believe this would be a step in the right direction.
%%Your points
x=Node_X;
y=Node_Y;
z=Node_Z;
%%3d grid
[X,Y,Z]=meshgrid(min(x):1:max(x),min(y):1:max(y),min(z):1:max(z));
%%3d boundary
tri = delaunayn([x y z]);
%%find points inside of 3d boundary
tn = tsearchn([x y z], tri, [X(:) Y(:) Z(:)]);
IsInside = ~isnan(tn)
%%build volumetric image
I=logical(zeros(size(X)));
I(IsInside)=true;
You can change the image resolution by adjusting the number of points in your grid. Even with a step-size of one unit, the code runs very slow.
Here's a faster way to look at your data as a shell
k=boundary([x,y,z])
trisurf(k,x,y,z)
The left figure displays a shell and the right figure displays a slice of the stacked image I.
  1 Comment
jonas
jonas on 3 Oct 2018
Edited: jonas on 3 Oct 2018
It does take time. You can reduce the resolution in the meshgrid line to verify that it works. Then you can increase the resolution and run it over night or so.
Try
[X,Y,Z]=meshgrid(min(x):5:max(x),min(y):5:max(y),min(z):5:max(z));
Tool about 2 min for my 5 yr old laptop

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!