Mon, 03 Aug 2009 14:21:23 +0100
added keepme for ptdecode obj directory
1 /*
2 #
3 # File : mcf_levelsets.cpp
4 # ( C++ source file )
5 #
6 # Description : Implementation of the Mean Curvature Flow (classical 2d curve evolution),
7 # using the framework of Level Sets.
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 // get_level0() : Retrieve the curve corresponding to the zero level set of the distance function
58 //-------------
59 CImg<unsigned char> get_level0(const CImg<>& img) {
60 CImg<unsigned char> dest(img);
61 CImg_2x2(I,float); Inn = 0;
62 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;
63 return dest;
64 }
66 //-----------------
67 // Main procedure
68 //-----------------
69 int main(int argc,char **argv) {
70 cimg_usage("Perform a Mean Curvature Flow on closed curves, using Level Sets");
71 const float dt = cimg_option("-dt",0.8f,"PDE time step");
72 const unsigned int nb_iter = cimg_option("-iter",10000,"Number of iterations");
74 // Create a user-defined closed curve
75 CImg<unsigned char> curve(256,256,1,2,0);
76 unsigned char col1[2]={0,255}, col2[2]={200,255}, col3[2]={255,255};
77 curve.draw_grid(20,20,0,0,false,false,col1,0.4f,0xCCCCCCCC,0xCCCCCCCC).
78 draw_text(5,5,"Please draw your curve\nin this window\n(Use your mouse)",col1);
79 CImgDisplay disp(curve,"Mean curvature flow",0);
80 int xo=-1,yo=-1,x0=-1,y0=-1,x1=-1,y1=-1;
81 while (!disp.is_closed && (x0<0 || disp.button)) {
82 if (disp.button && disp.mouse_x>=0 && disp.mouse_y>=0) {
83 if (x0<0) { xo = x0 = disp.mouse_x; yo = y0 = disp.mouse_y; } else {
84 x1 = disp.mouse_x; y1 = disp.mouse_y;
85 curve.draw_line(x0,y0,x1,y1,col2).display(disp);
86 x0 = x1; y0 = y1;
87 }
88 }
89 disp.wait();
90 if (disp.is_resized) disp.resize(disp);
91 }
92 curve.draw_line(x1,y1,xo,yo,col2).channel(0).draw_fill(0,0,col3);
93 CImg<> img = CImg<>(curve.get_shared_channel(0)).normalize(-1,1);
95 // Perform the "Mean Curvature Flow"
96 img.distance_hamilton(10);
97 CImg_3x3(I,float);
98 for (unsigned int iter=0; iter<nb_iter && !disp.is_closed && !disp.is_keyQ; iter++) {
99 CImg<> veloc(img.dimx(),img.dimy(),img.dimz(),img.dimv());
100 cimg_for3x3(img,x,y,0,0,I) {
101 const float
102 ix = 0.5f*(Inc-Ipc),
103 iy = 0.5f*(Icn-Icp),
104 ixx = Inc+Ipc-2*Icc,
105 iyy = Icn+Icp-2*Icc,
106 ixy = 0.25f*(Ipp+Inn-Inp-Ipn),
107 ngrad = ix*ix+iy*iy,
108 iee = (ngrad>1e-5)?(( iy*iy*ixx - 2*ix*iy*ixy + ix*ix*iyy )/ngrad):0;
109 veloc(x,y) = iee;
110 }
111 float m, M = veloc.maxmin(m);
112 const double xdt = dt/cimg::max(cimg::abs(m),cimg::abs(M));
113 img+=xdt*veloc;
114 if (!(iter%10)) {
115 get_level0(img).resize(disp.dimx(),disp.dimy()).draw_grid(20,20,0,0,false,false,col3,0.4f,0xCCCCCCCC,0xCCCCCCCC).
116 draw_text(5,5,"Iteration %d",col3,0,1,11,iter).display(disp);
117 }
118 if (!(iter%30)) img.distance_hamilton(1,3);
119 if (disp.is_resized) disp.resize();
120 }
122 // End of program
123 return 0;
124 }