26 #include <cv_bridge/CvBridge.h> 37 #include <sensor_msgs/Image.h> 38 #include <sensor_msgs/CameraInfo.h> 41 #include <cob_camera_sensors/AcquireCalibrationImages.h> 46 #include <opencv/highgui.h> 47 #include <boost/thread/mutex.hpp> 126 : node_handle_(node_handle),
127 image_transport_(node_handle),
129 shared_sub_sync_(ThreeImageSyncPolicy(3)),
130 stereo_sub_sync_(TwoImageSyncPolicy(3)),
131 all_sub_sync_(ThreeImageSyncPolicy(3)),
138 right_color_image_8U3_(0),
139 left_color_image_8U3_(0),
143 use_tof_camera_ =
true;
144 use_left_color_camera_ =
true;
145 use_right_color_camera_ =
true;
147 tof_image_counter_ = 0;
161 if (loadParameters() ==
false)
return false;
173 if(use_right_color_camera_ && use_tof_camera_ && !use_left_color_camera_)
175 ROS_INFO(
"[all_camera_viewer] Setting up subscribers for right color and tof camera");
176 shared_sub_sync_.connectInput(right_color_camera_image_sub_, tof_camera_grey_image_sub_);
179 else if(use_right_color_camera_ && !use_tof_camera_ && use_left_color_camera_)
181 ROS_INFO(
"[all_camera_viewer] Setting up subscribers left and right color camera");
182 stereo_sub_sync_.connectInput(left_color_camera_image_sub_, right_color_camera_image_sub_);
185 else if(use_right_color_camera_ && use_tof_camera_ && use_left_color_camera_)
187 ROS_INFO(
"[all_camera_viewer] Setting up subscribers for left color, right color and tof camera");
188 all_sub_sync_.connectInput(left_color_camera_image_sub_, right_color_camera_image_sub_, tof_camera_grey_image_sub_);
193 ROS_ERROR(
"[all_camera_viewer] Specified camera configuration not available");
200 ROS_INFO(
"[all_camera_viewer] Initializing [OK]");
207 if (sub_counter_ == 0)
210 ROS_DEBUG(
"[all_camera_viewer] Subscribing to camera topics");
212 if (use_right_color_camera_)
214 right_color_camera_image_sub_.
subscribe(image_transport_,
"right/image_color", 1);
215 right_camera_info_sub_.
subscribe(node_handle_,
"right/camera_info", 1);
217 if (use_left_color_camera_)
219 left_color_camera_image_sub_.
subscribe(image_transport_,
"left/image_color", 1);
220 left_camera_info_sub_.
subscribe(node_handle_,
"left/camera_info", 1);
224 tof_camera_grey_image_sub_.
subscribe(image_transport_,
"image_grey", 1);
233 if (sub_counter_ == 0)
235 ROS_DEBUG(
"[all_camera_viewer] Unsubscribing from camera topics");
237 if (use_right_color_camera_)
242 if (use_left_color_camera_)
255 const sensor_msgs::ImageConstPtr& right_camera_data,
256 const sensor_msgs::ImageConstPtr& tof_camera_grey_data)
258 ROS_INFO(
"[all_camera_viewer] allModeSrvCallback");
259 boost::mutex::scoped_lock lock(m_ServiceMutex);
263 right_color_image_8U3_ = cv_bridge_0_.imgMsgToCv(right_camera_data,
"bgr8");
264 left_color_image_8U3_ = cv_bridge_1_.imgMsgToCv(left_camera_data,
"bgr8");
265 grey_image_32F1_ = cv_bridge_2_.imgMsgToCv(tof_camera_grey_data,
"passthrough");
267 cv::Mat tmp = right_color_image_8U3_;
268 right_color_mat_8U3_ = tmp.clone();
270 tmp = left_color_image_8U3_;
271 left_color_mat_8U3_ = tmp.clone();
273 tmp = grey_image_32F1_;
274 grey_mat_32F1_ = tmp.clone();
276 catch (sensor_msgs::CvBridgeException& e)
278 ROS_ERROR(
"[all_camera_viewer] Could not convert stereo images with cv_bridge.");
283 updateTofGreyscaleData();
285 cv::imshow(
"TOF grey data", grey_mat_8U1_);
287 cv::Mat right_color_8U3;
288 cv::resize(right_color_mat_8U3_, right_color_8U3, cv::Size(), 0.5, 0.5);
289 cv::imshow(
"Right color data", right_color_8U3);
291 cv::Mat left_color_8U3;
292 cv::resize(left_color_mat_8U3_, left_color_8U3, cv::Size(), 0.5, 0.5);
293 cv::imshow(
"Left color data", left_color_8U3);
296 ROS_INFO(
"[all_camera_viewer] allModeSrvCallback [OK]");
302 const sensor_msgs::ImageConstPtr& tof_camera_grey_data)
304 boost::mutex::scoped_lock lock(m_ServiceMutex);
305 ROS_INFO(
"[all_camera_viewer] sharedModeSrvCallback");
309 right_color_image_8U3_ = cv_bridge_0_.imgMsgToCv(right_camera_data,
"passthrough");
310 grey_image_32F1_ = cv_bridge_2_.imgMsgToCv(tof_camera_grey_data,
"passthrough");
312 cv::Mat tmp = right_color_image_8U3_;
313 right_color_mat_8U3_ = tmp.clone();
315 tmp = grey_image_32F1_;
316 grey_mat_32F1_ = tmp.clone();
318 catch (sensor_msgs::CvBridgeException& e)
320 ROS_ERROR(
"[all_camera_viewer] Could not convert images with cv_bridge.");
325 updateTofGreyscaleData();
327 cv::imshow(
"TOF grey data", grey_mat_8U1_);
329 cv::Mat right_color_8U3;
330 cv::resize(right_color_mat_8U3_, right_color_8U3, cv::Size(), 0.5, 0.5);
331 cv::imshow(
"Right color data", right_color_8U3);
340 cv::Mat filtered_grey_mat_32F1 = cv::Mat::zeros(grey_mat_32F1_.rows, grey_mat_32F1_.cols, CV_8UC1);
343 if (vec_grey_mat_32F1_.size() <= (
unsigned int) tof_image_counter_)
344 vec_grey_mat_32F1_.push_back(grey_mat_32F1_);
346 vec_grey_mat_32F1_[tof_image_counter_] = grey_mat_32F1_;
348 tof_image_counter_ = (++tof_image_counter_)%5;
351 for (
unsigned int i=0; i<vec_grey_mat_32F1_.size(); i++)
352 filtered_grey_mat_32F1 += vec_grey_mat_32F1_[i];
353 filtered_grey_mat_32F1 * (1/vec_grey_mat_32F1_.size());
356 cv::Mat temp0_grey_mat_8U1;
357 cv::Mat temp1_grey_mat_8U1;
372 grey_mat_8U1_ = filtered_grey_mat_32F1;
379 const sensor_msgs::ImageConstPtr& right_camera_data)
381 ROS_INFO(
"[all_camera_viewer] stereoModeSrvCallback");
382 boost::mutex::scoped_lock lock(m_ServiceMutex);
386 right_color_image_8U3_ = cv_bridge_0_.imgMsgToCv(right_camera_data,
"passthrough");
387 left_color_image_8U3_ = cv_bridge_1_.imgMsgToCv(left_camera_data,
"passthrough");
389 cv::Mat tmp = right_color_image_8U3_;
390 right_color_mat_8U3_ = tmp.clone();
392 tmp = left_color_image_8U3_;
393 left_color_mat_8U3_ = tmp.clone();
395 catch (sensor_msgs::CvBridgeException& e)
397 ROS_ERROR(
"[all_camera_viewer] Could not convert stereo images with cv_bridge.");
400 cv::Mat right_color_8U3;
401 cv::resize(right_color_mat_8U3_, right_color_8U3, cv::Size(), 0.5, 0.5);
402 cv::imshow(
"Right color data", right_color_8U3);
404 cv::Mat left_color_8U3;
405 cv::resize(left_color_mat_8U3_, left_color_8U3, cv::Size(), 0.5, 0.5);
406 cv::imshow(
"Left color data", left_color_8U3);
409 ROS_INFO(
"[all_camera_viewer] stereoModeSrvCallback [OK]");
413 cob_camera_sensors::AcquireCalibrationImages::Response &res)
415 ROS_INFO(
"[all_camera_viewer] Service Callback");
416 boost::mutex::scoped_lock lock(m_ServiceMutex);
418 if (req.reset_image_counter) image_counter_ = 0;
420 char counterBuffer [50];
421 sprintf(counterBuffer,
"%04d", image_counter_);
423 if (use_right_color_camera_ && right_color_mat_8U3_.empty())
425 ROS_INFO(
"[all_camera_viewer] Right color image not available");
430 std::stringstream ss;
431 ss <<
"right_color_image_";
434 cv::imwrite(absolute_output_directory_path_ + ss.str(), right_color_mat_8U3_);
435 ROS_INFO(
"[all_camera_viewer] Saved right color image %d to %s", image_counter_, ss.str().c_str());
438 if (use_left_color_camera_ && left_color_mat_8U3_.empty())
440 ROS_INFO(
"[all_camera_viewer] Left color image not available");
445 std::stringstream ss;
446 ss <<
"left_color_image_";
449 cv::imwrite(absolute_output_directory_path_ + ss.str(), left_color_mat_8U3_);
450 ROS_INFO(
"[all_camera_viewer] Saved left color image %d to %s", image_counter_, ss.str().c_str());
454 if (use_tof_camera_ && grey_mat_8U1_.empty())
456 ROS_INFO(
"[all_camera_viewer] Tof grayscale image not available");
461 std::stringstream ss;
462 ss <<
"tof_grey_image_";
465 cv::imwrite(absolute_output_directory_path_ + ss.str(), grey_mat_8U1_);
466 ROS_INFO(
"[all_camera_viewer] Saved tof gray image %d to %s", image_counter_, ss.str().c_str());
476 std::string tmp_string;
479 if (node_handle_.
getParam(
"all_camera_viewer/use_tof_camera", use_tof_camera_) ==
false)
481 ROS_ERROR(
"[all_camera_viewer] 'use_tof_camera' not specified");
484 ROS_INFO(
"use tof camera: %d", use_tof_camera_);
486 if (node_handle_.
getParam(
"all_camera_viewer/use_right_color_camera", use_right_color_camera_) ==
false)
488 ROS_ERROR(
"[all_camera_viewer] 'use_right_color_camera' not specified");
491 ROS_INFO(
"use right color camera: %d", use_right_color_camera_);
493 if (node_handle_.
getParam(
"all_camera_viewer/use_left_color_camera", use_left_color_camera_) ==
false)
495 ROS_ERROR(
"[all_camera_viewer] 'use_left_color_camera' not specified");
498 ROS_INFO(
"use left color camera: %d", use_left_color_camera_);
500 if (node_handle_.
getParam(
"all_camera_viewer/absolute_output_directory_path", absolute_output_directory_path_) ==
false)
502 ROS_ERROR(
"[all_camera_viewer] 'absolute_output_directory_path' not specified");
505 ROS_INFO(
"use left color camera: %d", use_left_color_camera_);
514 int main(
int argc,
char** argv)
517 ros::init(argc, argv,
"all_camera_viewer");
526 if (!camera_node.
init())
return 0;
bool use_right_color_camera_
cv::Mat grey_mat_8U1_
Received gray values from tof sensor.
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::Image > shared_sub_sync_
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::Image > stereo_sub_sync_
cv::Mat grey_mat_32F1_
Received gray values from tof sensor.
std::string absolute_output_directory_path_
Directory, where camera images are saved.
ros::ServiceServer save_camera_images_service_
image_transport::Subscriber sub
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
void stereoModeSrvCallback(const sensor_msgs::ImageConstPtr &left_camera_data, const sensor_msgs::ImageConstPtr &right_camera_data)
IplImage * xyz_image_32F3_
Received point cloud form tof sensor.
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
unsigned long loadParameters()
int main(int argc, char **argv)
ROSCPP_DECL void spin(Spinner &spinner)
sensor_msgs::CvBridge cv_bridge_0_
Converts ROS image messages to openCV IplImages.
void updateTofGreyscaleData()
int tof_image_counter_
Number of subscribers to topic.
image_transport::SubscriberFilter right_color_camera_image_sub_
Right color camera image topic.
message_filters::TimeSynchronizer< sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::Image > all_sub_sync_
void sharedModeSrvCallback(const sensor_msgs::ImageConstPtr &right_camera_data, const sensor_msgs::ImageConstPtr &tof_camera_grey_data)
void allModeSrvCallback(const sensor_msgs::ImageConstPtr &left_camera_data, const sensor_msgs::ImageConstPtr &right_camera_data, const sensor_msgs::ImageConstPtr &tof_camera_grey_data)
IplImage * left_color_image_8U3_
Received color image of the left camera.
void disconnectCallback()
Unsubscribe from camera topics if possible.
sensor_msgs::CvBridge cv_bridge_1_
Converts ROS image messages to openCV IplImages.
void subscribe(ImageTransport &it, const std::string &base_topic, uint32_t queue_size, const TransportHints &transport_hints=TransportHints())
cv::Mat right_color_mat_8U3_
Received color image of the right camera.
AllCameraViewer(const ros::NodeHandle &node_handle)
Constructor.
void subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0)
bool saveCameraImagesServiceCallback(cob_camera_sensors::AcquireCalibrationImages::Request &req, cob_camera_sensors::AcquireCalibrationImages::Response &res)
bool getParam(const std::string &key, std::string &s) const
cv::Mat xyz_mat_32F3_
Received point cloud form tof sensor.
IplImage * right_color_image_8U3_
Received color image of the right camera.
sensor_msgs::Image xyz_image_msg_
sensor_msgs::Image confidence_mask_msg_
void connectCallback()
Subscribe to camera topics if not already done.
message_filters::Subscriber< sensor_msgs::CameraInfo > right_camera_info_sub_
Right camera information service.
sensor_msgs::Image color_image_msg_
Number of accumulated tog greyscale images.
message_filters::Subscriber< sensor_msgs::CameraInfo > left_camera_info_sub_
Left camera information service.
sensor_msgs::CvBridge cv_bridge_2_
Converts ROS image messages to openCV IplImages.
image_transport::SubscriberFilter left_color_camera_image_sub_
Left color camera image topic.
boost::mutex m_ServiceMutex
~AllCameraViewer()
Destructor.
cv::Mat left_color_mat_8U3_
Received color image of the left camera.
ros::NodeHandle node_handle_
bool use_left_color_camera_
std::vector< cv::Mat > vec_grey_mat_32F1_
Accumulated tof greyscale images for noise reduction.
image_transport::SubscriberFilter tof_camera_grey_image_sub_
Tof camera intensity image topic.
int image_counter_
Counts the number of images saved to the hard disk.
IplImage * grey_image_32F1_
Received gray values from tof sensor.
image_transport::ImageTransport image_transport_