lines.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
33 #include <opencv2/core/core.hpp>
34 #include <opencv2/imgproc/imgproc.hpp>
35 #include <boost/shared_ptr.hpp>
36 #include <Eigen/Dense>
37 
38 using namespace std;
39 using namespace cv;
40 using namespace Eigen;
41 class Lines
42 {
43  public:
44  vector<Vec4i> end_points;
45  vector<double> rhos;
46  vector<double> thetas;
47  MatrixXi extreme_points;
48 
49  /* Converter as linhas encontradas de coordenadas cartesianas para polares */
51  {
52  rhos.clear();
53  thetas.clear();
54 
55  for(uint i=0 ; i<end_points.size() ; i++)
56  {
57  double x1 = end_points[i][0];
58  double y1 = end_points[i][1];
59  double x2 = end_points[i][2];
60  double y2 = end_points[i][3];
61 
62  double th = atan2(x1-x2,y2-y1);
63  double rho = x1*cos(th) + y1*sin(th);
64 // cout<<"th="<<th<<" rho="<<rho<<endl;
65 
66  rhos.push_back(rho);
67  thetas.push_back(th);
68  }
69  }
70 
71  /* Retirar os valores fora da escala */
72  void extractInterval(double rho_max,double rho_min,double theta_max,double theta_min)
73  {
74  vector<Vec4i> end_points_local;
75  vector<double> rhos_local;
76  vector<double> thetas_local;
77 
78  /* Verificar se os valores de rho e theta estao dentro do intervalo pretendido */
79  for(uint i=0;i<end_points.size();i++)
80  {
81  if(thetas[i]>theta_min && thetas[i]<theta_max)
82  {
83  end_points_local.push_back(end_points[i]);
84  rhos_local.push_back(rhos[i]);
85  thetas_local.push_back(thetas[i]);
86  }
87 
88  }
89 
90  end_points=end_points_local;
91  rhos=rhos_local;
92  thetas=thetas_local;
93  }
94 
95  /* Calcular os pontos extremos das linhas encontradas */
96  MatrixXi extremePoints(int rows , int cols)
97  {
98  VectorXi P1(2);
99  VectorXi P2(2);
100  extreme_points.resize(20,4);
101  extreme_points.setOnes();
102 
103  for (uint i=0; i<20 ; i++)
104  extreme_points(i,3) = 2;
105 
106  for (uint i=0 ; i<end_points.size() ; i++)
107  {
108  /* Pontos extremos do segmento de recta econtrado */
109  P1 << end_points[i][0] , end_points[i][1];
110  P2 << end_points[i][2] , end_points[i][3];
111 
112  /* Variáveis auxiliares */
113  double Px,Py;
114  long double k;
115 
116  /* Pontos dos extremos da imagem */
117  VectorXi l1(2);
118  l1(0) = 0;
119  l1(1) = 0;
120 
121  VectorXi l2(2);
122  l2(0) = 0;
123  l2(1) = cols;
124 
125  VectorXi l3(2);
126  l3(0) = cols;
127  l3(1) = rows;
128 
129  VectorXi l4(2);
130  l4(0) = 0;
131  l4(1) = rows;
132 
133 
134  /* intersecção com as linhas horizontais */
135  if (thetas[i]!=0 && thetas[i]!=PI)
136  {
137  /* Calculo da intersecção com a linha 4*/
138  Py = rows;
139  k = ( (double)Py-(double)P1(1) )/( (double)P2(1)-(double)P1(1) );
140  Px = P1(0) + k*(P2(0)-P1(0));
141 
142  if (Px<=cols && Px>=0) /* Verificar se cumpre os limites da img */
143  {
144  extreme_points(i,0) = Px;
145  extreme_points(i,1) = Py;
146  }
147 
148  /* Calculo da intersecção com a linha 2*/
149  Py = 1;
150  k = ( (double)Py-(double)P1(1) )/( (double)P2(1)-(double)P1(1) );
151  Px = P1(0) + k*(P2(0)-P1(0));
152 
153  if (Px<=cols && Px>=0) /* Verificar se cumpre os limites da img */
154  {
155  extreme_points(i,0) = Px;
156  extreme_points(i,1) = Py;
157  }
158  }
159 
160  /* intersecção com as linhas verticais */
161  if (thetas[i]!=(PI/2) && thetas[i]!=(-PI/2) )
162  {
163  /* Calculo da intersecção com a linha 1*/
164  Px = 1;
165  k = ( (double)Px-(double)P1(0) )/( (double)P2(0)-(double)P1(0) );
166  Py = P1(1) + k*(P2(1)-P1(1));
167  if (Py>0 && Py<rows) /* Verificar se cumpre os limites da img */
168  {
169  extreme_points(i,2) = Px;
170  extreme_points(i,3) = Py;
171  }
172  /* Calculo da intersecção com a linha 3*/
173  Px = cols;
174  k = ( (double)Px-(double)P1(0) )/( (double)P2(0)-(double)P1(0) );
175  Py = P1(1) + k*(P2(1)-P1(1));
176 
177  if (Py>0 && Py<rows) /* Verificar se cumpre os limites da img */
178  {
179  extreme_points(i,2) = Px;
180  extreme_points(i,3) = Py;
181  }
182  }
183 
184  }
185 // cout<<"------------------------"<<endl;
186 
187  return extreme_points;
188  }
189 
190 };
MatrixXi extreme_points
Definition: lines.h:47
vector< double > thetas
Definition: lines.h:46
MatrixXi extremePoints(int rows, int cols)
Definition: lines.h:96
vector< Vec4i > end_points
Definition: lines.h:44
Definition: lines.h:41
void extractInterval(double rho_max, double rho_min, double theta_max, double theta_min)
Definition: lines.h:72
#define PI
Definition: edgedetector.h:21
vector< double > rhos
Definition: lines.h:45
void calculatePolarCoodinates(void)
Definition: lines.h:50


road_recognition
Author(s): Ricardo Morais
autogenerated on Mon Mar 2 2015 01:32:51