ed_sensor_integration
segment_and_show.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
3 #include <ed_sensor_integration/Segment.h>
4 
5 #include <ed/uuid.h>
6 #include <ed_gui_server/GetEntityInfo.h>
7 #include <ed_perception/Classify.h>
8 
9 #include <opencv2/imgproc/imgproc.hpp>
10 #include <opencv2/highgui/highgui.hpp>
11 
12 #include <vector>
13 
14 // ----------------------------------------------------------------------------------------------------
15 
16 bool fullColumnHasColor(const cv::Mat& img, int x, const cv::Vec3b& clr)
17 {
18  for(int y = 0; y < img.rows; ++y)
19  {
20  if (img.at<cv::Vec3b>(y, x) != clr)
21  return false;
22  }
23 
24  return true;
25 }
26 
27 // ----------------------------------------------------------------------------------------------------
28 
29 bool fullRowHasColor(const cv::Mat& img, int y, const cv::Vec3b& clr)
30 {
31  for(int x = 0; x < img.cols; ++x)
32  {
33  if (img.at<cv::Vec3b>(y, x) != clr)
34  return false;
35  }
36 
37  return true;
38 }
39 
40 // ----------------------------------------------------------------------------------------------------
41 
42 cv::Rect getBoundingRect(const cv::Mat& img)
43 {
44  int x_min = 0;
45  for(int x = 0; x < img.cols; ++x)
46  {
47  if (!fullColumnHasColor(img, x, cv::Vec3b(0, 0, 0)))
48  {
49  x_min = x;
50  break;
51  }
52  }
53 
54  int x_max = img.cols - 1;
55  for(int x = x_max; x >= 0; --x)
56  {
57  if (!fullColumnHasColor(img, x, cv::Vec3b(0, 0, 0)))
58  {
59  x_max = x;
60  break;
61  }
62  }
63 
64  int y_min = 0;
65  for(int y = 0; y < img.rows; ++y)
66  {
67  if (!fullRowHasColor(img, y, cv::Vec3b(0, 0, 0)))
68  {
69  y_min = y;
70  break;
71  }
72  }
73 
74  int y_max = img.rows - 1;
75  for(int y = y_max; y >= 0; --y)
76  {
77  if (!fullRowHasColor(img, y, cv::Vec3b(0, 0, 0)))
78  {
79  y_max = y;
80  break;
81  }
82  }
83 
84  return cv::Rect(cv::Point(x_min, y_min), cv::Point(x_max, y_max));
85 }
86 
87 // ----------------------------------------------------------------------------------------------------
88 
89 void mouseCallback(int event, int x, int y, int flags, void* userdata)
90 {
91  if ( event == cv::EVENT_LBUTTONDOWN )
92  {
93  std::cout << x << std::endl;
94  }
95  else if ( event == cv::EVENT_RBUTTONDOWN )
96  {
97  // std::cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
98  }
99  else if ( event == cv::EVENT_MBUTTONDOWN )
100  {
101  // std::cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
102  }
103  else if ( event == cv::EVENT_MOUSEMOVE )
104  {
105 // mouse_pos = cv::Vec2i(x, y);
106  }
107 }
108 
109 // ----------------------------------------------------------------------------------------------------
110 
111 int main(int argc, char **argv)
112 {
113  ros::init(argc, argv, "segment_and_show");
114  ros::NodeHandle nh;
115  ros::ServiceClient cl_segment = nh.serviceClient<ed_sensor_integration::Segment>("ed/kinect/segment");
116  ros::ServiceClient cl_info = nh.serviceClient<ed_gui_server::GetEntityInfo>("ed/gui/get_entity_info");
117  ros::ServiceClient cl_perception = nh.serviceClient<ed_perception::Classify>("ed/classify");
118 
119  ed_sensor_integration::Segment srv;
120  srv.request.max_sensor_range = 2.0;
121 
122  if (!cl_segment.call(srv))
123  {
124  std::cout << "Could not call segment service." << std::endl;
125  return 1;
126  }
127 
129  colors.push_back(cv::Scalar(255, 0, 0));
130  colors.push_back(cv::Scalar(0, 255, 0));
131  colors.push_back(cv::Scalar(0, 0, 255));
132  colors.push_back(cv::Scalar(255, 255, 0));
133  colors.push_back(cv::Scalar(0, 255, 255));
134  colors.push_back(cv::Scalar(255, 0, 255));
135 
136 
137  int i_color = 0;
138  cv::Mat img_combined;
139 
142 
143  for(std::vector<std::string>::const_iterator it = srv.response.entity_ids.begin(); it != srv.response.entity_ids.end(); ++it)
144  {
145  ed::UUID id = *it;
146 
147  ed_gui_server::GetEntityInfo srv;
148  srv.request.id = id.str();
149  srv.request.measurement_image_border = 20;
150 
151  if (!cl_info.call(srv))
152  {
153  std::cout << "Could not call info service (for measurement image)" << std::endl;
154  continue;
155  }
156 
157  cv::Mat img = cv::imdecode(srv.response.measurement_image, CV_LOAD_IMAGE_UNCHANGED);
158 
159  if (!img_combined.data)
160  {
161  img_combined = img;
162  }
163  else
164  {
165  for(int i = 0; i < img.rows * img.cols; ++i)
166  {
167  if (img.at<cv::Vec3b>(i) != cv::Vec3b(0, 0, 0))
168  {
169  img_combined.at<cv::Vec3b>(i) = img.at<cv::Vec3b>(i);
170  }
171  }
172  }
173 
174  cv::Rect roi = getBoundingRect(img);
175  rois.push_back(roi);
176 
177  cv::rectangle(img_combined, roi, colors[i_color], 2);
178  i_color = (i_color + 1) % colors.size();
179 
180  ids.push_back(id.str());
181 
182  cv::Mat img_cropped = cv::imdecode(srv.response.measurement_image_unmasked, CV_LOAD_IMAGE_UNCHANGED);
183  cv::imshow(id.str(), img_cropped);
184  }
185 
186  if (!img_combined.data)
187  {
188  std::cout << "No entities found" << std::endl;
189  return 1;
190  }
191 
192  //Create a window
193  cv::namedWindow("Segmentation", 1);
194 
195  cv::Mat img_combined_resized;
196  cv::resize(img_combined, img_combined_resized, cv::Size(img_combined.cols / 2, img_combined.rows / 2));
197 
198  cv::imshow("Segmentation", img_combined_resized);
199  cv::waitKey();
200 
201  // Try to classify
202  ed_perception::Classify srv_classify;
203  srv_classify.request.ids = ids;
204 
205  if (!cl_perception.call(srv_classify))
206  {
207  std::cout << "Could not classify" << std::endl;
208  }
209  else
210  {
211  for(unsigned int i = 0; i < srv_classify.response.types.size(); ++i)
212  {
213  std::string type = srv_classify.response.types[i];
214  if (type.empty())
215  type = "unknown";
216 
217  std::cout << rois[i].tl() << ": " << type << std::endl;
218 
219  cv::putText(img_combined, type, rois[i].tl(), cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(255, 255, 255), 1);
220  }
221  }
222 
223  //set the callback function for any mouse event
224  cv::setMouseCallback("Segmentation", mouseCallback, NULL);
225 
226  cv::resize(img_combined, img_combined_resized, cv::Size(img_combined.cols / 2, img_combined.rows / 2));
227  cv::imshow("Segmentation", img_combined_resized);
228  cv::waitKey();
229 
230  return 0;
231 }
getBoundingRect
cv::Rect getBoundingRect(const cv::Mat &img)
Definition: segment_and_show.cpp:42
uuid.h
std::string
mouseCallback
void mouseCallback(int event, int x, int y, int flags, void *userdata)
Definition: segment_and_show.cpp:89
vector
std::vector::size
T size(T... args)
std::vector::push_back
T push_back(T... args)
std::cout
ed::UUID
fullColumnHasColor
bool fullColumnHasColor(const cv::Mat &img, int x, const cv::Vec3b &clr)
Definition: segment_and_show.cpp:16
fullRowHasColor
bool fullRowHasColor(const cv::Mat &img, int y, const cv::Vec3b &clr)
Definition: segment_and_show.cpp:29
std::endl
T endl(T... args)
std::vector::begin
T begin(T... args)
std::string::empty
T empty(T... args)
ed::UUID::str
const std::string & str() const
main
int main(int argc, char **argv)
Definition: segment_and_show.cpp:111