Convert 2D image to 3D mesh

103 views
Skip to first unread message

gian...@gmail.com

unread,
Apr 30, 2019, 11:28:47 AM4/30/19
to iso2mesh-users
Hi all,

I have a 2D grayscale image (I attached the file of the image) and I would like to create a 3D mesh out of it, by simply extending the image along the vertical direction to create a 3D volume. 

My idea was to use repmat to create the volume from the image matrix and then covert it into mesh using v2m. However, how can I control the size of the mesh I want to create from the 3D volume? Ideally I would like a 1x1x2 mm meshed slab.
Is that achievevable?

Thanks,
Luca
Mouse_brain_picture.jpg

Qianqian Fang

unread,
Apr 30, 2019, 2:40:47 PM4/30/19
to iso2mes...@googlegroups.com, gian...@gmail.com

hi Luca

the first step is to segment your 2D image into vessel vs background, and convert it to a binary mask. Then, you can apply a similar process like I posted in this reply

https://groups.google.com/d/msg/iso2mesh-users/4wgNaBKQZFM/ccYfSw4UCwAJ

if your goal is to create a 3D mesh, you can just do the first 2 lines, i.e. repmat and then v2m. 

you can also see a similar meshing process in this "Hello World" demo script

https://github.com/fangq/iso2mesh/blob/master/sample/demo_helloworld.m#L36-L39

one thing I did previously was to apply bwmorph(img, 'erode', 1) for each layer you extend the image along the z-axis, as you move away from the center slice. This will give you a square-shaped vessel. You can adjust the erosion rate to make it more circular in the z-axis, but it won't be perfect.

Qianqian



Thanks,
Luca
--
You received this message because you are subscribed to the Google Groups "iso2mesh-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iso2mesh-user...@googlegroups.com.
To post to this group, send email to iso2mes...@googlegroups.com.
Visit this group at https://groups.google.com/group/iso2mesh-users.
For more options, visit https://groups.google.com/d/optout.


gian...@gmail.com

unread,
May 1, 2019, 5:36:33 AM5/1/19
to iso2mesh-users
Thank you!

Is there a way with v2m to control the size (width x height x depth) of the final mesh or does it depend only on the size of the 3D volume?

Luca


On Tuesday, 30 April 2019 19:40:47 UTC+1, q.fang wrote:
On 4/30/19 11:28 AM, gian...@gmail.com wrote:
Hi all,

I have a 2D grayscale image (I attached the file of the image) and I would like to create a 3D mesh out of it, by simply extending the image along the vertical direction to create a 3D volume. 

My idea was to use repmat to create the volume from the image matrix and then covert it into mesh using v2m. However, how can I control the size of the mesh I want to create from the 3D volume? Ideally I would like a 1x1x2 mm meshed slab.
Is that achievevable?


hi Luca

the first step is to segment your 2D image into vessel vs background, and convert it to a binary mask. Then, you can apply a similar process like I posted in this reply

https://groups.google.com/d/msg/iso2mesh-users/4wgNaBKQZFM/ccYfSw4UCwAJ

if your goal is to create a 3D mesh, you can just do the first 2 lines, i.e. repmat and then v2m. 

you can also see a similar meshing process in this "Hello World" demo script

https://github.com/fangq/iso2mesh/blob/master/sample/demo_helloworld.m#L36-L39

one thing I did previously was to apply bwmorph(img, 'erode', 1) for each layer you extend the image along the z-axis, as you move away from the center slice. This will give you a square-shaped vessel. You can adjust the erosion rate to make it more circular in the z-axis, but it won't be perfect.

Qianqian



Thanks,
Luca
--
You received this message because you are subscribed to the Google Groups "iso2mesh-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iso2me...@googlegroups.com.
To post to this group, send email to iso2me...@googlegroups.com.

gian...@gmail.com

unread,
May 1, 2019, 11:53:44 AM5/1/19
to iso2mesh-users
I have converted the image into a binary mask (see binary.tiff attached) and I tried to convert it into a mesh using the following script:

