tof_camera_viewer.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 //##################
00019 //#### includes ####
00020 
00021 // ROS includes
00022 #include <ros/ros.h>
00023 #include <cv_bridge/CvBridge.h>
00024 #include <image_transport/image_transport.h>
00025 
00026 // ROS message includes
00027 #include <sensor_msgs/Image.h>
00028 
00029 // External includes
00030 #include <opencv/cv.h>
00031 #include <opencv/highgui.h>
00032 
00033 #include <cob_vision_utils/VisionUtils.h>
00034 #include <sstream>
00035 
00036 //####################
00037 //#### node class ####
00038 class CobTofCameraViewerNode
00039 {
00040 private:
00041         ros::NodeHandle m_NodeHandle;   
00042 
00043         image_transport::ImageTransport image_transport_;       
00044         image_transport::Subscriber xyz_image_subscriber_;      
00045         image_transport::Subscriber grey_image_subscriber_;     
00046 
00047         sensor_msgs::CvBridge cv_bridge_0_;
00048         sensor_msgs::CvBridge cv_bridge_1_;
00049 
00050         IplImage* xyz_image_32F3_;      
00051         cv::Mat xyz_mat_8U3_;           
00052         IplImage* grey_image_32F1_;     
00053         cv::Mat grey_mat_8U3_;  
00054 
00055         int grey_image_counter_;
00056 
00057 public:
00060         CobTofCameraViewerNode(const ros::NodeHandle& node_handle)
00061         : m_NodeHandle(node_handle),
00062           image_transport_(node_handle),
00063           xyz_image_32F3_(0),
00064           xyz_mat_8U3_(cv::Mat()),
00065           grey_image_32F1_(0),
00066           grey_mat_8U3_(cv::Mat()),
00067           grey_image_counter_(0)
00068         {
00070         }
00071 
00073         ~CobTofCameraViewerNode()
00074         {
00078 
00079                 if(cvGetWindowHandle("z data"))cvDestroyWindow("z data");
00080                 if(cvGetWindowHandle("grey data"))cvDestroyWindow("grey data");
00081         }
00082 
00085         bool init()
00086         {
00088                 cvStartWindowThread();
00089                 cv::namedWindow("z data");
00090                 cv::namedWindow("grey data");
00091 
00092                 xyz_image_subscriber_ = image_transport_.subscribe("image_xyz", 1, &CobTofCameraViewerNode::xyzImageCallback, this);
00093                 grey_image_subscriber_ = image_transport_.subscribe("image_grey", 1, &CobTofCameraViewerNode::greyImageCallback, this);
00094 
00095                 return true;
00096         }
00097 
00101         void greyImageCallback(const sensor_msgs::ImageConstPtr& grey_image_msg)
00102         {
00105                 ROS_INFO("Grey Image Callback");
00106 
00107                 try
00108                 {
00109                         grey_image_32F1_ = cv_bridge_0_.imgMsgToCv(grey_image_msg, "passthrough");
00110 
00111                         cv::Mat grey_mat_32F1 = grey_image_32F1_;
00112                         ipa_Utils::ConvertToShowImage(grey_mat_32F1, grey_mat_8U3_, 1, 0, 800);
00113                         cv::imshow("grey data", grey_mat_8U3_);
00114                         int c = cvWaitKey(500);
00115                         std::cout << c << std::endl;
00116                         if (c=='s' || c==536871027)
00117                         {
00118                                 std::stringstream ss;
00119                                 char counterBuffer [50];
00120                                 sprintf(counterBuffer, "%04d", grey_image_counter_);
00121                                 ss << "greyImage8U3_";
00122                                 ss << counterBuffer;
00123                                 ss << ".bmp";
00124                                 cv::imwrite(ss.str().c_str(),grey_mat_8U3_);
00125                                 std::cout << "[tof_camera_viewer] Image " << grey_image_counter_ << " saved." << std::endl;
00126                                 grey_image_counter_++;
00127                         }
00128                 }
00129                 catch (sensor_msgs::CvBridgeException& e)
00130                 {
00131                         ROS_ERROR("[tof_camera_viewer] Could not convert from '%s' to '32FC1'.", grey_image_msg->encoding.c_str());
00132                 }
00133                 ROS_INFO("Image Processed");
00134         }
00135 
00139         void xyzImageCallback(const sensor_msgs::ImageConstPtr& xyz_image_msg)
00140         {
00143 
00144                 try
00145                 {
00146                         xyz_image_32F3_ = cv_bridge_1_.imgMsgToCv(xyz_image_msg, "passthrough");
00147 
00148                         cv::Mat xyz_mat_32F3 = xyz_image_32F3_;
00149                         ipa_Utils::ConvertToShowImage(xyz_mat_32F3, xyz_mat_8U3_, 3);
00150                         cv::imshow("z data", xyz_mat_8U3_);
00151                 }
00152                 catch (sensor_msgs::CvBridgeException& e)
00153                 {
00154                         ROS_ERROR("[tof_camera_viewer] Could not convert from '%s' to '32FC1'.", xyz_image_msg->encoding.c_str());
00155                 }
00156 
00157         }
00158 };
00159 
00160 //#######################
00161 //#### main programm ####
00162 int main(int argc, char** argv)
00163 {
00165         ros::init(argc, argv, "tof_camera_viewer");
00166 
00168         ros::NodeHandle nh;
00169 
00171         CobTofCameraViewerNode camera_viewer_node(nh);
00172 
00173 
00175         if (!camera_viewer_node.init()) return 0;
00176 
00177         ros::spin();
00178 
00179         return 0;
00180 }
00181 
00182 
00183 /*
00184  int numPoints = image->width*image->height;
00185  sensor_msgs::PointCloud pc_msg;
00186  pc_msg.header.stamp = ros::Time::now();
00187  pc_msg.points.resize(numPoints);
00188 
00190  for (int i = 0; i < numPoints; i++)
00191  {
00192  msg.points[i].x = ((float*)image->imageData)[3*i + 0];
00193  msg.points[i].y = ((float*)image->imageData)[3*i + 1];
00194  msg.points[i].z = ((float*)image->imageData)[3*i + 2];
00195  }
00196 */
00197 /*
00198 IplImage* image = cvCreateImage(cvSize(176, 144), IPL_DEPTH_32F, 3);
00199  for (unsigned int i=0; i<msg->points.size(); i++)
00200  {
00201  ((float*)image->imageData)[3*i+0] = (msg->points[i]).x;
00202  ((float*)image->imageData)[3*i+1] = (msg->points[i]).y;
00203  ((float*)image->imageData)[3*i+2] = (msg->points[i]).z;
00204  }
00205  IplImage* image_show = cvCreateImage(cvSize(176, 144), IPL_DEPTH_8U, 3);
00206  ipa_Utils::ConvertToShowImage(image, image_show, 3);
00207 */


cob_camera_sensors
Author(s): Jan Fischer , Richard Bormann
autogenerated on Sat Jun 8 2019 21:02:02