linefinder.h
Go to the documentation of this file.
1 /*------------------------------------------------------------------------------------------*\
2  This file contains material supporting chapter 7 of the cookbook:
3  Computer Vision Programming using the OpenCV Library.
4  by Robert Laganiere, Packt Publishing, 2011.
5 
6  This program is free software; permission is hereby granted to use, copy, modify,
7  and distribute this source code, or portions thereof, for any purpose, without fee,
8  subject to the restriction that the copyright notice may not be removed
9  or altered from any source or altered source distribution.
10  The software is released on an as-is basis and without any warranties of any kind.
11  In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12  The author disclaims all warranties with regard to this software, any use,
13  and any consequent failure, is purely the responsibility of the user.
14 
15  Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16 \*------------------------------------------------------------------------------------------*/
17 
18 #if !defined LINEF
19 #define LINEF
20 
21 #include <opencv2/core/core.hpp>
22 #include <opencv2/imgproc/imgproc.hpp>
23 #define PI 3.1415926
24 
25 class LineFinder {
26 
27  private:
28 
29  // original image
30  cv::Mat img;
31 
32  // vector containing the end points
33  // of the detected lines
34  std::vector<cv::Vec4i> lines;
35 
36  // accumulator resolution parameters
37  double deltaRho;
38  double deltaTheta;
39 
40  // minimum number of votes that a line
41  // must receive before being considered
42  int minVote;
43 
44  // min length for a line
45  double minLength;
46 
47  // max allowed gap along the line
48  double maxGap;
49 
50  public:
51 
52  // Default accumulator resolution is 1 pixel by 1 degree
53  // no gap, no mimimum length
54 
55  LineFinder() : deltaRho(1), deltaTheta(PI/180), minVote(10), minLength(0.), maxGap(0.) {}
56 
57  // Set the resolution of the accumulator
58  void setAccResolution(double dRho, double dTheta) {
59 
60  deltaRho= dRho;
61  deltaTheta= dTheta;
62  }
63 
64  // Set the minimum number of votes
65  void setMinVote(int minv) {
66 
67  minVote= minv;
68  }
69 
70  // Set line length and gap
71  void setLineLengthAndGap(double length, double gap) {
72 
73  minLength= length;
74  maxGap= gap;
75  }
76 
77  // Apply probabilistic Hough Transform
78  std::vector<cv::Vec4i> findLines(cv::Mat& binary) {
79 
80  lines.clear();
81  cv::HoughLinesP(binary,lines,deltaRho,deltaTheta,minVote, minLength, maxGap);
82 
83  return lines;
84  }
85 
86  // Draw the detected lines on an image
87  void drawDetectedLines(cv::Mat &image, cv::Scalar color=cv::Scalar(255,255,255)) {
88 
89  // Draw the lines
90  std::vector<cv::Vec4i>::const_iterator it2= lines.begin();
91 
92  while (it2!=lines.end()) {
93 
94  cv::Point pt1((*it2)[0],(*it2)[1]);
95  cv::Point pt2((*it2)[2],(*it2)[3]);
96 
97  cv::line( image, pt1, pt2, color);
98 
99  ++it2;
100  }
101  }
102 
103  // Eliminates lines that do not have an orientation equals to
104  // the ones specified in the input matrix of orientations
105  // At least the given percentage of pixels on the line must
106  // be within plus or minus delta of the corresponding orientation
108  const cv::Mat &orientations, double percentage, double delta) {
109 
110  std::vector<cv::Vec4i>::iterator it= lines.begin();
111 
112  // check all lines
113  while (it!=lines.end()) {
114 
115  // end points
116  int x1= (*it)[0];
117  int y1= (*it)[1];
118  int x2= (*it)[2];
119  int y2= (*it)[3];
120 
121  // line orientation + 90o to get the parallel line
122  double ori1= atan2(static_cast<double>(y1-y2),static_cast<double>(x1-x2))+PI/2;
123  if (ori1>PI) ori1= ori1-2*PI;
124 
125  double ori2= atan2(static_cast<double>(y2-y1),static_cast<double>(x2-x1))+PI/2;
126  if (ori2>PI) ori2= ori2-2*PI;
127 
128  // for all points on the line
129  cv::LineIterator lit(orientations,cv::Point(x1,y1),cv::Point(x2,y2));
130  int i,count=0;
131  for(i = 0, count=0; i < lit.count; i++, ++lit) {
132 
133  float ori= *(reinterpret_cast<float *>(*lit));
134 
135  // is line orientation similar to gradient orientation ?
136  if (std::min(fabs(ori-ori1),fabs(ori-ori2))<delta)
137  count++;
138 
139  }
140 
141  double consistency= count/static_cast<double>(i);
142 
143  // set to zero lines of inconsistent orientation
144  if (consistency < percentage) {
145 
146  (*it)[0]=(*it)[1]=(*it)[2]=(*it)[3]=0;
147 
148  }
149 
150  ++it;
151  }
152 
153  return lines;
154  }
155 };
156 
157 
158 std::vector<cv::Vec4i> convert2Matlab(const std::vector<cv::Vec4i>& ori)
159 {
160  cv::Vec4i vi;
161  std::vector<cv::Vec4i> vout;
162 
163  for(uint i=0;i<ori.size();i++)
164  {
165  vi[0] = ori[i][1];
166  vi[1] = ori[i][0];
167  vi[2] = ori[i][3];
168  vi[3] = ori[i][2];
169 
170  vout.push_back(vi);
171  }
172 
173  return vout;
174 }
175 
176 #endif
double minLength
Definition: linefinder.h:45
int minVote
Definition: linefinder.h:42
std::vector< cv::Vec4i > findLines(cv::Mat &binary)
Definition: linefinder.h:78
double maxGap
Definition: linefinder.h:48
void setAccResolution(double dRho, double dTheta)
Definition: linefinder.h:58
void drawDetectedLines(cv::Mat &image, cv::Scalar color=cv::Scalar(255, 255, 255))
Definition: linefinder.h:87
void setMinVote(int minv)
Definition: linefinder.h:65
std::vector< cv::Vec4i > convert2Matlab(const std::vector< cv::Vec4i > &ori)
Definition: linefinder.h:158
double deltaRho
Definition: linefinder.h:37
void setLineLengthAndGap(double length, double gap)
Definition: linefinder.h:71
double deltaTheta
Definition: linefinder.h:38
std::vector< cv::Vec4i > lines
Definition: linefinder.h:34
std::vector< cv::Vec4i > removeLinesOfInconsistentOrientations(const cv::Mat &orientations, double percentage, double delta)
Definition: linefinder.h:107
#define PI
Definition: linefinder.h:23
cv::Mat img
Definition: linefinder.h:30


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