1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/PTdecode/CImg-1.3.0/examples/mcf_levelsets3d.cpp Mon Aug 03 14:09:20 2009 +0100 1.3 @@ -0,0 +1,183 @@ 1.4 +/* 1.5 + # 1.6 + # File : mcf_levelsets3d.cpp 1.7 + # ( C++ source file ) 1.8 + # 1.9 + # Description : Implementation of the Mean Curvature Flow on Surfaces 1.10 + # using the framework of Level Sets 3D. 1.11 + # This file is a part of the CImg Library project. 1.12 + # ( http://cimg.sourceforge.net ) 1.13 + # 1.14 + # Copyright : David Tschumperle 1.15 + # ( http://www.greyc.ensicaen.fr/~dtschump/ ) 1.16 + # 1.17 + # License : CeCILL v2.0 1.18 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.19 + # 1.20 + # This software is governed by the CeCILL license under French law and 1.21 + # abiding by the rules of distribution of free software. You can use, 1.22 + # modify and/ or redistribute the software under the terms of the CeCILL 1.23 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.24 + # "http://www.cecill.info". 1.25 + # 1.26 + # As a counterpart to the access to the source code and rights to copy, 1.27 + # modify and redistribute granted by the license, users are provided only 1.28 + # with a limited warranty and the software's author, the holder of the 1.29 + # economic rights, and the successive licensors have only limited 1.30 + # liability. 1.31 + # 1.32 + # In this respect, the user's attention is drawn to the risks associated 1.33 + # with loading, using, modifying and/or developing or reproducing the 1.34 + # software by the user in light of its specific status of free software, 1.35 + # that may mean that it is complicated to manipulate, and that also 1.36 + # therefore means that it is reserved for developers and experienced 1.37 + # professionals having in-depth computer knowledge. Users are therefore 1.38 + # encouraged to load and test the software's suitability as regards their 1.39 + # requirements in conditions enabling the security of their systems and/or 1.40 + # data to be ensured and, more generally, to use and operate it in the 1.41 + # same conditions as regards security. 1.42 + # 1.43 + # The fact that you are presently reading this means that you have had 1.44 + # knowledge of the CeCILL license and that you accept its terms. 1.45 + # 1.46 +*/ 1.47 + 1.48 +#include "CImg.h" 1.49 +using namespace cimg_library; 1.50 + 1.51 +// The lines below are necessary when using a non-standard compiler as visualcpp6. 1.52 +#ifdef cimg_use_visualcpp6 1.53 +#define std 1.54 +#endif 1.55 +#ifdef min 1.56 +#undef min 1.57 +#undef max 1.58 +#endif 1.59 + 1.60 +// Apply the Mean curvature flow PDE 1.61 +//----------------------------------- 1.62 +template<typename T> CImg<T>& mcf_PDE(CImg<T>& img, const unsigned int nb_iter, 1.63 + const float dt=0.25f, const float narrow=4.0f) { 1.64 + CImg<T> veloc(img.dimx(),img.dimy(),img.dimz(),img.dimv()); 1.65 + CImg_3x3x3(I,float); 1.66 + for (unsigned int iter=0; iter<nb_iter; iter++) { 1.67 + cimg_for3x3x3(img,x,y,z,0,I) if (cimg::abs(Iccc)<narrow) { 1.68 + const float 1.69 + ix = 0.5f*(Incc-Ipcc), 1.70 + iy = 0.5f*(Icnc-Icpc), 1.71 + iz = 0.5f*(Iccn-Iccp), 1.72 + norm = (float)std::sqrt(1e-5f+ix*ix+iy*iy+iz*iz), 1.73 + ixx = Incc+Ipcc-2*Iccc, 1.74 + ixy = 0.25f*(Ippc+Innc-Inpc-Ipnc), 1.75 + ixz = 0.25f*(Ipcp+Incn-Incp-Ipcn), 1.76 + iyy = Icnc+Icpc-2*Iccc, 1.77 + iyz = 0.25f*(Icpp+Icnn-Icnp-Icpn), 1.78 + izz = Iccn+Iccp-2*Iccc, 1.79 + a = ix/norm, 1.80 + b = iy/norm, 1.81 + c = iz/norm, 1.82 + inn = a*a*ixx + b*b*iyy + c*c*izz + 2*a*b*ixy + 2*a*c*ixz + 2*b*c*iyz; 1.83 + veloc(x,y,z) = ixx+iyy+izz-inn; 1.84 + } else veloc(x,y,z) = 0; 1.85 + float m, M = veloc.maxmin(m); 1.86 + const double xdt = dt/cimg::max(cimg::abs(m),cimg::abs(M)); 1.87 + img+=xdt*veloc; 1.88 + } 1.89 + return img; 1.90 +} 1.91 + 1.92 +// Main procedure 1.93 +//---------------- 1.94 +int main(int argc,char **argv) { 1.95 + cimg_usage("Mean curvature flow of a surface, using 3D level sets"); 1.96 + const char *file_i = cimg_option("-i",(char*)0,"Input image"); 1.97 + const float dt = cimg_option("-dt",0.05f,"PDE Time step"); 1.98 + const float narrow = cimg_option("-band",5.0f,"Size of the narrow band"); 1.99 + const bool both = cimg_option("-both",false,"Show both evolving and initial surface"); 1.100 + 1.101 + // Define the signed distance map of the initial surface 1.102 + CImg<> img; 1.103 + if (file_i) { 1.104 + const float sigma = cimg_option("-sigma",1.2f,"Segmentation regularity"); 1.105 + const float alpha = cimg_option("-alpha",5.0f,"Region growing tolerance"); 1.106 + img.load(file_i).channel(0); 1.107 + CImg<int> s; 1.108 + CImgDisplay disp(img,"Please select a starting point"); 1.109 + while (!s || s[0]<0) s = img.get_select(0,disp); 1.110 + CImg<> region; 1.111 + float tmp[1] = { 0 }; 1.112 + img.draw_fill(s[0],s[1],s[2],tmp,1,region,alpha); 1.113 + ((img = region.normalize(-1,1))*=-1).blur(sigma); 1.114 + 1.115 + } 1.116 + else { // Create synthetic implicit function 1.117 + img.assign(60,60,60); 1.118 + const float exte[1]={1}, inte[1]={-1}; 1.119 + img.fill(*exte).draw_rectangle(15,15,15,45,45,45,inte).draw_rectangle(25,25,0,35,35,img.dimz()-1,exte). 1.120 + draw_rectangle(0,25,25,img.dimx()-1,35,35,exte).draw_rectangle(25,0,25,35,img.dimy()-1,35,exte); 1.121 + } 1.122 + img.distance_hamilton(10,0,0.1f); 1.123 + 1.124 + // Compute corresponding surface triangularization by the marching cube algorithm (isovalue 0) 1.125 + CImg<> points0; 1.126 + CImgList<unsigned int> faces0; 1.127 + if (both) points0 = img.get_isovalue3d(faces0,0); 1.128 + const CImgList<unsigned char> colors0(faces0.size,CImg<unsigned char>::vector(100,200,255)); 1.129 + const CImgList<> opacities0(faces0.size,1,1,1,1,0.2f); 1.130 + 1.131 + // Perform MCF evolution 1.132 + CImgDisplay disp(256,256,"",1), disp3d(512,512,"",0); 1.133 + float alpha = 0, beta = 0; 1.134 + for (unsigned int iter=0; !disp.is_closed && !disp3d.is_closed && !disp.is_keyESC && !disp3d.is_keyESC && 1.135 + !disp.is_keyQ && !disp3d.is_keyQ; iter++) { 1.136 + disp.set_title("3D implicit Function (iter. %u)",iter); 1.137 + disp3d.set_title("Mean curvature flow 3D - Isosurface (iter. %u)",iter); 1.138 + 1.139 + // Apply PDE on the distance function 1.140 + mcf_PDE(img,1,dt,narrow); // Do one iteration of mean curvature flow 1.141 + if (!(iter%10)) img.distance_hamilton(1,narrow,0.5f); // Every 10 steps, do one iteration of distance function re-initialization 1.142 + 1.143 + // Compute surface triangularization by the marching cube algorithm (isovalue 0) 1.144 + CImgList<unsigned int> faces; 1.145 + CImg<> points = img.get_isovalue3d(faces,0); 1.146 + CImgList<unsigned char> colors(faces.size,CImg<unsigned char>::vector(200,128,100)); 1.147 + CImgList<> opacities(faces.size,CImg<>::vector(1.0f)); 1.148 + const float fact = 3*cimg::max(disp3d.dimx(),disp3d.dimy())/(4.0f*cimg::max(img.dimx(),img.dimy())); 1.149 + 1.150 + // Append initial object if necessary. 1.151 + if (both) { 1.152 + points.append_object3d(faces,points0,faces0); 1.153 + colors.insert(colors0); 1.154 + opacities.insert(opacities0); 1.155 + } 1.156 + 1.157 + // center and rescale the objects 1.158 + cimg_forX(points,l) { 1.159 + points(l,0)=(points(l,0)-img.dimx()/2)*fact; 1.160 + points(l,1)=(points(l,1)-img.dimy()/2)*fact; 1.161 + points(l,2)=(points(l,2)-img.dimz()/2)*fact; 1.162 + } 1.163 + 1.164 + // Display 3D object on the display window. 1.165 + CImg<unsigned char> visu(disp3d.dimx(),disp3d.dimy(),1,3,0); 1.166 + const CImg<> rot = CImg<>::rotation_matrix(1,0,0,(beta+=0.01f))*CImg<>::rotation_matrix(0,1,1,(alpha+=0.05f)); 1.167 + if (points.size()) { 1.168 + visu.draw_object3d(visu.dimx()/2.0f,visu.dimy()/2.0f,0.0f, 1.169 + rot*points,faces,colors,opacities,3, 1.170 + false,500.0,0.0f,0.0f,-8000.0f).display(disp3d); 1.171 + } else visu.fill(0).display(disp3d); 1.172 + img.display(disp.wait(20)); 1.173 + 1.174 + if ((disp3d.button || disp3d.key) && points.size()) { 1.175 + unsigned char white[3]={ 255,255,255 }; 1.176 + visu.fill(0).draw_text(10,10,"Time stopped, press any key to start again",white). 1.177 + display_object3d(disp3d,points,faces,colors,opacities,true,4,3,false,500,0.4f,0.3f); 1.178 + disp3d.key = 0; 1.179 + } 1.180 + if (disp.is_resized) disp.resize(false); 1.181 + if (disp3d.is_resized) disp3d.resize(false); 1.182 + } 1.183 + 1.184 + // Exit 1.185 + return 0; 1.186 +}