Thu, 24 Sep 2009 17:18:28 +0100
Added support for separator ticks between labels
1 /*
2 #
3 # File : pde_heatflow2D.cpp
4 # ( C++ source file )
5 #
6 # Description : A simple Heat flow on 2D images.
7 # This file is a part of the CImg Library project.
8 # ( http://cimg.sourceforge.net )
9 #
10 # Copyright : David Tschumperle
11 # ( http://www.greyc.ensicaen.fr/~dtschump/ )
12 #
13 # License : CeCILL v2.0
14 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
15 #
16 # This software is governed by the CeCILL license under French law and
17 # abiding by the rules of distribution of free software. You can use,
18 # modify and/ or redistribute the software under the terms of the CeCILL
19 # license as circulated by CEA, CNRS and INRIA at the following URL
20 # "http://www.cecill.info".
21 #
22 # As a counterpart to the access to the source code and rights to copy,
23 # modify and redistribute granted by the license, users are provided only
24 # with a limited warranty and the software's author, the holder of the
25 # economic rights, and the successive licensors have only limited
26 # liability.
27 #
28 # In this respect, the user's attention is drawn to the risks associated
29 # with loading, using, modifying and/or developing or reproducing the
30 # software by the user in light of its specific status of free software,
31 # that may mean that it is complicated to manipulate, and that also
32 # therefore means that it is reserved for developers and experienced
33 # professionals having in-depth computer knowledge. Users are therefore
34 # encouraged to load and test the software's suitability as regards their
35 # requirements in conditions enabling the security of their systems and/or
36 # data to be ensured and, more generally, to use and operate it in the
37 # same conditions as regards security.
38 #
39 # The fact that you are presently reading this means that you have had
40 # knowledge of the CeCILL license and that you accept its terms.
41 #
42 */
44 // Include library header file
45 #include "CImg.h"
47 // Make a simpler namespace alias if one wants to avoid 'using namespace cimg_library'
48 namespace cil = cimg_library;
50 #ifndef cimg_imagepath
51 #define cimg_imagepath "img/"
52 #endif
54 //---------------------
55 // Begin main procedure
56 //---------------------
57 int main(int argc,char **argv) {
59 // Read command line arguments, and init images and displays
60 //-----------------------------------------------------------
61 cimg_usage("Perform a simple Heat Flow on 2D images");
62 cil::CImg<> img(cimg_option("-i",cimg_imagepath "milla.bmp","Input image")), veloc(img);
63 const double dt = cimg_option("-dt",3.0,"Adapting time step");
64 img.
65 noise(cimg_option("-ng",0.0,"Add gaussian noise"),0).
66 noise(cimg_option("-nu",0.0,"Add uniform noise"),1).
67 noise(cimg_option("-ns",0.0,"Add Salt&Pepper noise"),2);
68 cil::CImgDisplay profile(400,300,"Intensity Profile",0,false,true), disp(img,"Heat flow 2D",0,false,true);
69 disp.move((cil::CImgDisplay::screen_dimx()-disp.dimx()-profile.dimx())/2,
70 (cil::CImgDisplay::screen_dimy()-disp.dimy())/2);
72 profile.move(disp.window_x + 8 + disp.window_width, disp.window_y);
73 CImg_3x3(I,float);
74 float white[] = { 255,255,255 };
75 bool run_PDE = true;
77 // Begin PDE iteration loop
78 //-------------------------
79 for (int iter=0; !disp.is_closed && !profile.is_closed && !disp.is_keyQ && !disp.is_keyESC && !profile.is_keyQ && !profile.is_keyESC;) {
81 // Compute one iteration of PDE explicit scheme
82 if (run_PDE) {
83 cimg_forV(img,k) cimg_for3x3(img,x,y,0,k,I) veloc(x,y,k) = Inc + Ipc + Icn + Icp - 4*Icc;
84 float m, M = veloc.maxmin(m);
85 const double xdt = dt/(M-m);
86 img += veloc*xdt;
87 cil::CImg<>(img).draw_text(2,2,"iter = %d",white,0,1,13,iter).display(disp.wait(25));
88 }
90 // Plot (R,G,B) intensity profiles and display it
91 if (disp.mouse_x>=0) {
92 const int
93 mx = disp.mouse_x, my = disp.mouse_y,
94 mnx = mx*profile.dimx()/disp.dimx();
95 const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 }, white[] = { 255,255,255 };
96 cil::CImg<unsigned char>(profile.dimx(),profile.dimy(),1,3,0).
97 draw_graph(img.get_shared_line(my,0,0),red,1,1,0,255,0).
98 draw_graph(img.get_shared_line(my,0,1),green,1,1,0,255,0).
99 draw_graph(img.get_shared_line(my,0,2),blue,1,1,0,255,0).
100 draw_line(mnx,0,mnx,profile.dimy()-1,white,0.5f,cil::cimg::rol(0xFF00FF00,iter%32)).
101 draw_text(2,2,"(x,y)=(%d,%d)",white,0,1,13,mx,my).
102 display(profile);
103 }
105 // Mouse button stops/starts PDE evolution.
106 if (disp.button || profile.button) { disp.button = profile.button = 0; run_PDE = !run_PDE; }
107 profile.resize();
108 disp.resize(disp);
109 if (run_PDE) ++iter;
110 }
112 return 0;
113 }