I=imread('Mouse_brain_picture.png'); 
I=rgb2gray(I); 
img=imbinarize(I,'adaptive'); % Convert grayscale image into binary mask;
img=bwmorph(img,'erode',1); % Smooth binary mask;
vol=repmat(img,1,1,10);
[node,elem,face]=v2m(vol,1,2,10);

However I obtained only the vasculature mesh without the background (see attached mesh.tiff). Am I doing something wrong? How can i get both the media (vasculature + background)?

Luca
binary.tif
mesh.tif

Qianqian Fang

unread,
May 3, 2019, 5:50:16 PM5/3/19
to iso2mes...@googlegroups.com, gian...@gmail.com

hi Luca

please see my below script and attached screenshots

I=imread('binary.tif');

I=rgb2gray(I);
img=imbinarize(I,'adaptive'); % Convert grayscale image into binary mask;

vol=repmat(img,1,1,21);
for i=1:10
    vol(:,:,11+i)=bwmorph(vol(:,:,11+i),'erode',i);
    vol(:,:,11-i)=vol(:,:,11+i);
end
[node,elem,face]=v2m(uint8(vol+1),[],8,100, 'cgalmesh');
figure;
plotmesh(node,elem,'x>100')
figure;
plotmesh(node,elem(elem(:,5)==2,:))

Not entirely sure what's the squared frame outside of your vessel, but you can easily separate it after.

I first stack the 2d image into a 3D volume, and then I erode each layer differently, then I created a square-shared cross-section, see the 2nd mesh plot.

another way to do this is to keep the image in gray-scale, and use v2s() first to extract the vessel surface, then add a non-intersecting bounding box using meshabox and surfboolean (if intersecting)/mergemesh (if not intersecting), then call s2m to create the final mesh. the quality is better if you do this way, but the cgalmesh output might just be sufficient (if you don't need to apply smoothing or control the node densities).

Qianqian



Luca
--
You received this message because you are subscribed to the Google Groups "iso2mesh-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iso2mesh-user...@googlegroups.com.
To post to this group, send email to iso2mes...@googlegroups.com.
vessel_with_bk.png
vessel_mesh.png

gian...@gmail.com

unread,
May 7, 2019, 7:13:39 AM5/7/19
to iso2mesh-users
Thank you!

If I run the script myself on my platform, I don't actually get the strange frame!

I have a last question: Is there a way to control the size (width x height x depth) of the final mesh or does it depend only on the size of the 3D volume? I would like to have a 1.2 mm x 1.2 mm x 1.2 mm mesh (and perhaps add some extra depth made only of background medium (no vessels) in the z direction up to 1 cm).

Luca
To unsubscribe from this group and stop receiving emails from it, send an email to iso2me...@googlegroups.com.
To post to this group, send email to iso2me...@googlegroups.com.

Qianqian Fang

unread,
May 7, 2019, 11:35:20 AM5/7/19
to iso2mes...@googlegroups.com, gian...@gmail.com
On 5/7/19 7:13 AM, gian...@gmail.com wrote:
Thank you!

If I run the script myself on my platform, I don't actually get the strange frame!


glad to know


I have a last question: Is there a way to control the size (width x height x depth) of the final mesh or does it depend only on the size of the 3D volume? I would like to have a 1.2 mm x 1.2 mm x 1.2 mm mesh (and perhaps add some extra depth made only of background medium (no vessels) in the z direction up to 1 cm).


if you are referring to the aspect ratio of the generated mesh, if the aspect ratio is not too different from 1 (let's say <50% difference), then, a simple scaling should be fine, you can define

A=diag([1.2, 0.8, 1.1]);
node(:,1:3)=(node(:,1:3)*A);

if the aspect ratio is relatively big, you should use imresize() to resize the domain, preferably downsampling,  and then create the mesh based on the properly resampled volume.

hope this helps.


Qianqian

To unsubscribe from this group and stop receiving emails from it, send an email to iso2mesh-user...@googlegroups.com.
To post to this group, send email to iso2mes...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages