1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/PTdecode/CImg-1.3.0/examples/hough_transform.cpp Mon Aug 03 14:09:20 2009 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* 1.5 + # 1.6 + # File : hough_transform.cpp 1.7 + # ( C++ source file ) 1.8 + # 1.9 + # Description : Implementation of the Hough transform. 1.10 + # This file is a part of the CImg Library project. 1.11 + # ( http://cimg.sourceforge.net ) 1.12 + # 1.13 + # Copyright : David Tschumperle 1.14 + # ( http://www.greyc.ensicaen.fr/~dtschump/ ) 1.15 + # 1.16 + # License : CeCILL v2.0 1.17 + # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) 1.18 + # 1.19 + # This software is governed by the CeCILL license under French law and 1.20 + # abiding by the rules of distribution of free software. You can use, 1.21 + # modify and/ or redistribute the software under the terms of the CeCILL 1.22 + # license as circulated by CEA, CNRS and INRIA at the following URL 1.23 + # "http://www.cecill.info". 1.24 + # 1.25 + # As a counterpart to the access to the source code and rights to copy, 1.26 + # modify and redistribute granted by the license, users are provided only 1.27 + # with a limited warranty and the software's author, the holder of the 1.28 + # economic rights, and the successive licensors have only limited 1.29 + # liability. 1.30 + # 1.31 + # In this respect, the user's attention is drawn to the risks associated 1.32 + # with loading, using, modifying and/or developing or reproducing the 1.33 + # software by the user in light of its specific status of free software, 1.34 + # that may mean that it is complicated to manipulate, and that also 1.35 + # therefore means that it is reserved for developers and experienced 1.36 + # professionals having in-depth computer knowledge. Users are therefore 1.37 + # encouraged to load and test the software's suitability as regards their 1.38 + # requirements in conditions enabling the security of their systems and/or 1.39 + # data to be ensured and, more generally, to use and operate it in the 1.40 + # same conditions as regards security. 1.41 + # 1.42 + # The fact that you are presently reading this means that you have had 1.43 + # knowledge of the CeCILL license and that you accept its terms. 1.44 + # 1.45 +*/ 1.46 + 1.47 +#include "CImg.h" 1.48 +using namespace cimg_library; 1.49 + 1.50 +// The lines below are necessary when using a non-standard compiler as visualcpp6. 1.51 +#ifdef cimg_use_visualcpp6 1.52 +#define std 1.53 +#endif 1.54 +#ifdef min 1.55 +#undef min 1.56 +#undef max 1.57 +#endif 1.58 + 1.59 +#ifndef cimg_imagepath 1.60 +#define cimg_imagepath "img/" 1.61 +#endif 1.62 + 1.63 +int main(int argc,char **argv) { 1.64 + 1.65 + cimg_usage("Illustration of the Hough transform"); 1.66 + CImg<unsigned char> src(cimg_option("-i",cimg_imagepath "parrot_original.ppm","Input image")); 1.67 + CImg<> vote(500,400,1,1,0), img = CImg<>(src).get_pointwise_norm().normalize(0,255).resize(-100,-100,1,2,2); 1.68 + 1.69 + CImgDisplay disp(src,"Image"), dispvote(vote,"Hough Transform"); 1.70 + const unsigned char col1[3]={255,255,255}, col2[3]={0,0,0}; 1.71 + const double 1.72 + alpha = cimg_option("-a",1.5,"Gradient smoothing"), 1.73 + sigma = cimg_option("-s",0.5,"Hough Transform smoothing"), 1.74 + rhomax = std::sqrt((double)(img.dimx()*img.dimx()+img.dimy()*img.dimy()))/2, 1.75 + thetamax = 2*cimg::valuePI; 1.76 + 1.77 + if (cimg::dialog(cimg::basename(argv[0]), 1.78 + "Instructions : \n" 1.79 + "------------\n\n" 1.80 + "(1) When clicking on the image, all lines crossing the point\n" 1.81 + "will be voted in the Hough Transform image.\n\n" 1.82 + "(2) When clicking on the vote image, the corresponding line is drawn\n" 1.83 + "on the image.\n\n" 1.84 + "(3) When pressing the space bar, the image lines are detected from the\n" 1.85 + "image gradients.\n\n" 1.86 + "Note that a logarithmic scaling is performed for the vote image display.\n" 1.87 + "See also the available options (option '-h')\n","Start !","Quit",0,0,0,0, 1.88 + src.get_resize(100,100,1,3),true)) std::exit(0); 1.89 + 1.90 + while (!disp.is_closed && !dispvote.is_closed && !disp.is_keyQ && !dispvote.is_keyQ && !disp.is_keyESC && !dispvote.is_keyESC) { 1.91 + 1.92 + CImgDisplay::wait(disp,dispvote); 1.93 + 1.94 + // When pressing space bar, the vote is performed from the image gradients. 1.95 + if (dispvote.key==cimg::keySPACE || disp.key==cimg::keySPACE) { 1.96 + CImgList<> grad = img.get_gradient(); 1.97 + cimglist_for(grad,l) grad[l].blur((float)alpha); 1.98 + vote.fill(0); 1.99 + cimg_forXY(img,x,y) { 1.100 + const double 1.101 + X = (double)x-img.dimx()/2, 1.102 + Y = (double)y-img.dimy()/2, 1.103 + gx = grad[0](x,y), 1.104 + gy = grad[1](x,y); 1.105 + double 1.106 + theta = std::atan2(gy,gx), 1.107 + rho = std::sqrt(X*X+Y*Y)*std::cos(std::atan2(Y,X)-theta); 1.108 + if (rho<0) { rho=-rho; theta+=cimg::valuePI; } 1.109 + theta = cimg::mod(theta,thetamax); 1.110 + vote((int)(theta*dispvote.dimx()/thetamax),(int)(rho*dispvote.dimy()/rhomax))+=(float)std::sqrt(gx*gx+gy*gy); 1.111 + } 1.112 + vote.blur((float)sigma); 1.113 + CImg<> vote2(vote); { cimg_forXY(vote2,x,y) vote2(x,y) = (float)std::log(1+vote(x,y)); vote2.display(dispvote); } 1.114 + } 1.115 + 1.116 + // When clicking on the vote window. 1.117 + if (dispvote.button) { 1.118 + const double 1.119 + rho = dispvote.mouse_y*rhomax/dispvote.dimy(), 1.120 + theta = dispvote.mouse_x*thetamax/dispvote.dimx(), 1.121 + x = img.dimx()/2 + rho*std::cos(theta), 1.122 + y = img.dimy()/2 + rho*std::sin(theta); 1.123 + const int 1.124 + x0 = (int)(x+1000*std::sin(theta)), 1.125 + y0 = (int)(y-1000*std::cos(theta)), 1.126 + x1 = (int)(x-1000*std::sin(theta)), 1.127 + y1 = (int)(y+1000*std::cos(theta)); 1.128 + CImg<unsigned char>(src). 1.129 + draw_line(x0,y0,x1,y1,col1,1.0f,0xF0F0F0F0).draw_line(x0,y0,x1,y1,col2,1.0f,0x0F0F0F0F). 1.130 + draw_line(x0+1,y0,x1+1,y1,col1,1.0f,0xF0F0F0F0).draw_line(x0+1,y0,x1+1,y1,col2,1.0f,0x0F0F0F0F). 1.131 + draw_line(x0,y0+1,x1,y1+1,col1,1.0f,0xF0F0F0F0).draw_line(x0,y0+1,x1,y1+1,col2,1.0f,0x0F0F0F0F). 1.132 + display(disp); 1.133 + } 1.134 + 1.135 + // When clicking on the image. 1.136 + if (disp.button && disp.mouse_x>=0) { 1.137 + const double 1.138 + x0 = (double)disp.mouse_x-disp.dimx()/2, 1.139 + y0 = (double)disp.mouse_y-disp.dimy()/2, 1.140 + rho0 = std::sqrt(x0*x0+y0*y0), 1.141 + theta0 = std::atan2(y0,x0); 1.142 + 1.143 + for (double t=0; t<thetamax; t+=0.001) { 1.144 + double theta = t, rho = rho0*std::cos(theta0-t); 1.145 + if (rho<0) { rho=-rho; theta=cimg::mod(theta+cimg::valuePI,thetamax); } 1.146 + vote((int)(theta*vote.dimx()/thetamax),(int)(rho*vote.dimy()/rhomax))+=1; 1.147 + } 1.148 + CImg<> vote2(vote); cimg_forXY(vote2,x,y) vote2(x,y) = (float)std::log(1+vote(x,y)); vote2.display(dispvote); 1.149 + } 1.150 + dispvote.resize(dispvote); 1.151 + disp.resize(disp); 1.152 + } 1.153 + 1.154 + std::exit(0); 1.155 + return 0; 1.156 +}