stabelized_func_arrow_detection.cpp
Go to the documentation of this file.
1 
3 //stabalized functions
4 
5 cv::Point calculate_center_contour(cv::Point2f contour[],unsigned int size_contour)
6 {
7  //falta lançar um erro a dizer que se o contorno for menor que dois pontos
15  unsigned int cont=0;
16  double xx=0,yy=0;
17  cv::Point result;
18  for(cont=0;cont<size_contour;cont++)
19  {
20  xx+=contour[cont].x;
21  yy+=contour[cont].y;
22  }
23  result.x=xx/cont+1;
24  result.y=yy/cont+1;
25 
26  return result;
27 }
28 
30 void Opening(Mat & src, Mat & dest,unsigned int erosion_size)
31 {
41  Mat element = getStructuringElement( MORPH_RECT,Size( 2*erosion_size + 1, 2*erosion_size+1 ),
42  cv::Point( erosion_size, erosion_size ) );
43 
44  dilate( src, src, element );
45  erode( src, src, element );
46 }
47 
49 void closing(Mat & src, Mat & dest,unsigned int erosion_size)
50 {
60  Mat element = getStructuringElement( MORPH_RECT,Size( 2*erosion_size + 1, 2*erosion_size+1 ),
61  cv::Point( erosion_size, erosion_size ) );
62 
63  erode( src, src, element );
64  dilate( src, src, element );
65 }
66 
67 void boundig_box( Mat & src_tresh, Mat & threshold_output)
68 {
79  vector<vector<cv::Point> > contours;
80  vector<Vec4i> hierarchy;
81  // random number
82  RNG rng(12345);
84  findContours( src_tresh, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
85 
87  vector<vector<cv::Point> > contours_poly( contours.size() );
88  vector<Rect> boundRect( contours.size() );
89  vector<cv::Point2f>center( contours.size() );
90  vector<float>radius( contours.size() );
91 
92  for( unsigned i = 0; i < contours.size(); i++ )
93  { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
94  boundRect[i] = boundingRect( Mat(contours_poly[i]) );
95  minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
96  }
97 
99  Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
100 
101  for( unsigned i = 0; i< contours.size(); i++ )
102  {
103  Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
104  drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, cv::Point() );
105  rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
106  circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
107  }
108 
110 
111  imshow( "Contours", drawing );
112 
113 }
114 
115 
116 
118 void descritor( Mat & img_object, Mat & img_scene)
119 {
120 
121 //*****************************************************
123 //*****************************************************
124 
125 
126  //-- Step 1: Detect the keypoints using SURF Detector
127  int minHessian = 1000;
128 
129  SurfFeatureDetector detector( minHessian );
130 
131  std::vector<KeyPoint> keypoints_object, keypoints_scene;
132 
133  detector.detect( img_object, keypoints_object );
134  detector.detect( img_scene, keypoints_scene );
135 
136  //-- Step 2: Calculate descriptors (feature vectors)
137  SurfDescriptorExtractor extractor;
138 
139  Mat descriptors_object, descriptors_scene;
140 
141  extractor.compute( img_object, keypoints_object, descriptors_object );
142  extractor.compute( img_scene, keypoints_scene, descriptors_scene );
143 
144  //-- Step 3: Matching descriptor vectors using FLANN matcher
145  FlannBasedMatcher matcher;
146  std::vector< DMatch > matches;
147  matcher.match( descriptors_object, descriptors_scene, matches );
148 
149  double max_dist = 0; double min_dist = 100;
150 
151  //-- Quick calculation of max and min distances between keypoints
152  for( int i = 0; i < descriptors_object.rows; i++ )
153  { double dist = matches[i].distance;
154  if( dist < min_dist ) min_dist = dist;
155  if( dist > max_dist ) max_dist = dist;
156  }
157 
158  //printf("-- Max dist : %f \n", max_dist );
159  //printf("-- Min dist : %f \n", min_dist );
160 
161  //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
162  std::vector< DMatch > good_matches;
163 
164  for( int i = 0; i < descriptors_object.rows; i++ )
165  { if( matches[i].distance < 2*min_dist )
166  { good_matches.push_back( matches[i]); }
167  }
168 
169  Mat img_matches;
170  drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
171  good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
172  vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
173 
174  //-- Localize the object
175  std::vector<cv::Point2f> obj;
176  std::vector<cv::Point2f> scene;
177 
178  for(unsigned int i = 0; i < good_matches.size(); i++ )
179  {
180  //-- Get the keypoints from the good matches
181  obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
182  scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
183  }
184 
185  std::cout<<"Obj "<<obj.size()<<" scene: "<<scene.size()<<std::endl;
186 //alteração feita pelo jorge! da erro quando obj.size e scene.size <4
187  if(obj.size()>=4 && scene.size()>=4)
188  {
189 
190  Mat H = findHomography( obj, scene, CV_RANSAC );
191 
192  //-- Get the corners from the image_1 ( the object to be "detected" )
193  std::vector<cv::Point2f> obj_corners(4);
194  obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
195  obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
196  std::vector<cv::Point2f> scene_corners(4);
197 
198  perspectiveTransform( obj_corners, scene_corners, H);
199 
200  //-- Draw lines between the corners (the mapped object in the scene - image_2 )
201  line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
202  line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
203  line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
204  line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
205 
206  }
207  //-- Show detected matches
208  imshow( "Good Matches & Object detection", img_matches );
209 
210  }
211 
212 
void closing(Mat &src, Mat &dest, unsigned int erosion_size)
Morphological operation Closing.
void descritor(Mat &img_object, Mat &img_scene)
Find discriptors (Match points)
void Opening(Mat &src, Mat &dest, unsigned int erosion_size)
Morphological operation OPENING.
void boundig_box(Mat &src_tresh, Mat &threshold_output)
FIND THE BOUDING BOX.
cv::Point calculate_center_contour(cv::Point2f contour[], unsigned int size_contour)


arrow_detection
Author(s): César Sousa
autogenerated on Mon Mar 2 2015 01:31:21