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