image_test.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
3 #include <opencv2/core/core.hpp>
4 #include <opencv2/highgui/highgui.hpp>
5 #include <opencv2/imgproc/imgproc.hpp>
6 #include <iostream>
7 
8 using namespace std;
9 using namespace cv;
10 
11 int main(int argc, char** argv)
12 {
13  ros::init(argc, argv, "image_test");
14 
15  Mat input = imread("/home/asus/workingcopies/lar4/src/perception/pedestrians/multimodal_pedestrian_detect/images/outside_test/frame0002.jpg", CV_LOAD_IMAGE_COLOR);
16 //
17  Mat redOnly;
18 
19 // inRange(input, Scalar(75, 90, 0), Scalar(120,170,15), redOnly); // placa verde plastica
20  inRange(input, Scalar(150, 150, 150), Scalar(255,255,255), redOnly); // placa branca
21 
22  //Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
23  vector<std::vector<cv::Point> > contours;
24  Mat contourOutput = redOnly.clone();
25  findContours( contourOutput, contours, CV_RETR_TREE/*EXTERNAL*/, CV_CHAIN_APPROX_NONE );
26 
27  vector<vector<Point> > contours_poly( contours.size() );
28 
29  Mat drawing = Mat::zeros( redOnly.size(), CV_8UC3 );
30 
31  int max_size_triangle=0;
32  for(size_t i = 0; i < contours.size(); i++)
33  {
34  approxPolyDP(Mat(contours[i]), contours_poly[i], 0.01*arcLength(Mat(contours[i]),true), true);
35 // cout << "number of triangles: " << contours_poly.size() << endl;
36 
37 // cout << "size_t: " << i << endl;
38 // drawContours(drawing, contours_poly, i, Scalar(0, 0, 255),CV_FILLED,8); // fill RED
39  if (contours_poly[i].size() == 4) //from all object, only triangles are choosen
40  {
41  cout << "arclength: " << arcLength(Mat(contours[i]),true) << endl;
42  cout << "size_poly: " << contours_poly[i].size() << endl;
43  cout << "size: " << contours[i].size() << endl;
44 // drawContours(drawing, contours_poly, i, Scalar(0, 0, 255),CV_FILLED,8); // fill RED
45  if(i > 0)
46  if( arcLength(Mat(contours[i]),true) > arcLength(Mat(contours[max_size_triangle]),true)) //choose bigger triangle
47  {
48  max_size_triangle = i;
49  }
50  }
51  }
52 
53  double max_width = contours_poly[max_size_triangle][3].x - contours_poly[max_size_triangle][0].x;
54  double min_width = contours_poly[max_size_triangle][2].x - contours_poly[max_size_triangle][1].x;
55  double height = contours_poly[max_size_triangle][1].y - contours_poly[max_size_triangle][0].y;
56 
57  cout << "triangle pixel size: " << max_size_triangle << " " << contours_poly[max_size_triangle] << " " << arcLength(Mat(contours[max_size_triangle]),true) << endl
58  << "max width: " << max_width << endl
59  << "min width: " << min_width << endl
60  << "height: " << height << endl;
61 
62  drawContours(drawing, contours_poly, max_size_triangle, Scalar(255, 255, 255),CV_FILLED,8); // fill WHITE
63 
64 
65  double object_width_percent_laser_given = 1 - (0.02); // (inside value is the percentage, return by the laser)
66 
67  int laser_line_int = object_width_percent_laser_given * height;
68  double laser_line = object_width_percent_laser_given * height;
69 
70 // cout << "laser_line, double: " << laser_line << " int: " << laser_line_int << endl;
71 
72  if(laser_line - laser_line_int >=0.5 )
73  laser_line_int++;
74 
75  int laser_line_position = contours_poly[max_size_triangle][0].y + laser_line_int;
76 
77  //Grayscale matrix
78  Mat grayscaleMat(drawing.size(), CV_8U);
79 
80  //Convert BGR to Gray
81  cvtColor(drawing, grayscaleMat, CV_BGR2GRAY);
82 
83  int k=0;
84  Point left_point;
85  Point right_point;
86 
87  for(int col=contours_poly[max_size_triangle][0].x; col<contours_poly[max_size_triangle][3].x; col++)
88  if(grayscaleMat.at<uchar>(laser_line_position,col) != 0)
89  if (k==0)
90  {
91  left_point.y=laser_line_position;
92  left_point.x=col;
93  k++;
94  }else{
95 
96  right_point.y=laser_line_position;
97  right_point.x=col;
98  }
99 
100 
101  cout << "laser_line, double: " << laser_line << " int: " << laser_line_int << endl;
102  cout << "points, left_point: " << left_point << " right_point: " << right_point << endl;
103 
104  line(input, left_point, right_point, Scalar(0, 0, 255), 1, 8,0);
105 
106  imshow("input", input);
107 // imshow("redOnly", redOnly);
108 // imshow("drawing", drawing);
109 // imshow("drawing1", grayscaleMat);
110  waitKey();
111 
112  // detect squares after filtering...
113  ros::spin();
114  return 0;
115 }
int main(int argc, char **argv)
Definition: image_test.cpp:11


multimodal_pedestrian_detect
Author(s): Rui Azevedo
autogenerated on Mon Mar 2 2015 01:32:27