my_subscriber.cpp
Go to the documentation of this file.
1 //*****************************************************************************
2 
3 // STEMMER IMAGING AG
4 
5 //*****************************************************************************
6 //
7 
8 // Copyright @ 2019 STEMMER IMAGING AG
9 
10 //
11 
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 
14 // of this file and associated documentation files (the ‘Software’), to
15 
16 // deal in the Software without restriction, including without limitation the
17 
18 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 
20 // sell copies of the Software, and to permit persons to whom the Software is
21 
22 // furnished to do so, subject to the following conditions:
23 
24 //
25 
26 // The above copyright notice and this permission notice shall be included in
27 
28 // all copies or substantial portions of the Software.
29 
30 //
31 
32 // THE SOFTWARE IS PROVIDED ‚AS IS‘, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 
34 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 
36 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
37 
38 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 
40 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 
42 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43 
44 // SOFTWARE.
45 
46 
47 #include <iostream>
48 #include <cvb/cvb/device_factory.hpp>
49 #include <cvb/cvb/utilities/system_info.hpp>
50 #include <cvb/cvb/driver/stream.hpp>
51 
52 #include <ros/ros.h>
54 #include <opencv2/highgui/highgui.hpp>
55 #include <cv_bridge/cv_bridge.h>
57 #include <sensor_msgs/CameraInfo.h>
58 
59 sensor_msgs::CameraInfo _camera_info;
60 //image_transport::CameraPublisher* _camera_info_pub = NULL;
62 
63 void callback(const sensor_msgs::ImageConstPtr& image)
64 {
65  std::cout << "received an image" << std::endl;
66 };
67 
68 
69 sensor_msgs::Image toImageMsg(const Cvb::ImagePtr& cvbImg)
70 {
71  // sensor msg
72  sensor_msgs::Image rosImg;
73  rosImg.header.frame_id = "camera";
74  rosImg.height = cvbImg->Height();
75  rosImg.width = cvbImg->Width();
76  rosImg.encoding = sensor_msgs::image_encodings::RGB8; // workaround, needs proper encoding values.
77  rosImg.is_bigendian = false; // always depends on the pfnc dataformat
78 
79  auto dataAccess = cvbImg->Plane(0).LinearAccess();
80 
81  rosImg.step = dataAccess.YInc();
82 
83 // size_t size = dataAccess.YInc();
84  size_t size = rosImg.width * 3 * rosImg.height;
85  unsigned char * start = reinterpret_cast<unsigned char*>(dataAccess.BasePtr());
86  rosImg.data = std::vector<unsigned char>(start, start + size);
87  return rosImg;
88 
89 };
90 
91 
92 
93 int main(int argc, char **argv)
94 {
95  ros::init(argc, argv, "something");
96  ros::NodeHandle nh_("~");
99  std::cout << "changes here " << std::endl;
100  image_transport::Publisher pub = it.advertise("/jai_camera/image_raw", 1);
101  ros::Publisher camera_info_pub = n.advertise<sensor_msgs::CameraInfo>("/jai_camera/camera_info", 1);
102 
103  image_transport::Subscriber sub = it.subscribe("/jai_camera/image_raw", 1, callback);
104 
105  std::string camera_name;
106  std::string camera_info_url;
107  nh_.getParam("camera_name", camera_name);
108  nh_.getParam("camera_info_url", camera_info_url);
109 
110  // initialize CameraInfoManager, providing set_camera_info service for geometric calibration
111  // see http://wiki.ros.org/camera_info_manager
113  _camera_info_manager = &cinfo_manager;
114 
115  if (!_camera_info_manager->setCameraName(camera_name))
116  {
117  // GUID is 16 hex digits, which should be valid.
118  // If not, use it for log messages anyway.
119  ROS_WARN_STREAM("[" << camera_name << "] name not valid" << " for camera_info_manger");
120  }
121 
122  if (_camera_info_manager->validateURL(camera_info_url))
123  {
124  if ( !_camera_info_manager->loadCameraInfo(camera_info_url) )
125  {
126  ROS_WARN( "camera_info_url does not contain calibration data." );
127  }
128  else if ( !_camera_info_manager->isCalibrated() )
129  {
130  ROS_WARN( "Camera is not calibrated. Using default values." );
131  }
132  }
133  else
134  {
135  ROS_ERROR_STREAM_ONCE( "Calibration URL syntax is not supported by CameraInfoManager." );
136  }
137 
138 
139 
140  try
141  {
142 std::cout << " Try " << std::endl;
143  auto stream = Cvb::DeviceFactory::Open(Cvb::ExpandPath(CVB_LIT("%CVB%/drivers/GenICam.vin")))->Stream();
144  stream->Start();
145 
146  sensor_msgs::Image image;
147  sensor_msgs::Image imageRos;
148  cv_bridge::CvImagePtr frame;
149  cv::Mat Input;
150  cv::Mat cropped;
151  cv::Mat Output;
152  while(ros::ok())
153  {
154  // wait for an image with a timeout of 10 seconds
155  auto waitResult = stream->WaitFor(std::chrono::milliseconds(10000));
156  if (waitResult.Status == Cvb::WaitStatus::Ok)
157  {
158  image = toImageMsg(waitResult.Image);
159  frame = cv_bridge::toCvCopy(image, sensor_msgs::image_encodings::RGB8);
160  Input = frame->image;
161  // remove black stripes from fish eye camera image
162  cv::Rect cropArea(195, 0 , 1546, 1216);
163  cropped = Input(cropArea);
164  cv::resize(cropped, Output, cv::Size(), 1, 1);
165  cv_bridge::CvImage resizeRos;
166  resizeRos.encoding = "rgb8";
167  resizeRos.image = Output;
168  sensor_msgs::ImagePtr imagePtr = resizeRos.toImageMsg();
169 // imagePtr->header.stamp = ros::Time::now(); // NICHT verwenden! Sonst passen timestamps bei camera_info nicht dazu
170 //
171  resizeRos.toImageMsg(imageRos);
172  _camera_info = _camera_info_manager->getCameraInfo();
173  _camera_info.header = imageRos.header;
174 // _camera_info.header.stamp = ros::Time::now(); // NICHT verwenden! Sonst passen timestamps bei camera_info nicht dazu
175  _camera_info.height = imageRos.height;
176  _camera_info.width = imageRos.width;
177  camera_info_pub.publish(_camera_info);
178  pub.publish(imagePtr);
179  // pub.publish(toImageMsg(waitResult.Image));
180  ros::spinOnce();
181  }
182  else
183  {
184  throw std::runtime_error(std::string("cvb acq error ") + std::to_string(static_cast<int>(waitResult.Status)));
185  }
186  }
187  // synchronously stop the stream
188  stream->Stop();
189  }
190  catch (const std::exception& error)
191  {
192  std::cout << error.what() << std::endl;
193  }
194 }
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())
Output
sensor_msgs::CameraInfo getCameraInfo(void)
void publish(const boost::shared_ptr< M > &message) const
bool loadCameraInfo(const std::string &url)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
Input
#define ROS_WARN(...)
camera_info_manager::CameraInfoManager * _camera_info_manager
void callback(const sensor_msgs::ImageConstPtr &image)
bool validateURL(const std::string &url)
void publish(const sensor_msgs::Image &message) const
int main(int argc, char **argv)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_WARN_STREAM(args)
sensor_msgs::CameraInfo _camera_info
bool getParam(const std::string &key, std::string &s) const
#define ROS_ERROR_STREAM_ONCE(args)
ROSCPP_DECL void spinOnce()
bool setCameraName(const std::string &cname)
sensor_msgs::Image toImageMsg(const Cvb::ImagePtr &cvbImg)


ros_cvb_camera_driver
Author(s): Christopher Hartmann , Johanna Gleichauf
autogenerated on Tue Feb 11 2020 03:47:46