Asked by nasim mirzavand
on 15 Jul 2019

Hi,

I need to write a code to divide a 3D figure in matlab in to cubes and getting the cubes's addresses, who can help me in this regard?

Best

Answer by Walter Roberson
on 16 Jul 2019

% Step 1: Open the regions of interest file

B= dicominfo('C:\Users\mij\Music\RS.1.3.6.1.4.1.2452.6.3144839235.1145246484.2165573795.1001182548.dcm');

% Step 2: Start display

figure(1);

clf;

hold on;

% Step 3: Load number of regions of interest

roi = fieldnames(B.ROIContourSequence);

nroi= size(roi,1);

roi_cell = cell(nroi, 1);

% Step 4: Loop over all regions of interest

for i=1:nroi

% Step 4.1: Read the color of region of interest i

color=(B.ROIContourSequence.(roi{i}).ROIDisplayColor)/255;

% Step 4.2: Read the number of slices for region of interest i

slices=fieldnames(B.ROIContourSequence.(roi{i}).ContourSequence);

nslices=size(slices,1);

patches_info_cell = cell(nslices, 3);

% Step 4.3: Run over all the slices

for j=1:nslices

% Step 4.3.1: Find the number of points and their coordinates in

% the current slice

npoints=B.ROIContourSequence.(roi{i}).ContourSequence.(slices{j}).NumberOfContourPoints;

points=B.ROIContourSequence.(roi{i}).ContourSequence.(slices{j}).ContourData;

% Step 4.3.2: Convert single vector into 3D vector

x=points(1:3:3*npoints);

y=points(2:3:3*npoints);

z=points(3:3:3*npoints);

% Step 4.3.3: Plot the curve

H = fill3([x',x(1)],[y',y(1)],[z',z(1)],'r');

set(H,'FaceColor',color)

axis off ;

patches_info_cell(j,:) = {length(H.Faces), H.Faces, H.Vertices};

end

roi_cell{i} = patches_cell;

end

all_roi = vertcat(roi_cell{:});

At this point, if I have written the code correctly, then all_roi will be a something-by-3 cell array of information about contours for slices. Column 1 of the cell will be a scalar indicating the number of vertices, N, in the line. Column 2 of the cell will contain a row vector of face information, and it appears likely that it will be 1 : number of vertices, but if there are duplicate points in your contours that might not hold. Column 3 of the cell will contain an N x 3 2D array of vertex locations.

In preparing the vertex information for the github function, you should unique-ify the vertex coordinates as failure to do so could prevent some of the loops from closing properly; you will need to keep track of the original order (third output of unique)

You cannot just slam all of the face information together into one face array

- You need to renumber from local vertex numbers to over-all vertex numbers
- The number of vertices might not be the same for all faces. To create a proper face array in which each row describes one face, the shorter arrays must be padded with nan. The length information I stored saves some post-processing to find that information in order to figure out how wide the final face array will have to be.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 14 Comments

## KSSV (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724456

## nasim mirzavand (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724459

## Walter Roberson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724462

## nasim mirzavand (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724716

## Walter Roberson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724719

## nasim mirzavand (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724720

## Walter Roberson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724737

## nasim mirzavand (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724740

## Walter Roberson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724744

## nasim mirzavand (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724757

## Image Analyst (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724781

## Walter Roberson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724815

## nasim mirzavand (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724821

## Walter Roberson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/471700-how-to-divide-a-3d-figure-in-matlab-in-to-cubes-and-getting-the-cubes-s-addresses#comment_724825

Sign in to comment.