rgbd
viewer.cpp
Go to the documentation of this file.
1 #include <opencv2/highgui/highgui.hpp>
2 
3 #include <ros/console.h>
4 #include <ros/duration.h>
5 #include <ros/init.h>
6 #include <ros/master.h>
7 #include <ros/names.h>
8 #include <ros/node_handle.h>
9 #include <ros/rate.h>
10 #include <ros/time.h>
11 
12 #include "rgbd/client.h"
13 #include "rgbd/view.h"
14 
15 int main(int argc, char **argv)
16 {
17  ros::init(argc, argv, "rgbd_viewer");
18  ros::NodeHandle nh_private("~");
19 
20  float rate = 30;
21  nh_private.getParam("rate", rate);
22 
23  rgbd::Client client;
24  client.initialize(ros::names::resolve("rgbd"));
25 
26  rgbd::Image image;
27 
28  ros::WallTime last_master_check = ros::WallTime::now();
29 
30  ros::Rate r(rate);
31  while (ros::ok())
32  {
33  if (ros::WallTime::now() >= last_master_check + ros::WallDuration(1))
34  {
35  last_master_check = ros::WallTime::now();
36  if (!ros::master::check())
37  {
38  ROS_FATAL("Lost connection to master");
39  return 1;
40  }
41  }
42  if (client.nextImage(image))
43  {
44  // Show depth image
45  if (image.getDepthImage().data)
46  cv::imshow("depth_original", image.getDepthImage() / 8);
47 
48  // Show rgb image
49  if (image.getRGBImage().data)
50  cv::imshow("rgb_original", image.getRGBImage());
51 
52  // Show combined image
53  if (image.getDepthImage().data && image.getRGBImage().data)
54  {
55  cv::Mat image_hsv;
56  cv::cvtColor(image.getRGBImage(), image_hsv, cv::COLOR_BGR2HSV);
57 
58  rgbd::View view(image, image_hsv.cols);
59 
60  cv::Mat canvas_hsv(view.getHeight(), view.getWidth(), CV_8UC3, cv::Scalar(0, 0, 0));
61 
62  for(int y = 0; y < view.getHeight(); ++y)
63  {
64  for(int x = 0; x < view.getWidth(); ++x)
65  {
66  float d = view.getDepth(x, y);
67  if (d == d)
68  {
69  cv::Vec3b hsv = image_hsv.at<cv::Vec3b>(y, x);
70  hsv[2] = 255 - static_cast<unsigned char>(d / 8 * 255);
71  canvas_hsv.at<cv::Vec3b>(y, x) = hsv;
72  }
73  }
74  }
75 
76  cv::Mat canvas_bgr;
77  cv::cvtColor(canvas_hsv, canvas_bgr, cv::COLOR_HSV2BGR);
78  cv::imshow("image + depth", canvas_bgr);
79  }
80 
81  cv::waitKey(3);
82  }
83 
84  r.sleep();
85  }
86 
87  return 0;
88 }
rgbd::Image::getDepthImage
const cv::Mat & getDepthImage() const
Get the depth image.
Definition: image.h:72
rgbd::View::getWidth
const int & getWidth() const
Definition: view.h:17
rgbd::View::getDepth
const float & getDepth(int x, int y) const
Definition: view.h:32
rgbd::Image
Definition: image.h:43
rgbd::View::getHeight
const int & getHeight() const
Definition: view.h:22
rgbd::Client::initialize
bool initialize(const std::string &server_name, float timeout=5.0)
Initialize the client.
Definition: client.cpp:34
rgbd::Image::getRGBImage
const cv::Mat & getRGBImage() const
Get the RGB color image.
Definition: image.h:78
view.h
main
int main(int argc, char **argv)
Definition: viewer.cpp:15
rgbd::Client
Client which uses the interfaces of ClientRGBD and ClientSHM.
Definition: client.h:29
rgbd::View
Definition: view.h:11
client.h
rgbd::Client::nextImage
bool nextImage(Image &image)
Get a new Image. If no new image has been received since the last call, no image will be written and ...
Definition: client.cpp:76