featureDetection.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string.h>
5 
6 using namespace cv;
7 
9 
10  _detectedEdges = Mat();
11  _width = 800;
12  _height = 600;
13  _LMWidth = 10;
14  _thres = 40;
15  _rho = 1;
16  _theta = CV_PI/180.0;
17  _houghThres =100;
18  _ransacThres = 0.01;
19 }
20 
22 
23 }
24 
26 
27 // This is just one approach. Other approaches can be Edge detection, Connected Components, etc.
29  Mat img;
30 
32  Mat mask = Mat(src.size(), CV_8UC1, Scalar(1));
33  for(int i = 0;i < mask.rows/2; i++){
34  for(int j = 0;j < mask.cols;j++){
35  mask.at<uchar>(Point(j,i)) = 0;
36  }
37  }
38  src.copyTo(img,mask);
40 
41  _detectedEdges = Mat(img.size(),CV_8UC1); // detectedEdges
42  _detectedEdges.setTo(0);
43 
44  int val = 0;
45  // iterating through each row
46  for (int j = img.rows/2;j<img.rows;j++){
47  unsigned char *imgptr = img.ptr<uchar>(j);
48  unsigned char *detEdgesptr = _detectedEdges.ptr<uchar>(j);
49 
50  // iterating through each column seeing the difference among columns which are "width" apart
51  for (int i = _LMWidth;i < img.cols - _LMWidth; ++i){
52  if(imgptr[i]!= 0){
53  val = 2*imgptr[i];
54  val += -imgptr[i - _LMWidth];
55  val += -imgptr[i + _LMWidth];
56  val += -abs((int)(imgptr[i - _LMWidth] - imgptr[i + _LMWidth]));
57 
58  val = (val<0)?0:val;
59  val = (val>255)?255:val;
60 
61  detEdgesptr[i] = (unsigned char) val;
62  }
63  }
64  }
65 
66  // Thresholding
67  threshold(_detectedEdges,_detectedEdges,_thres,255,0);
68 
69 }
71 
72 // Performing Hough Transform
74 
75  Mat _detectedEdgesRGB;
76  cvtColor(_detectedEdges,_detectedEdgesRGB, CV_GRAY2BGR); // converting to RGB
77  HoughLines(_detectedEdges,_lines,_rho,_theta,_houghThres); // Finding the hough lines
78  vector<Vec2f> retVar;
79 
80  if (_lines.size() > 1){
81  Mat labels,centers;
82  Mat samples = Mat(_lines.size(),2,CV_32F);
83 
84  for (int i = 0;i < _lines.size();i++){
85  samples.at<float>(i,0) = _lines[i][0];
86  samples.at<float>(i,1) = _lines[i][1];
87  }
88  // K means clustering to get two lines
89  kmeans(samples, 2, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 1000, 0.001), 5, KMEANS_PP_CENTERS, centers );
90 
92  _lines.clear();
93 
94  vector<Point2f> left;
95  vector<Point2f> right;
96  for(int i = 0;i < labels.rows; i++){
97  if (labels.at<int>(i) == 0) left.push_back(Point2f(samples.at<float>(i,0), samples.at<float>(i,1)));
98  else right.push_back(Point2f(samples.at<float>(i,0), samples.at<float>(i,1)));
99  }
100  // Performing Ransac
101  vector<Point2f> leftR = ransac(left);
102  vector<Point2f> rightR = ransac(right);
104 
105  if (leftR.size() < 1 || rightR.size() < 1 ||
106  (float)(cos((leftR[0].y + leftR[1].y)/2) * cos((rightR[0].y + rightR[1].y)/2)) >= 0) return retVar;
107 
108  // pushing the end points of the line to _lines
109  _lines.push_back(Vec2f((leftR[0].x + leftR[1].x)/2, (leftR[0].y + leftR[1].y)/2));
110  _lines.push_back(Vec2f((rightR[0].x + rightR[1].x)/2, (rightR[0].y + rightR[1].y)/2));
111 
112  }
113 
114 
115  return _lines;
116 }
117 
118 // Implementing RANSAC to remove outlier lines
119 // Picking the best estimate having maximum number of inliers
120 // TO DO: Better implementation
121 vector<Point2f> featureDetection::ransac(vector<Point2f> data){
122 
123  vector<Point2f> res;
124  int maxInliers = 0;
125 
126  // Picking up the first sample
127  for(int i = 0;i < data.size();i++){
128  Point2f p1 = data[i];
129 
130  // Picking up the second sample
131  for(int j = i + 1;j < data.size();j++){
132  Point2f p2 = data[j];
133  int n = 0;
134 
135  // Finding the total number of inliers
136  for (int k = 0;k < data.size();k++){
137  Point2f p3 = data[k];
138  float normalLength = norm(p2 - p1);
139  float distance = abs((float)((p3.x - p1.x) * (p2.y - p1.y) - (p3.y - p1.y) * (p2.x - p1.x)) / normalLength);
140  if (distance < _ransacThres) n++;
141  }
142 
143  // if the current selection has more inliers, update the result and maxInliers
144  if (n > maxInliers) {
145  res.clear();
146  maxInliers = n;
147  res.push_back(p1);
148  res.push_back(p2);
149  }
150 
151  }
152 
153  }
154 
155  return res;
156 }
157 
158 
159 Mat featureDetection::lineItr(Mat img, vector<Vec2f> lines, string name){
160 
161  Mat imgRGB;
162  cvtColor(img,imgRGB,CV_GRAY2RGB); // converting the image to RGB for display
163  vector<Point> endPoints;
164 
165  // Here, I convert the polar coordinates to Cartesian coordinates.
166  // Then, I extend the line to meet the boundary of the image.
167  for (int i = 0;i < lines.size();i++){
168  float r = lines[i][0];
169  float t = lines[i][1];
170 
171  float x = r*cos(t);
172  float y = r*sin(t);
173 
174  Point p1(cvRound(x - 1.0*sin(t)*1000), cvRound(y + cos(t)*1000));
175  Point p2(cvRound(x + 1.0*sin(t)*1000), cvRound(y - cos(t)*1000));
176 
177  clipLine(img.size(),p1,p2);
178  if (p1.y > p2.y){
179  endPoints.push_back(p1);
180  endPoints.push_back(p2);
181  }
182  else{
183  endPoints.push_back(p2);
184  endPoints.push_back(p1);
185  }
186 
187  }
188 
189 
190  Point pint;
191  bool check = findIntersection(endPoints,pint);
192 
193  if (check){
194  line(imgRGB,endPoints[0],pint,Scalar(0,0,255),5);
195  line(imgRGB,endPoints[2],pint,Scalar(0,0,255),5);
196  }
198 
199 
200  float xIntercept = min(endPoints[0].x,endPoints[2].x);
201  myfile << name << "," << xIntercept * 2 << "," << pint.x * 2 << endl;
202 
203  visualize(imgRGB); // Visualize the final result
204 
205  return imgRGB;
206 }
207 
208 // Finding the Vanishing Point
209 bool featureDetection::findIntersection(vector<Point> endP, Point& pi){
210 
211  Point x = endP[2] - endP[0];
212  Point d1 = endP[1] - endP[0];
213  Point d2 = endP[3] - endP[2];
214 
215  float cross = d1.x*d2.y - d1.y*d2.x;
216  if (abs(cross) < 1e-8) // No intersection
217  return false;
218 
219  double t1 = (x.x * d2.y - x.y * d2.x)/cross;
220  pi = endP[0] + d1 * t1;
221  return true;
222 
223 
224 }
225 
226 // Visualize
228 
229  namedWindow("detectedFeatures");
230  imshow("detectedFeatures",imgRGB);
231  waitKey(100);
232 
233 }
234 
235 #ifdef testT
236 int main()
237 {
238  featureDetection detect; // object of featureDetection class
239 
240 
241  ippath += imname;
242  Mat img1 = imread(ippath,0); // Read the image
243  resize(img1,img1,Size(detect._width,detect._height));
244 
245  detect.filteringPipeLine(img1);
246  vector<Vec2f> lines = detect.houghTransform(); // Hough Transform
247  Mat imgFinal = detect.lineItr(img1, lines, imname);
248 
249  oppath += imname;
250  imwrite(oppath,imgFinal);
251 
252  while ( getline (imageNames,imname) ){
253  ippath = "./images/";
254  oppath = "./output/";
255  ippath += imname;
256 
257  Mat img2 = imread(ippath,0); // Read the image
258  resize(img2,img2,Size(detect._width,detect._height));
259 
260  detect.filteringPipeLine(img2);
261  vector<Vec2f> lines2 = detect.houghTransform(); // Hough Transform
262 
263 
264 
265  if (lines2.size() < 2) {
266  imgFinal = detect.lineItr(img2,lines, imname);
267  oppath += imname;
268  imwrite(oppath,imgFinal);
269  continue;
270  }
271 
273  CKalmanFilter KF2(lines);
274  vector<Vec2f> pp = KF2.predict();
275 
276  vector<Vec2f> lines2Final = KF2.update(lines2);
277  lines = lines2Final;
278  imgFinal = detect.lineItr(img2,lines2, imname);
280 
281  oppath += imname;
282  imwrite(oppath,imgFinal);
283  }
284 
285 
286 }
287 #endif
ROSCPP_DECL bool check()
bool findIntersection(vector< Point >, Point &)
TFSIMD_FORCE_INLINE Vector3 cross(const Vector3 &v) const
cv::KalmanFilter KF2(stateDim, measDim, ctrlDim, CV_32F)
int main(int argc, char **argv)
Definition: main.cpp:703
void filteringPipeLine(Mat)
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
Mat lineItr(Mat, vector< Vec2f >, string)
TFSIMD_FORCE_INLINE const tfScalar & y() const
vector< Vec2f > update(vector< Vec2f >)
TFSIMD_FORCE_INLINE const tfScalar & x() const
TFSIMD_FORCE_INLINE const tfScalar & y() const
TFSIMD_FORCE_INLINE const tfScalar & x() const
vector< Point2f > ransac(vector< Point2f >)
int min(int a, int b)
vector< Vec2f > houghTransform()
vector< Vec2f > predict()


multi_object_tracking_lidar
Author(s): Praveen Palanisamy
autogenerated on Sat Jul 18 2020 03:29:17