Wed, 05 Aug 2009 17:10:56 +0100
add README
1 /*
2 #
3 # File : mcf_levelsets3d.cpp
4 # ( C++ source file )
5 #
6 # Description : Implementation of the Mean Curvature Flow on Surfaces
7 # using the framework of Level Sets 3D.
8 # This file is a part of the CImg Library project.
9 # ( http://cimg.sourceforge.net )
10 #
11 # Copyright : David Tschumperle
12 # ( http://www.greyc.ensicaen.fr/~dtschump/ )
13 #
14 # License : CeCILL v2.0
15 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
16 #
17 # This software is governed by the CeCILL license under French law and
18 # abiding by the rules of distribution of free software. You can use,
19 # modify and/ or redistribute the software under the terms of the CeCILL
20 # license as circulated by CEA, CNRS and INRIA at the following URL
21 # "http://www.cecill.info".
22 #
23 # As a counterpart to the access to the source code and rights to copy,
24 # modify and redistribute granted by the license, users are provided only
25 # with a limited warranty and the software's author, the holder of the
26 # economic rights, and the successive licensors have only limited
27 # liability.
28 #
29 # In this respect, the user's attention is drawn to the risks associated
30 # with loading, using, modifying and/or developing or reproducing the
31 # software by the user in light of its specific status of free software,
32 # that may mean that it is complicated to manipulate, and that also
33 # therefore means that it is reserved for developers and experienced
34 # professionals having in-depth computer knowledge. Users are therefore
35 # encouraged to load and test the software's suitability as regards their
36 # requirements in conditions enabling the security of their systems and/or
37 # data to be ensured and, more generally, to use and operate it in the
38 # same conditions as regards security.
39 #
40 # The fact that you are presently reading this means that you have had
41 # knowledge of the CeCILL license and that you accept its terms.
42 #
43 */
45 #include "CImg.h"
46 using namespace cimg_library;
48 // The lines below are necessary when using a non-standard compiler as visualcpp6.
49 #ifdef cimg_use_visualcpp6
50 #define std
51 #endif
52 #ifdef min
53 #undef min
54 #undef max
55 #endif
57 // Apply the Mean curvature flow PDE
58 //-----------------------------------
59 template<typename T> CImg<T>& mcf_PDE(CImg<T>& img, const unsigned int nb_iter,
60 const float dt=0.25f, const float narrow=4.0f) {
61 CImg<T> veloc(img.dimx(),img.dimy(),img.dimz(),img.dimv());
62 CImg_3x3x3(I,float);
63 for (unsigned int iter=0; iter<nb_iter; iter++) {
64 cimg_for3x3x3(img,x,y,z,0,I) if (cimg::abs(Iccc)<narrow) {
65 const float
66 ix = 0.5f*(Incc-Ipcc),
67 iy = 0.5f*(Icnc-Icpc),
68 iz = 0.5f*(Iccn-Iccp),
69 norm = (float)std::sqrt(1e-5f+ix*ix+iy*iy+iz*iz),
70 ixx = Incc+Ipcc-2*Iccc,
71 ixy = 0.25f*(Ippc+Innc-Inpc-Ipnc),
72 ixz = 0.25f*(Ipcp+Incn-Incp-Ipcn),
73 iyy = Icnc+Icpc-2*Iccc,
74 iyz = 0.25f*(Icpp+Icnn-Icnp-Icpn),
75 izz = Iccn+Iccp-2*Iccc,
76 a = ix/norm,
77 b = iy/norm,
78 c = iz/norm,
79 inn = a*a*ixx + b*b*iyy + c*c*izz + 2*a*b*ixy + 2*a*c*ixz + 2*b*c*iyz;
80 veloc(x,y,z) = ixx+iyy+izz-inn;
81 } else veloc(x,y,z) = 0;
82 float m, M = veloc.maxmin(m);
83 const double xdt = dt/cimg::max(cimg::abs(m),cimg::abs(M));
84 img+=xdt*veloc;
85 }
86 return img;
87 }
89 // Main procedure
90 //----------------
91 int main(int argc,char **argv) {
92 cimg_usage("Mean curvature flow of a surface, using 3D level sets");
93 const char *file_i = cimg_option("-i",(char*)0,"Input image");
94 const float dt = cimg_option("-dt",0.05f,"PDE Time step");
95 const float narrow = cimg_option("-band",5.0f,"Size of the narrow band");
96 const bool both = cimg_option("-both",false,"Show both evolving and initial surface");
98 // Define the signed distance map of the initial surface
99 CImg<> img;
100 if (file_i) {
101 const float sigma = cimg_option("-sigma",1.2f,"Segmentation regularity");
102 const float alpha = cimg_option("-alpha",5.0f,"Region growing tolerance");
103 img.load(file_i).channel(0);
104 CImg<int> s;
105 CImgDisplay disp(img,"Please select a starting point");
106 while (!s || s[0]<0) s = img.get_select(0,disp);
107 CImg<> region;
108 float tmp[1] = { 0 };
109 img.draw_fill(s[0],s[1],s[2],tmp,1,region,alpha);
110 ((img = region.normalize(-1,1))*=-1).blur(sigma);
112 }
113 else { // Create synthetic implicit function
114 img.assign(60,60,60);
115 const float exte[1]={1}, inte[1]={-1};
116 img.fill(*exte).draw_rectangle(15,15,15,45,45,45,inte).draw_rectangle(25,25,0,35,35,img.dimz()-1,exte).
117 draw_rectangle(0,25,25,img.dimx()-1,35,35,exte).draw_rectangle(25,0,25,35,img.dimy()-1,35,exte);
118 }
119 img.distance_hamilton(10,0,0.1f);
121 // Compute corresponding surface triangularization by the marching cube algorithm (isovalue 0)
122 CImg<> points0;
123 CImgList<unsigned int> faces0;
124 if (both) points0 = img.get_isovalue3d(faces0,0);
125 const CImgList<unsigned char> colors0(faces0.size,CImg<unsigned char>::vector(100,200,255));
126 const CImgList<> opacities0(faces0.size,1,1,1,1,0.2f);
128 // Perform MCF evolution
129 CImgDisplay disp(256,256,"",1), disp3d(512,512,"",0);
130 float alpha = 0, beta = 0;
131 for (unsigned int iter=0; !disp.is_closed && !disp3d.is_closed && !disp.is_keyESC && !disp3d.is_keyESC &&
132 !disp.is_keyQ && !disp3d.is_keyQ; iter++) {
133 disp.set_title("3D implicit Function (iter. %u)",iter);
134 disp3d.set_title("Mean curvature flow 3D - Isosurface (iter. %u)",iter);
136 // Apply PDE on the distance function
137 mcf_PDE(img,1,dt,narrow); // Do one iteration of mean curvature flow
138 if (!(iter%10)) img.distance_hamilton(1,narrow,0.5f); // Every 10 steps, do one iteration of distance function re-initialization
140 // Compute surface triangularization by the marching cube algorithm (isovalue 0)
141 CImgList<unsigned int> faces;
142 CImg<> points = img.get_isovalue3d(faces,0);
143 CImgList<unsigned char> colors(faces.size,CImg<unsigned char>::vector(200,128,100));
144 CImgList<> opacities(faces.size,CImg<>::vector(1.0f));
145 const float fact = 3*cimg::max(disp3d.dimx(),disp3d.dimy())/(4.0f*cimg::max(img.dimx(),img.dimy()));
147 // Append initial object if necessary.
148 if (both) {
149 points.append_object3d(faces,points0,faces0);
150 colors.insert(colors0);
151 opacities.insert(opacities0);
152 }
154 // center and rescale the objects
155 cimg_forX(points,l) {
156 points(l,0)=(points(l,0)-img.dimx()/2)*fact;
157 points(l,1)=(points(l,1)-img.dimy()/2)*fact;
158 points(l,2)=(points(l,2)-img.dimz()/2)*fact;
159 }
161 // Display 3D object on the display window.
162 CImg<unsigned char> visu(disp3d.dimx(),disp3d.dimy(),1,3,0);
163 const CImg<> rot = CImg<>::rotation_matrix(1,0,0,(beta+=0.01f))*CImg<>::rotation_matrix(0,1,1,(alpha+=0.05f));
164 if (points.size()) {
165 visu.draw_object3d(visu.dimx()/2.0f,visu.dimy()/2.0f,0.0f,
166 rot*points,faces,colors,opacities,3,
167 false,500.0,0.0f,0.0f,-8000.0f).display(disp3d);
168 } else visu.fill(0).display(disp3d);
169 img.display(disp.wait(20));
171 if ((disp3d.button || disp3d.key) && points.size()) {
172 unsigned char white[3]={ 255,255,255 };
173 visu.fill(0).draw_text(10,10,"Time stopped, press any key to start again",white).
174 display_object3d(disp3d,points,faces,colors,opacities,true,4,3,false,500,0.4f,0.3f);
175 disp3d.key = 0;
176 }
177 if (disp.is_resized) disp.resize(false);
178 if (disp3d.is_resized) disp3d.resize(false);
179 }
181 // Exit
182 return 0;
183 }