ed_sensor_integration
tools/segmenter.cpp
Go to the documentation of this file.
2 #include <ed/world_model.h>
3 #include <ed/update_request.h>
5 #include <ed/io/json_reader.h>
6 #include <ed/kinect/updater.h>
7 
9 
10 #include <rgbd/image.h>
11 #include <rgbd/serialization.h>
12 #include <rgbd/view.h>
13 
14 #include <opencv2/highgui/highgui.hpp>
15 
16 #include <tue/config/read.h>
17 #include <tue/config/reader.h>
19 
20 #include <ed/entity.h>
21 
22 #include <fstream>
23 #include <vector>
24 
26 
33 void visualizeSegmentation(const ed::Snapshot& snapshot, const UpdateResult& res, cv::Mat canvas)
34 {
35  cv::Vec3b fill_colour(0, 0, 255); // red
36  cv::Scalar outline_colour_1(255, 255, 255); // white
37  cv::Scalar outline_colour_2(0, 0, 0); // black
38 
39  int depth_width = snapshot.image->getDepthImage().cols;
40  double f = (double)canvas.cols / depth_width;
41 
42  for(unsigned int i = 0; i < res.entity_updates.size(); ++i)
43  {
44  const EntityUpdate& e_update = res.entity_updates[i];
45  if (e_update.pixel_indices.empty())
46  continue;
47 
48  unsigned i_pxl = e_update.pixel_indices[0];
49  cv::Point bb_min(i_pxl % depth_width, i_pxl / depth_width);
50  cv::Point bb_max(i_pxl % depth_width, i_pxl / depth_width);
51 
52  for(std::vector<unsigned int>::const_iterator it = e_update.pixel_indices.begin(); it != e_update.pixel_indices.end(); ++it)
53  {
54  int x = *it % depth_width;
55  int y = *it / depth_width;
56 
57  bb_min.x = std::min(bb_min.x, x);
58  bb_min.y = std::min(bb_min.y, y);
59  bb_max.x = std::max(bb_max.x, x);
60  bb_max.y = std::max(bb_max.y, y);
61 
62  for(double x2 = f * x; x2 < (f * (x + 1)); ++x2)
63  for(double y2 = f * y; y2 < (f * (y + 1)); ++y2)
64  canvas.at<cv::Vec3b>(y2, x2) = fill_colour;
65  }
66 
67  cv::Point d(2, 2);
68  cv::rectangle(canvas, f * bb_min, f * bb_max, cv::Scalar(255, 255, 255), 2);
69  cv::rectangle(canvas, f * bb_min - d, f * bb_max + d, cv::Scalar(0, 0, 0), 2);
70  cv::rectangle(canvas, f * bb_min + d, f * bb_max - d, cv::Scalar(0, 0, 0), 2);
71  }
72 }
73 
77 void usage()
78 {
79  std::cout << "Usage: ed_segmenter IMAGE-FILE-OR-DIRECTORY [WORLDMODEL-NAME] [ENTITY-ID]" << std::endl;
80 }
81 
82 
83 int main(int argc, char **argv)
84 {
85  if (argc != 4)
86  {
87  if (argc == 2)
88  {
89  std::cout << "No worldmodel provided! Using empty worldmodel and no segmentation area" << std::endl;
90  }
91  else
92  {
93  usage();
94  return 1;
95  }
96  }
97 
98  tue::filesystem::Path path = argv[1];
99  if (!path.exists())
100  {
101  std::cerr << "Path '" << path << "' does not exist." << std::endl;
102  return 1;
103  }
104 
105  std::string model_name;
106  ed::WorldModelPtr world_model;
107  std::string entity_id;
109  std::string area_description;
110 
111  if (argc == 4)
112  {
113  model_name = argv[2];
114 
115  try
116  {
117  world_model = ed::loadWorldModel(model_name);
118  }
119  catch (const ed::ModelNotFoundException& e)
120  {
121  std::cerr << "World model '" << model_name << "' could not be loaded." << std::endl;
122  std::cerr << e.what() << std::endl;
123  return 1;
124  }
125 
126  entity_id = argv[3];
127  area_description = "on_top_of " + entity_id;
128  e = world_model->getEntity(entity_id);
129  if (!e)
130  {
131  std::cerr << "Entity '" << entity_id << "' could not be found in world model '" << model_name << "'." << std::endl;
132  return 1;
133  }
134  }
135 
136  ed::SnapshotCrawler crawler(path);
137 
138  Updater updater;
139 
140  std::vector<ed::Snapshot> snapshots;
141 
142  while(true)
143  {
144  ed::Snapshot& snapshot = crawler.current();
145 
146  ed::UpdateRequest update_req;
147  UpdateResult res(update_req);
148 
149  UpdateRequest kinect_update_request;
150  kinect_update_request.area_description = area_description;
151  updater.update(*world_model, snapshot.image, snapshot.sensor_pose, kinect_update_request, res);
152 
153  std::cout << update_req.measurements.size() << std::endl;
154 
155  cv::Mat canvas = snapshot.image->getRGBImage().clone();
156 
157  std::string error_msg = res.error.str();
158  if (error_msg.empty())
159  {
160  visualizeSegmentation(snapshot, res, canvas);
161  }
162  else
163  {
164  std::cerr << error_msg << std::endl;
165  cv::putText(canvas, "Segmentation failed", cv::Point(10, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(255, 255, 255), 1);
166  }
167 
168  cv::imshow("RGB", canvas);
169  char key = cv::waitKey();
170 
171  if (key == 81) // Left arrow
172  {
173  crawler.previous();
174  }
175  else if (key == 83) // Right arrow
176  {
177  crawler.next();
178  }
179  else if (key == 'q')
180  {
181  break;
182  }
183  }
184 
185  return 0;
186 }
EntityUpdate::pixel_indices
std::vector< unsigned int > pixel_indices
Definition: kinect/entity_update.h:16
ed::UpdateRequest::measurements
std::map< UUID, std::vector< MeasurementConstPtr > > measurements
read.h
ed::UpdateRequest
fstream
std::string
EntityUpdate
collection structure for laser entities
Definition: kinect/entity_update.h:9
ed::Snapshot
The Snapshot struct, of a camera image taken at a single point in time.
Definition: snapshot.h:47
ed::Snapshot::sensor_pose
geo::Pose3D sensor_pose
Definition: snapshot.h:50
update_request.h
tue::filesystem::Path
vector
std::vector::size
T size(T... args)
reader.h
std::cerr
tue::filesystem::Path::exists
bool exists() const
crawler.h
ed::loadWorldModel
ed::WorldModelPtr loadWorldModel(const std::string &model_name)
Definition: snapshot.h:115
ed::EntityConstPtr
shared_ptr< const Entity > EntityConstPtr
std::cout
UpdateResult::entity_updates
std::vector< EntityUpdate > entity_updates
Definition: kinect/updater.h:38
Updater
Definition: kinect/updater.h:46
image.h
main
int main(int argc, char **argv)
Definition: tools/segmenter.cpp:83
ed::SnapshotCrawler
Definition: snapshot.h:137
ed::SnapshotCrawler::next
void next()
next, proceed to next index of the snapshot list. If index is out of bounds, try to load a new snapsh...
Definition: snapshot.h:168
UpdateResult::error
std::stringstream error
Definition: kinect/updater.h:41
serialization.h
visualizeSegmentation
void visualizeSegmentation(const ed::Snapshot &snapshot, const UpdateResult &res, cv::Mat canvas)
visualizeSegmentation paint segmentation result over an image
Definition: tools/segmenter.cpp:33
ed::SnapshotCrawler::previous
void previous()
Definition: snapshot.h:159
UpdateRequest::area_description
std::string area_description
Definition: kinect/updater.h:18
data_pointer.h
std::min
T min(T... args)
ed::WorldModelPtr
shared_ptr< WorldModel > WorldModelPtr
ed::Snapshot::image
rgbd::ImagePtr image
Definition: snapshot.h:49
view.h
world_model.h
std::endl
T endl(T... args)
Updater::update
bool update(const ed::WorldModel &world, const rgbd::ImageConstPtr &image, const geo::Pose3D &sensor_pose, const UpdateRequest &req, UpdateResult &res)
Definition: kinect/updater.cpp:201
std::vector::begin
T begin(T... args)
UpdateRequest
Definition: kinect/updater.h:13
snapshot.h
ed::SnapshotCrawler::current
Snapshot & current()
Definition: snapshot.h:156
usage
void usage()
usage: instruct user how to use the executable
Definition: tools/segmenter.cpp:77
std::vector::empty
T empty(T... args)
std::stringstream::str
T str(T... args)
std::vector::end
T end(T... args)
std::max
T max(T... args)
updater.h
entity.h
ed::ModelNotFoundException
Definition: snapshot.h:29
model_loader.h
json_reader.h
UpdateResult
Definition: kinect/updater.h:34
ed::ModelNotFoundException::what
const char * what() const
Definition: snapshot.h:39