Austin Schuh | 8c794d5 | 2019-03-03 21:17:37 -0800 | [diff] [blame^] | 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.eu ) |
| 9 | # |
| 10 | # Copyright : David Tschumperle |
| 11 | # ( http://tschumperle.users.greyc.fr/ ) |
| 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 | */ |
| 43 | |
| 44 | // Include library header file |
| 45 | #include "CImg.h" |
| 46 | #ifndef cimg_imagepath |
| 47 | #define cimg_imagepath "img/" |
| 48 | #endif |
| 49 | #undef min |
| 50 | #undef max |
| 51 | |
| 52 | // Make a simpler namespace alias if one wants to avoid 'using namespace cimg_library' |
| 53 | namespace cil = cimg_library; |
| 54 | |
| 55 | // Main procedure |
| 56 | //---------------- |
| 57 | int main(int argc,char **argv) { |
| 58 | |
| 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_width() - disp.width() - profile.width())/2, |
| 70 | (cil::CImgDisplay::screen_height() - disp.height())/2); |
| 71 | |
| 72 | profile.move(disp.window_x() + 8 + disp.window_width(), disp.window_y()); |
| 73 | const float white[] = { 255,255,255 }; |
| 74 | bool run_PDE = true; |
| 75 | |
| 76 | // Begin PDE iteration loop |
| 77 | //------------------------- |
| 78 | for (int iter = 0; !disp.is_closed() && !profile.is_closed() && |
| 79 | !disp.is_keyQ() && !disp.is_keyESC() && !profile.is_keyQ() && !profile.is_keyESC();) { |
| 80 | |
| 81 | // Compute one iteration of PDE explicit scheme |
| 82 | if (run_PDE) { |
| 83 | CImg_3x3(I,float); |
| 84 | cimg_forC(img,k) cimg_for3x3(img,x,y,0,k,I,float) veloc(x,y,k) = Inc + Ipc + Icn + Icp - 4*Icc; |
| 85 | float m, M = veloc.max_min(m); |
| 86 | const double xdt = dt/(M - m); |
| 87 | img += veloc*xdt; |
| 88 | cil::CImg<>(img).draw_text(2,2,"iter = %d",white,0,1,13,iter).display(disp.wait(25)); |
| 89 | } |
| 90 | |
| 91 | // Plot (R,G,B) intensity profiles and display it |
| 92 | if (disp.mouse_x()>=0) { |
| 93 | const int |
| 94 | mx = disp.mouse_x(), my = disp.mouse_y(), |
| 95 | mnx = mx*profile.width()/disp.width(); |
| 96 | const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 }, white[] = { 255,255,255 }; |
| 97 | cil::CImg<unsigned char>(profile.width(),profile.height(),1,3,0). |
| 98 | draw_graph(img.get_shared_row(my,0,0),red,1,1,0,255,0). |
| 99 | draw_graph(img.get_shared_row(my,0,1),green,1,1,0,255,0). |
| 100 | draw_graph(img.get_shared_row(my,0,2),blue,1,1,0,255,0). |
| 101 | draw_line(mnx,0,mnx,profile.height() - 1,white,0.5f,cil::cimg::rol(0xFF00FF00,iter%32)). |
| 102 | draw_text(2,2,"(x,y)=(%d,%d)",white,0,1,13,mx,my). |
| 103 | display(profile); |
| 104 | } |
| 105 | |
| 106 | // Mouse button stops/starts PDE evolution. |
| 107 | if (disp.button() || profile.button()) { disp.set_button(); profile.set_button(); run_PDE = !run_PDE; } |
| 108 | profile.resize(); |
| 109 | disp.resize(disp); |
| 110 | if (run_PDE) ++iter; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |