template_fetcher.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <sensor_msgs/Image.h>
00003 #include <cv_bridge/cv_bridge.h>
00004 #include <opencv2/opencv.hpp>
00005 
00006 #include <iostream>
00007 
00008 using namespace std;
00009 using namespace cv;
00010 
00011 void fetcher(const sensor_msgs::Image::ConstPtr& msg);
00012 
00013 void print_usage() {
00014   std::cout << "\n"
00015             << "Saves a snapshot of image from the input sensor_msgs::Image topic "
00016             << "to the current directory.\n"
00017             << "\n"
00018             << "Usage: rosrun custom_landmark_2d template_fetcher rgb:=/topic1"
00019             << std::endl;
00020 }
00021 
00022 // run this class like this:
00023 // rosrun custom_landmark_2d template_fetcher rgb:=/head_mount_kinect/rgb/image_raw
00024 int main( int argc, char** argv ) {
00025 
00026   if (argc < 2) {
00027     print_usage();
00028     return 1;
00029   }
00030 
00031   ros::init(argc, argv, "template_fetcher");
00032 
00033   ros::NodeHandle n;
00034   ros::Subscriber sub = n.subscribe("rgb", 5, fetcher);
00035 
00036   ros::spin();
00037 
00038   return 0;
00039 }
00040 
00041 void fetcher(const sensor_msgs::Image::ConstPtr& msg) {
00042   if (ros::ok()) {
00043     cv_bridge::CvImagePtr cv_ptr;
00044 
00045     try {
00046       cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); // , sensor_msgs::image_encodings::RGB8
00047     }
00048     catch (cv_bridge::Exception& e) {
00049       ROS_ERROR("cv_bridge exception: %s", e.what());
00050       exit(1);
00051     }
00052 
00053     imwrite("template.jpg", cv_ptr->image);
00054     ROS_INFO("image captured successfully, shutting down...");
00055 
00056     ros::shutdown();
00057   }
00058 }


custom_landmark_2d
Author(s):
autogenerated on Thu Jun 6 2019 18:35:43