1.1 diff -r 5edfbd3e7a46 -r 1204ebf9340d PTdecode/CImg-1.3.0/examples/mcf_levelsets.cpp 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/PTdecode/CImg-1.3.0/examples/mcf_levelsets.cpp Mon Aug 03 14:09:20 2009 +0100 1.4 @@ -0,0 +1,124 @@ 1.5 +/* 1.6 + # 1.7 + # File : mcf_levelsets.cpp 1.8 + # ( C++ source file ) 1.9 + # 1.10 + # Description : Implementation of the Mean Curvature Flow (classical 2d curve evolution), 1.11 + # using the framework of Level Sets. 1.12 + # This file is a part of the CImg Library project. 1.13 + # ( http://cimg.sourceforge.net ) 1.14 + # 1.15 + # Copyright : David Tschumperle 1.16 + # ( http://www.greyc.ensicaen.fr/~dtschump/ ) 1.17 + # 1.18 + # License : CeCILL v2.0 1.19 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.20 + # 1.21 + # This software is governed by the CeCILL license under French law and 1.22 + # abiding by the rules of distribution of free software. You can use, 1.23 + # modify and/ or redistribute the software under the terms of the CeCILL 1.24 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.25 + # "http://www.cecill.info". 1.26 + # 1.27 + # As a counterpart to the access to the source code and rights to copy, 1.28 + # modify and redistribute granted by the license, users are provided only 1.29 + # with a limited warranty and the software's author, the holder of the 1.30 + # economic rights, and the successive licensors have only limited 1.31 + # liability. 1.32 + # 1.33 + # In this respect, the user's attention is drawn to the risks associated 1.34 + # with loading, using, modifying and/or developing or reproducing the 1.35 + # software by the user in light of its specific status of free software, 1.36 + # that may mean that it is complicated to manipulate, and that also 1.37 + # therefore means that it is reserved for developers and experienced 1.38 + # professionals having in-depth computer knowledge. Users are therefore 1.39 + # encouraged to load and test the software's suitability as regards their 1.40 + # requirements in conditions enabling the security of their systems and/or 1.41 + # data to be ensured and, more generally, to use and operate it in the 1.42 + # same conditions as regards security. 1.43 + # 1.44 + # The fact that you are presently reading this means that you have had 1.45 + # knowledge of the CeCILL license and that you accept its terms. 1.46 + # 1.47 +*/ 1.48 + 1.49 +#include "CImg.h" 1.50 +using namespace cimg_library; 1.51 + 1.52 +// The lines below are necessary when using a non-standard compiler as visualcpp6. 1.53 +#ifdef cimg_use_visualcpp6 1.54 +#define std 1.55 +#endif 1.56 +#ifdef min 1.57 +#undef min 1.58 +#undef max 1.59 +#endif 1.60 + 1.61 +// get_level0() : Retrieve the curve corresponding to the zero level set of the distance function 1.62 +//------------- 1.63 +CImg<unsigned char> get_level0(const CImg<>& img) { 1.64 + CImg<unsigned char> dest(img); 1.65 + CImg_2x2(I,float); Inn = 0; 1.66 + cimg_for2x2(img,x,y,0,0,I) if (Icc*Inc<0 || Icc*Icn<0) dest(x,y) = 255; else dest(x,y) = Icc<0?100:0; 1.67 + return dest; 1.68 +} 1.69 + 1.70 +//----------------- 1.71 +// Main procedure 1.72 +//----------------- 1.73 +int main(int argc,char **argv) { 1.74 + cimg_usage("Perform a Mean Curvature Flow on closed curves, using Level Sets"); 1.75 + const float dt = cimg_option("-dt",0.8f,"PDE time step"); 1.76 + const unsigned int nb_iter = cimg_option("-iter",10000,"Number of iterations"); 1.77 + 1.78 + // Create a user-defined closed curve 1.79 + CImg<unsigned char> curve(256,256,1,2,0); 1.80 + unsigned char col1[2]={0,255}, col2[2]={200,255}, col3[2]={255,255}; 1.81 + curve.draw_grid(20,20,0,0,false,false,col1,0.4f,0xCCCCCCCC,0xCCCCCCCC). 1.82 + draw_text(5,5,"Please draw your curve\nin this window\n(Use your mouse)",col1); 1.83 + CImgDisplay disp(curve,"Mean curvature flow",0); 1.84 + int xo=-1,yo=-1,x0=-1,y0=-1,x1=-1,y1=-1; 1.85 + while (!disp.is_closed && (x0<0 || disp.button)) { 1.86 + if (disp.button && disp.mouse_x>=0 && disp.mouse_y>=0) { 1.87 + if (x0<0) { xo = x0 = disp.mouse_x; yo = y0 = disp.mouse_y; } else { 1.88 + x1 = disp.mouse_x; y1 = disp.mouse_y; 1.89 + curve.draw_line(x0,y0,x1,y1,col2).display(disp); 1.90 + x0 = x1; y0 = y1; 1.91 + } 1.92 + } 1.93 + disp.wait(); 1.94 + if (disp.is_resized) disp.resize(disp); 1.95 + } 1.96 + curve.draw_line(x1,y1,xo,yo,col2).channel(0).draw_fill(0,0,col3); 1.97 + CImg<> img = CImg<>(curve.get_shared_channel(0)).normalize(-1,1); 1.98 + 1.99 + // Perform the "Mean Curvature Flow" 1.100 + img.distance_hamilton(10); 1.101 + CImg_3x3(I,float); 1.102 + for (unsigned int iter=0; iter<nb_iter && !disp.is_closed && !disp.is_keyQ; iter++) { 1.103 + CImg<> veloc(img.dimx(),img.dimy(),img.dimz(),img.dimv()); 1.104 + cimg_for3x3(img,x,y,0,0,I) { 1.105 + const float 1.106 + ix = 0.5f*(Inc-Ipc), 1.107 + iy = 0.5f*(Icn-Icp), 1.108 + ixx = Inc+Ipc-2*Icc, 1.109 + iyy = Icn+Icp-2*Icc, 1.110 + ixy = 0.25f*(Ipp+Inn-Inp-Ipn), 1.111 + ngrad = ix*ix+iy*iy, 1.112 + iee = (ngrad>1e-5)?(( iy*iy*ixx - 2*ix*iy*ixy + ix*ix*iyy )/ngrad):0; 1.113 + veloc(x,y) = iee; 1.114 + } 1.115 + float m, M = veloc.maxmin(m); 1.116 + const double xdt = dt/cimg::max(cimg::abs(m),cimg::abs(M)); 1.117 + img+=xdt*veloc; 1.118 + if (!(iter%10)) { 1.119 + get_level0(img).resize(disp.dimx(),disp.dimy()).draw_grid(20,20,0,0,false,false,col3,0.4f,0xCCCCCCCC,0xCCCCCCCC). 1.120 + draw_text(5,5,"Iteration %d",col3,0,1,11,iter).display(disp); 1.121 + } 1.122 + if (!(iter%30)) img.distance_hamilton(1,3); 1.123 + if (disp.is_resized) disp.resize(); 1.124 + } 1.125 + 1.126 + // End of program 1.127 + return 0; 1.128 +}