Wed, 05 Aug 2009 15:02:31 +0100
PTdecode: add support for uncompressed data (NOTE: *NOT* supported by the PT-2450DX)
philpem@5 | 1 | /* |
philpem@5 | 2 | # |
philpem@5 | 3 | # File : hough_transform.cpp |
philpem@5 | 4 | # ( C++ source file ) |
philpem@5 | 5 | # |
philpem@5 | 6 | # Description : Implementation of the Hough transform. |
philpem@5 | 7 | # This file is a part of the CImg Library project. |
philpem@5 | 8 | # ( http://cimg.sourceforge.net ) |
philpem@5 | 9 | # |
philpem@5 | 10 | # Copyright : David Tschumperle |
philpem@5 | 11 | # ( http://www.greyc.ensicaen.fr/~dtschump/ ) |
philpem@5 | 12 | # |
philpem@5 | 13 | # License : CeCILL v2.0 |
philpem@5 | 14 | # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) |
philpem@5 | 15 | # |
philpem@5 | 16 | # This software is governed by the CeCILL license under French law and |
philpem@5 | 17 | # abiding by the rules of distribution of free software. You can use, |
philpem@5 | 18 | # modify and/ or redistribute the software under the terms of the CeCILL |
philpem@5 | 19 | # license as circulated by CEA, CNRS and INRIA at the following URL |
philpem@5 | 20 | # "http://www.cecill.info". |
philpem@5 | 21 | # |
philpem@5 | 22 | # As a counterpart to the access to the source code and rights to copy, |
philpem@5 | 23 | # modify and redistribute granted by the license, users are provided only |
philpem@5 | 24 | # with a limited warranty and the software's author, the holder of the |
philpem@5 | 25 | # economic rights, and the successive licensors have only limited |
philpem@5 | 26 | # liability. |
philpem@5 | 27 | # |
philpem@5 | 28 | # In this respect, the user's attention is drawn to the risks associated |
philpem@5 | 29 | # with loading, using, modifying and/or developing or reproducing the |
philpem@5 | 30 | # software by the user in light of its specific status of free software, |
philpem@5 | 31 | # that may mean that it is complicated to manipulate, and that also |
philpem@5 | 32 | # therefore means that it is reserved for developers and experienced |
philpem@5 | 33 | # professionals having in-depth computer knowledge. Users are therefore |
philpem@5 | 34 | # encouraged to load and test the software's suitability as regards their |
philpem@5 | 35 | # requirements in conditions enabling the security of their systems and/or |
philpem@5 | 36 | # data to be ensured and, more generally, to use and operate it in the |
philpem@5 | 37 | # same conditions as regards security. |
philpem@5 | 38 | # |
philpem@5 | 39 | # The fact that you are presently reading this means that you have had |
philpem@5 | 40 | # knowledge of the CeCILL license and that you accept its terms. |
philpem@5 | 41 | # |
philpem@5 | 42 | */ |
philpem@5 | 43 | |
philpem@5 | 44 | #include "CImg.h" |
philpem@5 | 45 | using namespace cimg_library; |
philpem@5 | 46 | |
philpem@5 | 47 | // The lines below are necessary when using a non-standard compiler as visualcpp6. |
philpem@5 | 48 | #ifdef cimg_use_visualcpp6 |
philpem@5 | 49 | #define std |
philpem@5 | 50 | #endif |
philpem@5 | 51 | #ifdef min |
philpem@5 | 52 | #undef min |
philpem@5 | 53 | #undef max |
philpem@5 | 54 | #endif |
philpem@5 | 55 | |
philpem@5 | 56 | #ifndef cimg_imagepath |
philpem@5 | 57 | #define cimg_imagepath "img/" |
philpem@5 | 58 | #endif |
philpem@5 | 59 | |
philpem@5 | 60 | int main(int argc,char **argv) { |
philpem@5 | 61 | |
philpem@5 | 62 | cimg_usage("Illustration of the Hough transform"); |
philpem@5 | 63 | CImg<unsigned char> src(cimg_option("-i",cimg_imagepath "parrot_original.ppm","Input image")); |
philpem@5 | 64 | CImg<> vote(500,400,1,1,0), img = CImg<>(src).get_pointwise_norm().normalize(0,255).resize(-100,-100,1,2,2); |
philpem@5 | 65 | |
philpem@5 | 66 | CImgDisplay disp(src,"Image"), dispvote(vote,"Hough Transform"); |
philpem@5 | 67 | const unsigned char col1[3]={255,255,255}, col2[3]={0,0,0}; |
philpem@5 | 68 | const double |
philpem@5 | 69 | alpha = cimg_option("-a",1.5,"Gradient smoothing"), |
philpem@5 | 70 | sigma = cimg_option("-s",0.5,"Hough Transform smoothing"), |
philpem@5 | 71 | rhomax = std::sqrt((double)(img.dimx()*img.dimx()+img.dimy()*img.dimy()))/2, |
philpem@5 | 72 | thetamax = 2*cimg::valuePI; |
philpem@5 | 73 | |
philpem@5 | 74 | if (cimg::dialog(cimg::basename(argv[0]), |
philpem@5 | 75 | "Instructions : \n" |
philpem@5 | 76 | "------------\n\n" |
philpem@5 | 77 | "(1) When clicking on the image, all lines crossing the point\n" |
philpem@5 | 78 | "will be voted in the Hough Transform image.\n\n" |
philpem@5 | 79 | "(2) When clicking on the vote image, the corresponding line is drawn\n" |
philpem@5 | 80 | "on the image.\n\n" |
philpem@5 | 81 | "(3) When pressing the space bar, the image lines are detected from the\n" |
philpem@5 | 82 | "image gradients.\n\n" |
philpem@5 | 83 | "Note that a logarithmic scaling is performed for the vote image display.\n" |
philpem@5 | 84 | "See also the available options (option '-h')\n","Start !","Quit",0,0,0,0, |
philpem@5 | 85 | src.get_resize(100,100,1,3),true)) std::exit(0); |
philpem@5 | 86 | |
philpem@5 | 87 | while (!disp.is_closed && !dispvote.is_closed && !disp.is_keyQ && !dispvote.is_keyQ && !disp.is_keyESC && !dispvote.is_keyESC) { |
philpem@5 | 88 | |
philpem@5 | 89 | CImgDisplay::wait(disp,dispvote); |
philpem@5 | 90 | |
philpem@5 | 91 | // When pressing space bar, the vote is performed from the image gradients. |
philpem@5 | 92 | if (dispvote.key==cimg::keySPACE || disp.key==cimg::keySPACE) { |
philpem@5 | 93 | CImgList<> grad = img.get_gradient(); |
philpem@5 | 94 | cimglist_for(grad,l) grad[l].blur((float)alpha); |
philpem@5 | 95 | vote.fill(0); |
philpem@5 | 96 | cimg_forXY(img,x,y) { |
philpem@5 | 97 | const double |
philpem@5 | 98 | X = (double)x-img.dimx()/2, |
philpem@5 | 99 | Y = (double)y-img.dimy()/2, |
philpem@5 | 100 | gx = grad[0](x,y), |
philpem@5 | 101 | gy = grad[1](x,y); |
philpem@5 | 102 | double |
philpem@5 | 103 | theta = std::atan2(gy,gx), |
philpem@5 | 104 | rho = std::sqrt(X*X+Y*Y)*std::cos(std::atan2(Y,X)-theta); |
philpem@5 | 105 | if (rho<0) { rho=-rho; theta+=cimg::valuePI; } |
philpem@5 | 106 | theta = cimg::mod(theta,thetamax); |
philpem@5 | 107 | vote((int)(theta*dispvote.dimx()/thetamax),(int)(rho*dispvote.dimy()/rhomax))+=(float)std::sqrt(gx*gx+gy*gy); |
philpem@5 | 108 | } |
philpem@5 | 109 | vote.blur((float)sigma); |
philpem@5 | 110 | CImg<> vote2(vote); { cimg_forXY(vote2,x,y) vote2(x,y) = (float)std::log(1+vote(x,y)); vote2.display(dispvote); } |
philpem@5 | 111 | } |
philpem@5 | 112 | |
philpem@5 | 113 | // When clicking on the vote window. |
philpem@5 | 114 | if (dispvote.button) { |
philpem@5 | 115 | const double |
philpem@5 | 116 | rho = dispvote.mouse_y*rhomax/dispvote.dimy(), |
philpem@5 | 117 | theta = dispvote.mouse_x*thetamax/dispvote.dimx(), |
philpem@5 | 118 | x = img.dimx()/2 + rho*std::cos(theta), |
philpem@5 | 119 | y = img.dimy()/2 + rho*std::sin(theta); |
philpem@5 | 120 | const int |
philpem@5 | 121 | x0 = (int)(x+1000*std::sin(theta)), |
philpem@5 | 122 | y0 = (int)(y-1000*std::cos(theta)), |
philpem@5 | 123 | x1 = (int)(x-1000*std::sin(theta)), |
philpem@5 | 124 | y1 = (int)(y+1000*std::cos(theta)); |
philpem@5 | 125 | CImg<unsigned char>(src). |
philpem@5 | 126 | draw_line(x0,y0,x1,y1,col1,1.0f,0xF0F0F0F0).draw_line(x0,y0,x1,y1,col2,1.0f,0x0F0F0F0F). |
philpem@5 | 127 | draw_line(x0+1,y0,x1+1,y1,col1,1.0f,0xF0F0F0F0).draw_line(x0+1,y0,x1+1,y1,col2,1.0f,0x0F0F0F0F). |
philpem@5 | 128 | draw_line(x0,y0+1,x1,y1+1,col1,1.0f,0xF0F0F0F0).draw_line(x0,y0+1,x1,y1+1,col2,1.0f,0x0F0F0F0F). |
philpem@5 | 129 | display(disp); |
philpem@5 | 130 | } |
philpem@5 | 131 | |
philpem@5 | 132 | // When clicking on the image. |
philpem@5 | 133 | if (disp.button && disp.mouse_x>=0) { |
philpem@5 | 134 | const double |
philpem@5 | 135 | x0 = (double)disp.mouse_x-disp.dimx()/2, |
philpem@5 | 136 | y0 = (double)disp.mouse_y-disp.dimy()/2, |
philpem@5 | 137 | rho0 = std::sqrt(x0*x0+y0*y0), |
philpem@5 | 138 | theta0 = std::atan2(y0,x0); |
philpem@5 | 139 | |
philpem@5 | 140 | for (double t=0; t<thetamax; t+=0.001) { |
philpem@5 | 141 | double theta = t, rho = rho0*std::cos(theta0-t); |
philpem@5 | 142 | if (rho<0) { rho=-rho; theta=cimg::mod(theta+cimg::valuePI,thetamax); } |
philpem@5 | 143 | vote((int)(theta*vote.dimx()/thetamax),(int)(rho*vote.dimy()/rhomax))+=1; |
philpem@5 | 144 | } |
philpem@5 | 145 | CImg<> vote2(vote); cimg_forXY(vote2,x,y) vote2(x,y) = (float)std::log(1+vote(x,y)); vote2.display(dispvote); |
philpem@5 | 146 | } |
philpem@5 | 147 | dispvote.resize(dispvote); |
philpem@5 | 148 | disp.resize(disp); |
philpem@5 | 149 | } |
philpem@5 | 150 | |
philpem@5 | 151 | std::exit(0); |
philpem@5 | 152 | return 0; |
philpem@5 | 153 | } |