1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/PTdecode/CImg-1.3.0/examples/image_registration.cpp Mon Aug 03 14:09:20 2009 +0100 1.3 @@ -0,0 +1,237 @@ 1.4 +/* 1.5 + # 1.6 + # File : image_registration.cpp 1.7 + # ( C++ source file ) 1.8 + # 1.9 + # Description : Compute a motion field between two images, 1.10 + # with a multiscale and variational algorithm. 1.11 + # This file is a part of the CImg Library project. 1.12 + # ( http://cimg.sourceforge.net ) 1.13 + # 1.14 + # Copyright : David Tschumperle 1.15 + # ( http://www.greyc.ensicaen.fr/~dtschump/ ) 1.16 + # 1.17 + # License : CeCILL v2.0 1.18 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.19 + # 1.20 + # This software is governed by the CeCILL license under French law and 1.21 + # abiding by the rules of distribution of free software. You can use, 1.22 + # modify and/ or redistribute the software under the terms of the CeCILL 1.23 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.24 + # "http://www.cecill.info". 1.25 + # 1.26 + # As a counterpart to the access to the source code and rights to copy, 1.27 + # modify and redistribute granted by the license, users are provided only 1.28 + # with a limited warranty and the software's author, the holder of the 1.29 + # economic rights, and the successive licensors have only limited 1.30 + # liability. 1.31 + # 1.32 + # In this respect, the user's attention is drawn to the risks associated 1.33 + # with loading, using, modifying and/or developing or reproducing the 1.34 + # software by the user in light of its specific status of free software, 1.35 + # that may mean that it is complicated to manipulate, and that also 1.36 + # therefore means that it is reserved for developers and experienced 1.37 + # professionals having in-depth computer knowledge. Users are therefore 1.38 + # encouraged to load and test the software's suitability as regards their 1.39 + # requirements in conditions enabling the security of their systems and/or 1.40 + # data to be ensured and, more generally, to use and operate it in the 1.41 + # same conditions as regards security. 1.42 + # 1.43 + # The fact that you are presently reading this means that you have had 1.44 + # knowledge of the CeCILL license and that you accept its terms. 1.45 + # 1.46 +*/ 1.47 + 1.48 +#include "CImg.h" 1.49 +using namespace cimg_library; 1.50 + 1.51 +// The lines below are necessary when using a non-standard compiler as visualcpp6. 1.52 +#ifdef cimg_use_visualcpp6 1.53 +#define std 1.54 +#endif 1.55 +#ifdef min 1.56 +#undef min 1.57 +#undef max 1.58 +#endif 1.59 + 1.60 +#ifndef cimg_imagepath 1.61 +#define cimg_imagepath "img/" 1.62 +#endif 1.63 + 1.64 +// animate_warp() : Create warping animation from two images and a motion field 1.65 +//---------------- 1.66 +void animate_warp(const CImg<unsigned char>& src, const CImg<unsigned char>& dest, const CImg<>& u, 1.67 + const bool morph, const bool imode, const char *filename,int nb, CImgDisplay& disp) { 1.68 + CImg<unsigned char> visu = CImgList<unsigned char>(src,dest,src).get_append('x'), warp(src); 1.69 + float t=0; 1.70 + for (unsigned int iter=0; !disp || (!disp.is_closed && !disp.is_keyQ); iter++) { 1.71 + if (morph) cimg_forXYV(warp,x,y,k) { 1.72 + const float dx = u(x,y,0), dy = u(x,y,1), 1.73 + I1 = (float)src.linear_atXY(x-t*dx, y-t*dy, k), 1.74 + I2 = (float)dest.linear_atXY(x+(1-t)*dx,y+(1-t)*dy,k); 1.75 + warp(x,y,k) = (unsigned char)((1-t)*I1 + t*I2); 1.76 + } else cimg_forXYV(warp,x,y,k) { 1.77 + const float dx = u(x,y,0), dy = u(x,y,1), I1 = (float)src.linear_atXY(x-t*dx, y-t*dy, 0,k); 1.78 + warp(x,y,k) = (unsigned char)I1; 1.79 + } 1.80 + if (disp) visu.draw_image(2*src.dimx(),warp).display(disp.resize().wait(30)); 1.81 + if (filename && *filename && (imode || (int)iter<nb)) { 1.82 + std::fprintf(stderr,"\r > frame %d ",iter); 1.83 + warp.save(filename,iter); 1.84 + } 1.85 + t+=1.0f/nb; 1.86 + if (t<0) { t=0; nb=-nb; } 1.87 + if (t>1) { t=1; nb=-nb; if (filename && *filename) std::exit(0); } 1.88 + } 1.89 +} 1.90 + 1.91 +// get_warp() : Return the image src warped by the motion field u. 1.92 +//------------ 1.93 +template<typename T> CImg<T> getwarp(const CImg<T>& src, const CImg<>& u) { 1.94 + CImg<T> warp(src); 1.95 + cimg_forXY(warp,x,y) warp(x,y) = (T)src.linear_atXY(x - u(x,y,0), y - u(x,y,1)); 1.96 + return warp; 1.97 +} 1.98 + 1.99 +// optmonoflow() : Register images for one scale ( semi-implicite PDE scheme ) between I2->I1 1.100 +//--------------- 1.101 +CImg<> optmonoflow(const CImg<>& I1, const CImg<>& I2, const CImg<>& u0, 1.102 + const float smooth, const float precision, CImgDisplay& disp) { 1.103 + 1.104 + CImg<> u = u0.get_resize(I1.dimx(),I1.dimy(),1,2,3),dI(u); 1.105 + CImg_3x3(I,float); 1.106 + float dt=2,E=1e20f; 1.107 + 1.108 + // compute first derivatives of I2 1.109 + cimg_for3x3(I2,x,y,0,0,I) { 1.110 + dI(x,y,0) = 0.5f*(Inc-Ipc); 1.111 + dI(x,y,1) = 0.5f*(Icn-Icp); 1.112 + } 1.113 + 1.114 + // Main PDE iteration 1.115 + for (unsigned int iter=0; iter<100000; iter++) { 1.116 + std::fprintf(stderr,"\r- Iteration %d - E = %g",iter,E); std::fflush(stderr); 1.117 + const float Eold = E; 1.118 + E = 0; 1.119 + cimg_for3XY(u,x,y) { 1.120 + const float 1.121 + X = x + u(x,y,0), 1.122 + Y = y + u(x,y,1), 1.123 + deltaI = (float)(I2.linear_atXY(X,Y) - I1(x,y)); 1.124 + float tmpf = 0; 1.125 + cimg_forV(u,k) { 1.126 + const float 1.127 + ux = 0.5f*(u(_n1x,y,k)-u(_p1x,y,k)), 1.128 + uy = 0.5f*(u(x,_n1y,k)-u(x,_p1y,k)); 1.129 + u(x,y,k) = (float)( u(x,y,k) + 1.130 + dt*( 1.131 + -deltaI*dI.linear_atXY(X,Y,k) + 1.132 + smooth* ( u(_n1x,y,k) + u(_p1x,y,k) + u(x,_n1y,k) + u(x,_p1y,k) ) 1.133 + ) 1.134 + )/(1+4*smooth*dt); 1.135 + tmpf += ux*ux + uy*uy; 1.136 + } 1.137 + E += deltaI*deltaI + smooth * tmpf; 1.138 + } 1.139 + if (cimg::abs(Eold-E)<precision) break; 1.140 + if (Eold<E) dt*=0.5; 1.141 + if (disp) disp.resize(); 1.142 + if (disp && disp.is_closed) std::exit(0); 1.143 + if (disp && !(iter%300)) { 1.144 + const unsigned char white = 255; 1.145 + CImg<unsigned char> tmp = getwarp(I1,u).normalize(0,200); 1.146 + tmp.resize(disp.dimx(),disp.dimy()).draw_quiver(u,&white,0.7f,15,-14,0).display(disp); 1.147 + } 1.148 + } 1.149 + return u; 1.150 +} 1.151 + 1.152 +// optflow() : multiscale version of the image registration algorithm 1.153 +//----------- 1.154 +CImg<> optflow(const CImg<>& xsrc, const CImg<>& xdest, 1.155 + const float smooth, const float precision, const unsigned int pnb_scale, CImgDisplay& disp) { 1.156 + const CImg<> 1.157 + src = xsrc.get_pointwise_norm(1).resize(xdest.dimx(),xdest.dimy(),1,1,3).normalize(0,1), 1.158 + dest = xdest.get_pointwise_norm(1).resize(xdest.dimx(),xdest.dimy(),1,1,3).normalize(0,1); 1.159 + CImg<> u = CImg<>(src.dimx(),src.dimy(),1,2).fill(0); 1.160 + 1.161 + const unsigned int nb_scale = pnb_scale>0?pnb_scale:(unsigned int)(2*std::log((double)(cimg::max(src.dimx(),src.dimy())))); 1.162 + for (int scale=nb_scale-1; scale>=0; scale--) { 1.163 + const CImg<> I1 = src.get_resize((int)(src.dimx()/std::pow(1.5,scale)), (int)(src.dimy()/std::pow(1.5,scale)) ,1,1,3); 1.164 + const CImg<> I2 = dest.get_resize((int)(src.dimx()/std::pow(1.5,scale)), (int)(src.dimy()/std::pow(1.5,scale)) ,1,1,3); 1.165 + std::fprintf(stderr," * Scale %d\n",scale); 1.166 + u*=1.5; 1.167 + u = optmonoflow(I1,I2,u,smooth,(float)(precision/std::pow(2.25,1+scale)),disp); 1.168 + std::fprintf(stderr,"\n"); 1.169 + } 1.170 + return u; 1.171 +} 1.172 + 1.173 +/*------------------------ 1.174 + 1.175 + Main function 1.176 + 1.177 + ------------------------*/ 1.178 + 1.179 +int main(int argc,char **argv) { 1.180 + 1.181 + // Read command line parameters 1.182 + cimg_usage("Compute an optical flow between two 2D images, and create a warped animation"); 1.183 + const char 1.184 + *name_i1 = cimg_option("-i",cimg_imagepath "sh0r.pgm","Input Image 1 (Destination)"), 1.185 + *name_i2 = cimg_option("-i2",cimg_imagepath "sh1r.pgm","Input Image 2 (Source)"), 1.186 + *name_o = cimg_option("-o",(const char*)NULL,"Output 2D flow (inrimage)"), 1.187 + *name_seq = cimg_option("-o2",(const char*)NULL,"Output Warping Sequence"); 1.188 + const float 1.189 + smooth = cimg_option("-s",0.1f,"Flow Smoothness"), 1.190 + precision = cimg_option("-p",0.9f,"Convergence precision"); 1.191 + const unsigned int 1.192 + nb = cimg_option("-n",40,"Number of warped frames"), 1.193 + nbscale = cimg_option("-scale",0,"Number of scales (0=auto)"); 1.194 + const bool 1.195 + normalize = cimg_option("-equalize",true,"Histogram normalization of the images"), 1.196 + morph = cimg_option("-m",true,"Morphing mode"), 1.197 + imode = cimg_option("-c",true,"Complete interpolation (or last frame is missing)"), 1.198 + dispflag = !cimg_option("-novisu",false,"Visualization"); 1.199 + 1.200 + // Init images and display 1.201 + std::fprintf(stderr," - Init images.\n"); 1.202 + const CImg<> 1.203 + src(name_i1), 1.204 + dest(CImg<>(name_i2).resize(src,3)), 1.205 + src_blur = normalize?src.get_blur(0.5f).equalize(256):src.get_blur(0.5f), 1.206 + dest_blur = normalize?dest.get_blur(0.5f).equalize(256):dest.get_blur(0.5f); 1.207 + 1.208 + CImgDisplay disp; 1.209 + if (dispflag) { 1.210 + unsigned int w = src.dimx(), h = src.dimy(); 1.211 + const unsigned int dmin = cimg::min(w,h), minsiz = 512; 1.212 + if (dmin<minsiz) { w=w*minsiz/dmin; h=h*minsiz/dmin; } 1.213 + const unsigned int dmax = cimg::max(w,h), maxsiz = 1024; 1.214 + if (dmax>maxsiz) { w=w*maxsiz/dmax; h=h*maxsiz/dmax; } 1.215 + disp.assign(w,h,"Estimated Motion",0); 1.216 + } 1.217 + 1.218 + // Run Motion estimation algorithm 1.219 + std::fprintf(stderr," - Compute optical flow.\n"); 1.220 + const CImg<> u = optflow(src_blur,dest_blur,smooth,precision,nbscale,disp); 1.221 + if (name_o) u.save(name_o); 1.222 + u.print("Computed flow"); 1.223 + 1.224 + // Do morphing animation 1.225 + std::fprintf(stderr," - Create warped animation.\n"); 1.226 + CImgDisplay disp2; 1.227 + if (dispflag) { 1.228 + unsigned int w = src.dimx(), h = src.dimy(); 1.229 + const unsigned int dmin = cimg::min(w,h), minsiz = 100; 1.230 + if (dmin<minsiz) { w=w*minsiz/dmin; h=h*minsiz/dmin; } 1.231 + const unsigned int dmax = cimg::max(w,h), maxsiz = 1024/3; 1.232 + if (dmax>maxsiz) { w=w*maxsiz/dmax; h=h*maxsiz/dmax; } 1.233 + disp2.assign(3*w,h,"Source/Destination images and Motion animation",0); 1.234 + } 1.235 + 1.236 + animate_warp(src.get_normalize(0,255),dest.get_normalize(0,255),u,morph,imode,name_seq,nb,disp2); 1.237 + 1.238 + std::exit(0); 1.239 + return 0; 1.240 +}