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