Austin Schuh | 8c794d5 | 2019-03-03 21:17:37 -0800 | [diff] [blame] | 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.eu ) |
| 10 | # |
| 11 | # Copyright : David Tschumperle |
| 12 | # ( http://tschumperle.users.greyc.fr/ ) |
| 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 | */ |
| 44 | |
| 45 | #include "CImg.h" |
| 46 | using namespace cimg_library; |
| 47 | #undef min |
| 48 | #undef max |
| 49 | |
| 50 | // Apply the Mean curvature flow PDE |
| 51 | //----------------------------------- |
| 52 | template<typename T> CImg<T>& mcf_PDE(CImg<T>& img, const unsigned int nb_iterations, |
| 53 | const float dt=0.25f, const float narrow=4.0f) { |
| 54 | CImg<float> velocity(img.width(),img.height(),img.depth(),img.spectrum()); |
| 55 | CImg_3x3x3(I,float); |
| 56 | for (unsigned int iteration = 0; iteration<nb_iterations; ++iteration) { |
| 57 | float *ptrd = velocity.data(), veloc_max = 0; |
| 58 | cimg_for3x3x3(img,x,y,z,0,I,float) if (cimg::abs(Iccc)<narrow) { |
| 59 | const float |
| 60 | ix = (Incc - Ipcc)/2, |
| 61 | iy = (Icnc - Icpc)/2, |
| 62 | iz = (Iccn - Iccp)/2, |
| 63 | norm = (float)std::sqrt(1e-5f + ix*ix + iy*iy + iz*iz), |
| 64 | ixx = Incc + Ipcc - 2*Iccc, |
| 65 | ixy = (Ippc + Innc - Inpc - Ipnc)/4, |
| 66 | ixz = (Ipcp + Incn - Incp - Ipcn)/4, |
| 67 | iyy = Icnc + Icpc - 2*Iccc, |
| 68 | iyz = (Icpp + Icnn - Icnp - Icpn)/4, |
| 69 | izz = Iccn + Iccp - 2*Iccc, |
| 70 | a = ix/norm, |
| 71 | b = iy/norm, |
| 72 | c = iz/norm, |
| 73 | inn = a*a*ixx + b*b*iyy + c*c*izz + 2*a*b*ixy + 2*a*c*ixz + 2*b*c*iyz, |
| 74 | veloc = ixx + iyy + izz - inn; |
| 75 | *(ptrd++) = veloc; |
| 76 | if (veloc>veloc_max) veloc_max = veloc; else if (-veloc>veloc_max) veloc_max = -veloc; |
| 77 | } else *(ptrd++) = 0; |
| 78 | if (veloc_max>0) img+=(velocity*=dt/veloc_max); |
| 79 | } |
| 80 | return img; |
| 81 | } |
| 82 | |
| 83 | /*---------------------- |
| 84 | |
| 85 | Main procedure |
| 86 | |
| 87 | --------------------*/ |
| 88 | int main(int argc,char **argv) { |
| 89 | cimg_usage("Mean curvature flow of a surface, using 3D level sets"); |
| 90 | const char *file_i = cimg_option("-i",(char*)0,"Input image"); |
| 91 | const float dt = cimg_option("-dt",0.05f,"PDE Time step"); |
| 92 | const float narrow = cimg_option("-band",5.0f,"Size of the narrow band"); |
| 93 | const bool both = cimg_option("-both",false,"Show both evolving and initial surface"); |
| 94 | |
| 95 | // Define the signed distance map of the initial surface. |
| 96 | CImg<> img; |
| 97 | if (file_i) { |
| 98 | const float sigma = cimg_option("-sigma",1.2f,"Segmentation regularity"); |
| 99 | const float alpha = cimg_option("-alpha",5.0f,"Region growing tolerance"); |
| 100 | img.load(file_i).channel(0); |
| 101 | CImg<int> s; |
| 102 | CImgDisplay disp(img,"Please select a starting point"); |
| 103 | while (!s || s[0]<0) s = img.get_select(0,disp); |
| 104 | CImg<> region; |
| 105 | float tmp[] = { 0 }; |
| 106 | img.draw_fill(s[0],s[1],s[2],tmp,1,region,alpha); |
| 107 | ((img = region.normalize(-1,1))*=-1).blur(sigma); |
| 108 | } |
| 109 | else { // Create synthetic implicit function |
| 110 | img.assign(60,60,60); |
| 111 | const float exte[] = { 1 }, inte[] = { -1 }; |
| 112 | img.fill(*exte).draw_rectangle(15,15,15,45,45,45,inte).draw_rectangle(25,25,0,35,35,img.depth() - 1,exte). |
| 113 | draw_rectangle(0,25,25,img.width() - 1,35,35,exte).draw_rectangle(25,0,25,35,img.height() - 1,35,exte).noise(0.7); |
| 114 | } |
| 115 | img.distance_eikonal(10,0,0.1f); |
| 116 | |
| 117 | // Compute corresponding surface triangularization by the marching cube algorithm (isovalue 0). |
| 118 | CImg<> points0; |
| 119 | CImgList<unsigned int> faces0; |
| 120 | if (both) points0 = img.get_isosurface3d(faces0,0); |
| 121 | const CImgList<unsigned char> colors0(faces0.size(),CImg<unsigned char>::vector(100,200,255)); |
| 122 | const CImgList<> opacities0(faces0.size(),1,1,1,1,0.2f); |
| 123 | |
| 124 | // Perform MCF evolution. |
| 125 | CImgDisplay disp(256,256,0,1), disp3d(512,512,0,0); |
| 126 | float alpha = 0, beta = 0; |
| 127 | for (unsigned int iteration = 0; !disp.is_closed() && !disp3d.is_closed() && |
| 128 | !disp.is_keyESC() && !disp3d.is_keyESC() && !disp.is_keyQ() && !disp3d.is_keyQ(); ++iteration) { |
| 129 | disp.set_title("3D implicit Function (iter. %u)",iteration); |
| 130 | disp3d.set_title("Mean curvature flow 3D - Isosurface (iter. %u)",iteration); |
| 131 | |
| 132 | // Apply PDE on the distance function. |
| 133 | mcf_PDE(img,1,dt,narrow); // Do one iteration of mean curvature flow |
| 134 | // Every 10 steps, do one iteration of distance function re-initialization. |
| 135 | if (!(iteration%10)) img.distance_eikonal(1,narrow,0.5f); |
| 136 | |
| 137 | // Compute surface triangularization by the marching cube algorithm (isovalue 0) |
| 138 | CImgList<unsigned int> faces; |
| 139 | CImg<> points = img.get_isosurface3d(faces,0); |
| 140 | CImgList<unsigned char> colors(faces.size(),CImg<unsigned char>::vector(200,128,100)); |
| 141 | CImgList<> opacities(faces.size(),CImg<>::vector(1.0f)); |
| 142 | const float fact = 3*std::max(disp3d.width(),disp3d.height())/(4.0f*std::max(img.width(),img.height())); |
| 143 | |
| 144 | // Append initial object if necessary. |
| 145 | if (both) { |
| 146 | points.append_object3d(faces,points0,faces0); |
| 147 | colors.insert(colors0); |
| 148 | opacities.insert(opacities0); |
| 149 | } |
| 150 | |
| 151 | // Center and rescale the objects |
| 152 | cimg_forX(points,l) { |
| 153 | points(l,0)=(points(l,0) - img.width()/2)*fact; |
| 154 | points(l,1)=(points(l,1) - img.height()/2)*fact; |
| 155 | points(l,2)=(points(l,2) - img.depth()/2)*fact; |
| 156 | } |
| 157 | |
| 158 | // Display 3D object on the display window. |
| 159 | CImg<unsigned char> visu(disp3d.width(),disp3d.height(),1,3,0); |
| 160 | const CImg<> rot = CImg<>::rotation_matrix(1,0,0,(beta+=0.5f))*CImg<>::rotation_matrix(0,1,1,(alpha+=3)); |
| 161 | if (points.size()) { |
| 162 | visu.draw_object3d(visu.width()/2.0f,visu.height()/2.0f,0.0f, |
| 163 | rot*points,faces,colors,opacities,3, |
| 164 | false,500.0,0.0f,0.0f,-8000.0f).display(disp3d); |
| 165 | } else visu.fill(0).display(disp3d); |
| 166 | img.display(disp.wait(20)); |
| 167 | |
| 168 | if ((disp3d.button() || disp3d.key()) && points.size() && !disp3d.is_keyESC() && !disp3d.is_keyQ()) { |
| 169 | const unsigned char white[3] = { 255, 255, 255 }; |
| 170 | visu.fill(0).draw_text(10,10,"Time stopped, press any key to start again",white). |
| 171 | display_object3d(disp3d,points,faces,colors,opacities,true,4,3,false,500,0,0,-5000,0.4f,0.3f); |
| 172 | disp3d.set_key(); |
| 173 | } |
| 174 | if (disp.is_resized()) disp.resize(false); |
| 175 | if (disp3d.is_resized()) disp3d.resize(false); |
| 176 | disp.wait(50); |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |