-------------------- Main procedure ----------------------*/
| 62 | |
| 63 | ----------------------*/ |
| 64 | int main(int argc,char **argv) { |
| 65 | cimg_usage("Perform a Mean Curvature Flow on closed curves, using Level Sets"); |
| 66 | const float dt = cimg_option("-dt",0.8f,"PDE time step"); |
| 67 | const unsigned int nb_iterations = cimg_option("-iter",10000,"Number of iterations"); |
| 68 | |
| 69 | // Create a user-defined closed curve. |
| 70 | CImg<unsigned char> curve(256,256,1,2,0); |
| 71 | unsigned char col1[] = {0,255}, col2[] = {200,255}, col3[] = {255,255}; |
| 72 | curve.draw_grid(20,20,0,0,false,false,col1,0.4f,0xCCCCCCCC,0xCCCCCCCC). |
| 73 | draw_text(5,5,"Please draw your curve\nin this window\n(Use your mouse)",col1); |
| 74 | CImgDisplay disp(curve,"Mean curvature flow",0); |
| 75 | int xo = -1, yo = -1, x0 = -1, y0 = -1, x1 = -1, y1 = -1; |
| 76 | while (!disp.is_closed() && (x0<0 || disp.button())) { |
| 77 | if (disp.button() && disp.mouse_x()>=0 && disp.mouse_y()>=0) { |
| 78 | if (x0<0) { xo = x0 = disp.mouse_x(); yo = y0 = disp.mouse_y(); } else { |
| 79 | x1 = disp.mouse_x(); y1 = disp.mouse_y(); |
| 80 | curve.draw_line(x0,y0,x1,y1,col2).display(disp); |
| 81 | x0 = x1; y0 = y1; |
| 82 | } |
| 83 | } |
| 84 | disp.wait(); |
| 85 | if (disp.is_resized()) disp.resize(disp); |
| 86 | } |
| 87 | curve.draw_line(x1,y1,xo,yo,col2).channel(0).draw_fill(0,0,col3); |
| 88 | CImg<> img = CImg<>(curve.get_shared_channel(0)).normalize(-1,1); |
| 89 | |
| 90 | // Perform the "Mean Curvature Flow". |
| 91 | img.distance_eikonal(10); |
| 92 | CImg_3x3(I,float); |
| 93 | for (unsigned int iteration = 0; iteration<nb_iterations && !disp.is_closed() && |
| 94 | !disp.is_keyQ() && !disp.is_keyESC(); ++iteration) { |
| 95 | CImg<float> velocity(img.width(),img.height(),img.depth(),img.spectrum()); |
| 96 | float *ptrd = velocity.data(), veloc_max = 0; |
| 97 | cimg_for3x3(img,x,y,0,0,I,float) { |
| 98 | const float |
| 99 | ix = (Inc - Ipc)/2, |
| 100 | iy = (Icn - Icp)/2, |
| 101 | ixx = Inc + Ipc - 2*Icc, |
| 102 | iyy = Icn + Icp - 2*Icc, |
| 103 | ixy = (Ipp + Inn - Inp - Ipn)/4, |
| 104 | ngrad = ix*ix + iy*iy, |
| 105 | iee = (ngrad>1e-5)?((iy*iy*ixx - 2*ix*iy*ixy + ix*ix*iyy)/ngrad):0; |
| 106 | *(ptrd++) = iee; |
| 107 | if (iee>veloc_max) veloc_max = iee; else if (-iee>veloc_max) veloc_max = -iee; |
| 108 | } |
| 109 | if (veloc_max>0) img+=(velocity*=dt/veloc_max); |
| 110 | if (!(iteration%10)) { |
| 111 | get_level0(img).resize(disp.width(),disp.height()). |
| 112 | draw_grid(20,20,0,0,false,false,col3,0.4f,0xCCCCCCCC,0xCCCCCCCC). |
| 113 | draw_text(5,5,"Iteration %d",col3,0,1,13,iteration).display(disp); |
| 114 | } |
| 115 | if (!(iteration%60)) img.distance_eikonal(1,3); |
| 116 | if (disp.is_resized()) disp.resize(); |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
nothing calls this directly
no test coverage detected