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