tof_camera_viewer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 //##################
19 //#### includes ####
20 
21 // ROS includes
22 #include <ros/ros.h>
23 #include <cv_bridge/CvBridge.h>
25 
26 // ROS message includes
27 #include <sensor_msgs/Image.h>
28 
29 // External includes
30 #include <opencv/cv.h>
31 #include <opencv/highgui.h>
32 
34 #include <sstream>
35 
36 //####################
37 //#### node class ####
39 {
40 private:
42 
46 
47  sensor_msgs::CvBridge cv_bridge_0_;
48  sensor_msgs::CvBridge cv_bridge_1_;
49 
50  IplImage* xyz_image_32F3_;
51  cv::Mat xyz_mat_8U3_;
52  IplImage* grey_image_32F1_;
53  cv::Mat grey_mat_8U3_;
54 
56 
57 public:
61  : m_NodeHandle(node_handle),
62  image_transport_(node_handle),
63  xyz_image_32F3_(0),
64  xyz_mat_8U3_(cv::Mat()),
65  grey_image_32F1_(0),
66  grey_mat_8U3_(cv::Mat()),
67  grey_image_counter_(0)
68  {
70  }
71 
74  {
78 
79  if(cvGetWindowHandle("z data"))cvDestroyWindow("z data");
80  if(cvGetWindowHandle("grey data"))cvDestroyWindow("grey data");
81  }
82 
85  bool init()
86  {
88  cvStartWindowThread();
89  cv::namedWindow("z data");
90  cv::namedWindow("grey data");
91 
92  xyz_image_subscriber_ = image_transport_.subscribe("image_xyz", 1, &CobTofCameraViewerNode::xyzImageCallback, this);
93  grey_image_subscriber_ = image_transport_.subscribe("image_grey", 1, &CobTofCameraViewerNode::greyImageCallback, this);
94 
95  return true;
96  }
97 
101  void greyImageCallback(const sensor_msgs::ImageConstPtr& grey_image_msg)
102  {
105  ROS_INFO("Grey Image Callback");
106 
107  try
108  {
109  grey_image_32F1_ = cv_bridge_0_.imgMsgToCv(grey_image_msg, "passthrough");
110 
111  cv::Mat grey_mat_32F1 = grey_image_32F1_;
112  ipa_Utils::ConvertToShowImage(grey_mat_32F1, grey_mat_8U3_, 1, 0, 800);
113  cv::imshow("grey data", grey_mat_8U3_);
114  int c = cvWaitKey(500);
115  std::cout << c << std::endl;
116  if (c=='s' || c==536871027)
117  {
118  std::stringstream ss;
119  char counterBuffer [50];
120  sprintf(counterBuffer, "%04d", grey_image_counter_);
121  ss << "greyImage8U3_";
122  ss << counterBuffer;
123  ss << ".bmp";
124  cv::imwrite(ss.str().c_str(),grey_mat_8U3_);
125  std::cout << "[tof_camera_viewer] Image " << grey_image_counter_ << " saved." << std::endl;
126  grey_image_counter_++;
127  }
128  }
129  catch (sensor_msgs::CvBridgeException& e)
130  {
131  ROS_ERROR("[tof_camera_viewer] Could not convert from '%s' to '32FC1'.", grey_image_msg->encoding.c_str());
132  }
133  ROS_INFO("Image Processed");
134  }
135 
139  void xyzImageCallback(const sensor_msgs::ImageConstPtr& xyz_image_msg)
140  {
143 
144  try
145  {
146  xyz_image_32F3_ = cv_bridge_1_.imgMsgToCv(xyz_image_msg, "passthrough");
147 
148  cv::Mat xyz_mat_32F3 = xyz_image_32F3_;
149  ipa_Utils::ConvertToShowImage(xyz_mat_32F3, xyz_mat_8U3_, 3);
150  cv::imshow("z data", xyz_mat_8U3_);
151  }
152  catch (sensor_msgs::CvBridgeException& e)
153  {
154  ROS_ERROR("[tof_camera_viewer] Could not convert from '%s' to '32FC1'.", xyz_image_msg->encoding.c_str());
155  }
156 
157  }
158 };
159 
160 //#######################
161 //#### main programm ####
162 int main(int argc, char** argv)
163 {
165  ros::init(argc, argv, "tof_camera_viewer");
166 
168  ros::NodeHandle nh;
169 
171  CobTofCameraViewerNode camera_viewer_node(nh);
172 
173 
175  if (!camera_viewer_node.init()) return 0;
176 
177  ros::spin();
178 
179  return 0;
180 }
181 
182 
183 /*
184  int numPoints = image->width*image->height;
185  sensor_msgs::PointCloud pc_msg;
186  pc_msg.header.stamp = ros::Time::now();
187  pc_msg.points.resize(numPoints);
188 
190  for (int i = 0; i < numPoints; i++)
191  {
192  msg.points[i].x = ((float*)image->imageData)[3*i + 0];
193  msg.points[i].y = ((float*)image->imageData)[3*i + 1];
194  msg.points[i].z = ((float*)image->imageData)[3*i + 2];
195  }
196 */
197 /*
198 IplImage* image = cvCreateImage(cvSize(176, 144), IPL_DEPTH_32F, 3);
199  for (unsigned int i=0; i<msg->points.size(); i++)
200  {
201  ((float*)image->imageData)[3*i+0] = (msg->points[i]).x;
202  ((float*)image->imageData)[3*i+1] = (msg->points[i]).y;
203  ((float*)image->imageData)[3*i+2] = (msg->points[i]).z;
204  }
205  IplImage* image_show = cvCreateImage(cvSize(176, 144), IPL_DEPTH_8U, 3);
206  ipa_Utils::ConvertToShowImage(image, image_show, 3);
207 */
Subscriber subscribe(const std::string &base_topic, uint32_t queue_size, const boost::function< void(const sensor_msgs::ImageConstPtr &)> &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
void xyzImageCallback(const sensor_msgs::ImageConstPtr &xyz_image_msg)
unsigned long ConvertToShowImage(const cv::Mat &source, cv::Mat &dest, int channel=1, double min=-1, double max=-1)
ros::NodeHandle m_NodeHandle
Node handle.
IplImage * grey_image_32F1_
OpenCV image holding the transformed 8bit RGB point cloud.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
sensor_msgs::CvBridge cv_bridge_1_
void greyImageCallback(const sensor_msgs::ImageConstPtr &grey_image_msg)
sensor_msgs::CvBridge cv_bridge_0_
ROSCPP_DECL void spin(Spinner &spinner)
cv::Mat xyz_mat_8U3_
OpenCV image holding the 32bit point cloud.
cv::Mat grey_mat_8U3_
OpenCV image holding the 32bit amplitude values.
~CobTofCameraViewerNode()
Destructor.
#define ROS_INFO(...)
image_transport::Subscriber xyz_image_subscriber_
Subscribes to xyz image data.
int main(int argc, char **argv)
image_transport::Subscriber grey_image_subscriber_
Subscribes to gray image data.
int grey_image_counter_
OpenCV image holding the transformed 8bit RGB amplitude values.
CobTofCameraViewerNode(const ros::NodeHandle &node_handle)
#define ROS_ERROR(...)
image_transport::ImageTransport image_transport_
Image transport instance.


cob_camera_sensors
Author(s): Jan Fischer , Richard Bormann
autogenerated on Thu Mar 19 2020 03:23:05