简体   繁体   中英

How to create a 3d surface from 2d images?

I've been working on modyfing dicom images so I can later 3d print them. The problem is that once I've modified the individual slices to turn them into an .stl file, the software I'm using (Osirix) prompts an error that asks for volumic data. If I try to render the .stl without modifying it first in MAtlab I get no error message.

I need an example of code for Matlab that can stack 2d images into a 3d surface so I can later import it to an .stl file. Can anyone help me?

One way could be to stack 2D images into a 3D cube of images.

Solution 1. The following solution is based in the following assumptions: 1) that the size of all 2d images are the same: n1 by n2 2) that you know the number of images (slices): n3 3) that the images are uint8

You could create a 3D cube beforehand with:

allImgs = uint8(zeros(n1, n2, n3));

Then to fill the first slice with aSlice image, do:

allImgs(:, :, 1) = aSlice;

For the second, do:

allImgs(:, :, 2) = anotherSlice;

and so forth.

If you have a function (like getMeASlice() )that gets the slices for you, you could efficiently call this from a for loop and fill it up:

for k = 1:n3
    allImgs(:, :, k) = getMeASlice(your_params);
end

Solution 2. You could concatenate slices as you go. Say that you read a slice:

aSlice = imread('cameraman.tif');

and then you later on your code you read another slice and want to stack it to your existing one, you could do:

aSlice = cat(3, aSlice, imread('anotherimage.jpg'));

which concatenates your new slice in the third dimension. And you could keep doing this as long as you need.

However, note the following. Concatenating is very slow compared to preallocating a variable and adding things (populating) such variable. For instance, if you compare the following two scripts, the second one is much slower:

n1=256; n2=256; n3=200;
allImgs = uint8(zeros(n1, n2, n3));
aSlice = imread('cameraman.tif');

tic;
for k=1:n3
    allImgs(:,:,1)=aSlice;
end
fprintf(['Total time is: ' num2str(toc) '\n']);

tic;
allImgs=aSlice;
for k=2:n3
    allImgs=cat(3, allImgs, aSlice);
end
fprintf(['Total time is: ' num2str(toc) '\n']);

Gives the following in my computer:

Total time is: 0.0085632
Total time is: 0.89103

Let me know if this helps or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM