1.1 diff -r 5edfbd3e7a46 -r 1204ebf9340d PTdecode/CImg-1.3.0/examples/hough_transform.cpp 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/PTdecode/CImg-1.3.0/examples/hough_transform.cpp Mon Aug 03 14:09:20 2009 +0100 1.4 @@ -0,0 +1,153 @@ 1.5 +/* 1.6 + # 1.7 + # File : hough_transform.cpp 1.8 + # ( C++ source file ) 1.9 + # 1.10 + # Description : Implementation of the Hough transform. 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 +int main(int argc,char **argv) { 1.65 + 1.66 + cimg_usage("Illustration of the Hough transform"); 1.67 + CImg<unsigned char> src(cimg_option("-i",cimg_imagepath "parrot_original.ppm","Input image")); 1.68 + CImg<> vote(500,400,1,1,0), img = CImg<>(src).get_pointwise_norm().normalize(0,255).resize(-100,-100,1,2,2); 1.69 + 1.70 + CImgDisplay disp(src,"Image"), dispvote(vote,"Hough Transform"); 1.71 + const unsigned char col1[3]={255,255,255}, col2[3]={0,0,0}; 1.72 + const double 1.73 + alpha = cimg_option("-a",1.5,"Gradient smoothing"), 1.74 + sigma = cimg_option("-s",0.5,"Hough Transform smoothing"), 1.75 + rhomax = std::sqrt((double)(img.dimx()*img.dimx()+img.dimy()*img.dimy()))/2, 1.76 + thetamax = 2*cimg::valuePI; 1.77 + 1.78 + if (cimg::dialog(cimg::basename(argv[0]), 1.79 + "Instructions : \n" 1.80 + "------------\n\n" 1.81 + "(1) When clicking on the image, all lines crossing the point\n" 1.82 + "will be voted in the Hough Transform image.\n\n" 1.83 + "(2) When clicking on the vote image, the corresponding line is drawn\n" 1.84 + "on the image.\n\n" 1.85 + "(3) When pressing the space bar, the image lines are detected from the\n" 1.86 + "image gradients.\n\n" 1.87 + "Note that a logarithmic scaling is performed for the vote image display.\n" 1.88 + "See also the available options (option '-h')\n","Start !","Quit",0,0,0,0, 1.89 + src.get_resize(100,100,1,3),true)) std::exit(0); 1.90 + 1.91 + while (!disp.is_closed && !dispvote.is_closed && !disp.is_keyQ && !dispvote.is_keyQ && !disp.is_keyESC && !dispvote.is_keyESC) { 1.92 + 1.93 + CImgDisplay::wait(disp,dispvote); 1.94 + 1.95 + // When pressing space bar, the vote is performed from the image gradients. 1.96 + if (dispvote.key==cimg::keySPACE || disp.key==cimg::keySPACE) { 1.97 + CImgList<> grad = img.get_gradient(); 1.98 + cimglist_for(grad,l) grad[l].blur((float)alpha); 1.99 + vote.fill(0); 1.100 + cimg_forXY(img,x,y) { 1.101 + const double 1.102 + X = (double)x-img.dimx()/2, 1.103 + Y = (double)y-img.dimy()/2, 1.104 + gx = grad[0](x,y), 1.105 + gy = grad[1](x,y); 1.106 + double 1.107 + theta = std::atan2(gy,gx), 1.108 + rho = std::sqrt(X*X+Y*Y)*std::cos(std::atan2(Y,X)-theta); 1.109 + if (rho<0) { rho=-rho; theta+=cimg::valuePI; } 1.110 + theta = cimg::mod(theta,thetamax); 1.111 + vote((int)(theta*dispvote.dimx()/thetamax),(int)(rho*dispvote.dimy()/rhomax))+=(float)std::sqrt(gx*gx+gy*gy); 1.112 + } 1.113 + vote.blur((float)sigma); 1.114 + CImg<> vote2(vote); { cimg_forXY(vote2,x,y) vote2(x,y) = (float)std::log(1+vote(x,y)); vote2.display(dispvote); } 1.115 + } 1.116 + 1.117 + // When clicking on the vote window. 1.118 + if (dispvote.button) { 1.119 + const double 1.120 + rho = dispvote.mouse_y*rhomax/dispvote.dimy(), 1.121 + theta = dispvote.mouse_x*thetamax/dispvote.dimx(), 1.122 + x = img.dimx()/2 + rho*std::cos(theta), 1.123 + y = img.dimy()/2 + rho*std::sin(theta); 1.124 + const int 1.125 + x0 = (int)(x+1000*std::sin(theta)), 1.126 + y0 = (int)(y-1000*std::cos(theta)), 1.127 + x1 = (int)(x-1000*std::sin(theta)), 1.128 + y1 = (int)(y+1000*std::cos(theta)); 1.129 + CImg<unsigned char>(src). 1.130 + draw_line(x0,y0,x1,y1,col1,1.0f,0xF0F0F0F0).draw_line(x0,y0,x1,y1,col2,1.0f,0x0F0F0F0F). 1.131 + 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.132 + 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.133 + display(disp); 1.134 + } 1.135 + 1.136 + // When clicking on the image. 1.137 + if (disp.button && disp.mouse_x>=0) { 1.138 + const double 1.139 + x0 = (double)disp.mouse_x-disp.dimx()/2, 1.140 + y0 = (double)disp.mouse_y-disp.dimy()/2, 1.141 + rho0 = std::sqrt(x0*x0+y0*y0), 1.142 + theta0 = std::atan2(y0,x0); 1.143 + 1.144 + for (double t=0; t<thetamax; t+=0.001) { 1.145 + double theta = t, rho = rho0*std::cos(theta0-t); 1.146 + if (rho<0) { rho=-rho; theta=cimg::mod(theta+cimg::valuePI,thetamax); } 1.147 + vote((int)(theta*vote.dimx()/thetamax),(int)(rho*vote.dimy()/rhomax))+=1; 1.148 + } 1.149 + CImg<> vote2(vote); cimg_forXY(vote2,x,y) vote2(x,y) = (float)std::log(1+vote(x,y)); vote2.display(dispvote); 1.150 + } 1.151 + dispvote.resize(dispvote); 1.152 + disp.resize(disp); 1.153 + } 1.154 + 1.155 + std::exit(0); 1.156 + return 0; 1.157 +}