Austin Schuh | 8c794d5 | 2019-03-03 21:17:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | # |
| 3 | # File : pde_TschumperleDeriche2d.cpp |
| 4 | # ( C++ source file ) |
| 5 | # |
| 6 | # Description : Implementation of the Tschumperle-Deriche's Regularization |
| 7 | # PDE, for 2D multivalued images, as described in the articles below. |
| 8 | # This file is a part of the CImg Library project. |
| 9 | # ( http://cimg.eu ) |
| 10 | # |
| 11 | # (1) PDE-Based Regularization of Multivalued Images and Applications. |
| 12 | # (D. Tschumperle). PhD Thesis. University of Nice-Sophia Antipolis, December 2002. |
| 13 | # (2) Diffusion PDE's on Vector-valued Images : Local Approach and Geometric Viewpoint. |
| 14 | # (D. Tschumperle and R. Deriche). IEEE Signal Processing Magazine, October 2002. |
| 15 | # (3) Vector-Valued Image Regularization with PDE's : A Common Framework for Different Applications. |
| 16 | # (D. Tschumperle and R. Deriche). CVPR'2003, Computer Vision and Pattern Recognition, |
| 17 | # Madison, United States, June 2003. |
| 18 | # |
| 19 | # This code can be used to perform image restoration, inpainting, magnification or flow visualization. |
| 20 | # |
| 21 | # Copyright : David Tschumperle |
| 22 | # ( http://tschumperle.users.greyc.fr/ ) |
| 23 | # |
| 24 | # License : CeCILL v2.0 |
| 25 | # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) |
| 26 | # |
| 27 | # This software is governed by the CeCILL license under French law and |
| 28 | # abiding by the rules of distribution of free software. You can use, |
| 29 | # modify and/ or redistribute the software under the terms of the CeCILL |
| 30 | # license as circulated by CEA, CNRS and INRIA at the following URL |
| 31 | # "http://www.cecill.info". |
| 32 | # |
| 33 | # As a counterpart to the access to the source code and rights to copy, |
| 34 | # modify and redistribute granted by the license, users are provided only |
| 35 | # with a limited warranty and the software's author, the holder of the |
| 36 | # economic rights, and the successive licensors have only limited |
| 37 | # liability. |
| 38 | # |
| 39 | # In this respect, the user's attention is drawn to the risks associated |
| 40 | # with loading, using, modifying and/or developing or reproducing the |
| 41 | # software by the user in light of its specific status of free software, |
| 42 | # that may mean that it is complicated to manipulate, and that also |
| 43 | # therefore means that it is reserved for developers and experienced |
| 44 | # professionals having in-depth computer knowledge. Users are therefore |
| 45 | # encouraged to load and test the software's suitability as regards their |
| 46 | # requirements in conditions enabling the security of their systems and/or |
| 47 | # data to be ensured and, more generally, to use and operate it in the |
| 48 | # same conditions as regards security. |
| 49 | # |
| 50 | # The fact that you are presently reading this means that you have had |
| 51 | # knowledge of the CeCILL license and that you accept its terms. |
| 52 | # |
| 53 | */ |
| 54 | |
| 55 | #include "CImg.h" |
| 56 | using namespace cimg_library; |
| 57 | #ifndef cimg_imagepath |
| 58 | #define cimg_imagepath "img/" |
| 59 | #endif |
| 60 | #undef min |
| 61 | #undef max |
| 62 | |
| 63 | // Main procedure |
| 64 | //---------------- |
| 65 | int main(int argc,char **argv) { |
| 66 | |
| 67 | // Read command line arguments |
| 68 | //----------------------------- |
| 69 | cimg_usage("Tschumperle-Deriche's flow for 2D Image Restoration, Inpainting, Magnification or Flow visualization"); |
| 70 | const char *file_i = cimg_option("-i",cimg_imagepath "milla.bmp","Input image"); |
| 71 | const char *file_m = cimg_option("-m",(char*)NULL,"Mask image (if Inpainting)"); |
| 72 | const char *file_f = cimg_option("-f",(char*)NULL,"Flow image (if Flow visualization)"); |
| 73 | const char *file_o = cimg_option("-o",(char*)NULL,"Output file"); |
| 74 | const double zoom = cimg_option("-zoom",1.0,"Image magnification"); |
| 75 | |
| 76 | const unsigned int nb_iter = cimg_option("-iter",100000,"Number of iterations"); |
| 77 | const double dt = cimg_option("-dt",20.0,"Adapting time step"); |
| 78 | const double alpha = cimg_option("-alpha",0.0,"Gradient smoothing"); |
| 79 | const double sigma = cimg_option("-sigma",0.5,"Structure tensor smoothing"); |
| 80 | const float a1 = cimg_option("-a1",0.5f,"Diffusion limiter along minimal variations"); |
| 81 | const float a2 = cimg_option("-a2",0.9f,"Diffusion limiter along maximal variations"); |
| 82 | const double noiseg = cimg_option("-ng",0.0,"Add gauss noise before aplying the algorithm"); |
| 83 | const double noiseu = cimg_option("-nu",0.0,"Add uniform noise before applying the algorithm"); |
| 84 | const double noises = cimg_option("-ns",0.0,"Add salt&pepper noise before applying the algorithm"); |
| 85 | const bool stflag = cimg_option("-stats",false,"Display image statistics at each iteration"); |
| 86 | const unsigned int save = cimg_option("-save",0,"Iteration saving step"); |
| 87 | const unsigned int visu = cimg_option("-visu",10,"Visualization step (0=no visualization)"); |
| 88 | const unsigned int init = cimg_option("-init",3,"Inpainting initialization (0=black, 1=white, 2=noise, 3=unchanged)"); |
| 89 | const unsigned int skip = cimg_option("-skip",1,"Step of image geometry computation"); |
| 90 | bool view_t = cimg_option("-d",false,"View tensor directions (useful for debug)"); |
| 91 | double xdt = 0; |
| 92 | |
| 93 | // Variable initialization |
| 94 | //------------------------- |
| 95 | CImg<> img, flow; |
| 96 | CImg<int> mask; |
| 97 | |
| 98 | if (file_i) { |
| 99 | img = CImg<>(file_i).resize(-100,-100,1,-100); |
| 100 | if (file_m) mask = CImg<unsigned char>(file_m).resize(img.width(),img.height(),1,1); |
| 101 | else if (zoom>1) { |
| 102 | mask = CImg<int>(img.width(),img.height(),1,1,-1). |
| 103 | resize((int)(img.width()*zoom),(int)(img.height()*zoom),1,1,4) + 1; |
| 104 | img.resize((int)(img.width()*zoom),(int)(img.height()*zoom),1,-100,3); |
| 105 | } |
| 106 | } else { |
| 107 | if (file_f) { |
| 108 | flow = CImg<>(file_f); |
| 109 | img = CImg<>((int)(flow.width()*zoom),(int)(flow.height()*zoom),1,1,0).noise(100,2); |
| 110 | flow.resize(img.width(),img.height(),1,2,3); |
| 111 | } else |
| 112 | throw CImgException("You need to specify at least one input image (option -i), or one flow image (option -f)"); |
| 113 | } |
| 114 | img.noise(noiseg,0).noise(noiseu,1).noise(noises,2); |
| 115 | float initial_min, initial_max = img.max_min(initial_min); |
| 116 | if (mask && init!=3) |
| 117 | cimg_forXYC(img,x,y,k) if (mask(x,y)) |
| 118 | img(x,y,k) = (float)((init? |
| 119 | (init==1?initial_max:((initial_max - initial_min)*cimg::rand())): |
| 120 | initial_min)); |
| 121 | |
| 122 | CImgDisplay disp; |
| 123 | if (visu) disp.assign(img,"Iterated Image"); |
| 124 | CImg<> G(img.width(),img.height(),1,3,0), T(G), veloc(img), val(2), vec(2,2); |
| 125 | |
| 126 | // PDE main iteration loop |
| 127 | //------------------------- |
| 128 | for (unsigned int iter = 0; iter<nb_iter && |
| 129 | (!disp || (!disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC())); ++iter) { |
| 130 | std::printf("\riter %u , xdt = %g ",iter,xdt); std::fflush(stdout); |
| 131 | if (stflag) img.print(); |
| 132 | if (disp && disp.is_keySPACE()) { view_t = !view_t; disp.set_key(); } |
| 133 | |
| 134 | if (!(iter%skip)) { |
| 135 | |
| 136 | // Compute the tensor field T, used to drive the diffusion |
| 137 | //--------------------------------------------------------- |
| 138 | |
| 139 | // When using PDE for flow visualization |
| 140 | if (flow) cimg_forXY(flow,x,y) { |
| 141 | const float |
| 142 | u = flow(x,y,0,0), |
| 143 | v = flow(x,y,0,1), |
| 144 | n = (float)std::sqrt((double)(u*u + v*v)), |
| 145 | nn = (n!=0)?n:1; |
| 146 | T(x,y,0) = u*u/nn; |
| 147 | T(x,y,1) = u*v/nn; |
| 148 | T(x,y,2) = v*v/nn; |
| 149 | } else { |
| 150 | |
| 151 | // Compute structure tensor field G |
| 152 | CImgList<> grad = img.get_gradient(); |
| 153 | if (alpha!=0) cimglist_for(grad,l) grad[l].blur((float)alpha); |
| 154 | G.fill(0); |
| 155 | cimg_forXYC(img,x,y,k) { |
| 156 | const float ix = grad[0](x,y,k), iy = grad[1](x,y,k); |
| 157 | G(x,y,0) += ix*ix; |
| 158 | G(x,y,1) += ix*iy; |
| 159 | G(x,y,2) += iy*iy; |
| 160 | } |
| 161 | if (sigma!=0) G.blur((float)sigma); |
| 162 | |
| 163 | // When using PDE for image restoration, inpainting or zooming |
| 164 | T.fill(0); |
| 165 | if (!mask) cimg_forXY(G,x,y) { |
| 166 | G.get_tensor_at(x,y).symmetric_eigen(val,vec); |
| 167 | const float |
| 168 | l1 = (float)std::pow(1.0f + val[0] + val[1],-a1), |
| 169 | l2 = (float)std::pow(1.0f + val[0] + val[1],-a2), |
| 170 | ux = vec(1,0), |
| 171 | uy = vec(1,1); |
| 172 | T(x,y,0) = l1*ux*ux + l2*uy*uy; |
| 173 | T(x,y,1) = l1*ux*uy - l2*ux*uy; |
| 174 | T(x,y,2) = l1*uy*uy + l2*ux*ux; |
| 175 | } |
| 176 | else cimg_forXY(G,x,y) if (mask(x,y)) { |
| 177 | G.get_tensor_at(x,y).symmetric_eigen(val,vec); |
| 178 | const float |
| 179 | ux = vec(1,0), |
| 180 | uy = vec(1,1); |
| 181 | T(x,y,0) = ux*ux; |
| 182 | T(x,y,1) = ux*uy; |
| 183 | T(x,y,2) = uy*uy; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Compute the PDE velocity and update the iterated image |
| 189 | //-------------------------------------------------------- |
| 190 | CImg_3x3(I,float); |
| 191 | veloc.fill(0); |
| 192 | cimg_forC(img,k) cimg_for3x3(img,x,y,0,k,I,float) { |
| 193 | const float |
| 194 | a = T(x,y,0), |
| 195 | b = T(x,y,1), |
| 196 | c = T(x,y,2), |
| 197 | ixx = Inc + Ipc - 2*Icc, |
| 198 | iyy = Icn + Icp - 2*Icc, |
| 199 | ixy = 0.25f*(Ipp + Inn - Ipn - Inp); |
| 200 | veloc(x,y,k) = a*ixx + 2*b*ixy + c*iyy; |
| 201 | } |
| 202 | if (dt>0) { |
| 203 | float m, M = veloc.max_min(m); |
| 204 | xdt = dt/std::max(cimg::abs(m),cimg::abs(M)); |
| 205 | } else xdt=-dt; |
| 206 | img+=veloc*xdt; |
| 207 | img.cut((float)initial_min,(float)initial_max); |
| 208 | |
| 209 | // Display and save iterations |
| 210 | if (disp && !(iter%visu)) { |
| 211 | if (!view_t) img.display(disp); |
| 212 | else { |
| 213 | const unsigned char white[3] = {255,255,255}; |
| 214 | CImg<unsigned char> visu = img.get_resize(disp.width(),disp.height()).normalize(0,255); |
| 215 | CImg<> isophotes(img.width(),img.height(),1,2,0); |
| 216 | cimg_forXY(img,x,y) if (!mask || mask(x,y)) { |
| 217 | T.get_tensor_at(x,y).symmetric_eigen(val,vec); |
| 218 | isophotes(x,y,0) = vec(0,0); |
| 219 | isophotes(x,y,1) = vec(0,1); |
| 220 | } |
| 221 | visu.draw_quiver(isophotes,white,0.5f,10,9,0).display(disp); |
| 222 | } |
| 223 | } |
| 224 | if (save && file_o && !(iter%save)) img.save(file_o,iter); |
| 225 | if (disp) disp.resize().display(img); |
| 226 | } |
| 227 | |
| 228 | // Save result and exit. |
| 229 | if (file_o) img.save(file_o); |
| 230 | return 0; |
| 231 | } |