capture.cpp
Go to the documentation of this file.
1 // Copyright [2015] Takashi Ogura<t.ogura@gmail.com>
2 
3 #include "cv_camera/capture.h"
4 #include <sstream>
5 #include <string>
6 
7 namespace cv_camera
8 {
9 
11 
12 Capture::Capture(ros::NodeHandle &node, const std::string &topic_name,
13  int32_t buffer_size, const std::string &frame_id)
14  : node_(node),
15  it_(node_),
16  topic_name_(topic_name),
17  buffer_size_(buffer_size),
18  frame_id_(frame_id),
19  info_manager_(node_, frame_id),
20  capture_delay_(ros::Duration(node_.param("capture_delay", 0.0)))
21 {
22 }
23 
25 {
26  std::string url;
27  if (node_.getParam("camera_info_url", url))
28  {
29  if (info_manager_.validateURL(url))
30  {
32  }
33  }
34 
35  rescale_camera_info_ = node_.param<bool>("rescale_camera_info", false);
36 
37  for (int i = 0;; ++i)
38  {
39  int code = 0;
40  double value = 0.0;
41  std::stringstream stream;
42  stream << "property_" << i << "_code";
43  const std::string param_for_code = stream.str();
44  stream.str("");
45  stream << "property_" << i << "_value";
46  const std::string param_for_value = stream.str();
47  if (!node_.getParam(param_for_code, code) || !node_.getParam(param_for_value, value))
48  {
49  break;
50  }
51  if (!cap_.set(code, value))
52  {
53  ROS_ERROR_STREAM("Setting with code " << code << " and value " << value << " failed"
54  << std::endl);
55  }
56  }
57 }
58 
59 void Capture::rescaleCameraInfo(int width, int height)
60 {
61  double width_coeff = static_cast<double>(width) / info_.width;
62  double height_coeff = static_cast<double>(height) / info_.height;
63  info_.width = width;
64  info_.height = height;
65 
66  // See http://docs.ros.org/api/sensor_msgs/html/msg/CameraInfo.html for clarification
67  info_.K[0] *= width_coeff;
68  info_.K[2] *= width_coeff;
69  info_.K[4] *= height_coeff;
70  info_.K[5] *= height_coeff;
71 
72  info_.P[0] *= width_coeff;
73  info_.P[2] *= width_coeff;
74  info_.P[5] *= height_coeff;
75  info_.P[6] *= height_coeff;
76 }
77 
78 void Capture::open(int32_t device_id)
79 {
80  cap_.open(device_id);
81  if (!cap_.isOpened())
82  {
83  std::stringstream stream;
84  stream << "device_id" << device_id << " cannot be opened";
85  throw DeviceError(stream.str());
86  }
88 
90 }
91 
92 void Capture::open(const std::string &device_path)
93 {
94  cap_.open(device_path, cv::CAP_V4L);
95  if (!cap_.isOpened())
96  {
97  throw DeviceError("device_path " + device_path + " cannot be opened");
98  }
100 
101  loadCameraInfo();
102 }
103 
105 {
106  open(0);
107 }
108 
109 void Capture::openFile(const std::string &file_path)
110 {
111  cap_.open(file_path);
112  if (!cap_.isOpened())
113  {
114  std::stringstream stream;
115  stream << "file " << file_path << " cannot be opened";
116  throw DeviceError(stream.str());
117  }
119 
120  std::string url;
121  if (node_.getParam("camera_info_url", url))
122  {
123  if (info_manager_.validateURL(url))
124  {
126  }
127  }
128 }
129 
131 {
132  if (cap_.read(bridge_.image))
133  {
135  bridge_.encoding = enc::BGR8;
136  bridge_.header.stamp = stamp;
137  bridge_.header.frame_id = frame_id_;
138 
140  if (info_.height == 0 && info_.width == 0)
141  {
142  info_.height = bridge_.image.rows;
143  info_.width = bridge_.image.cols;
144  }
145  else if (info_.height != bridge_.image.rows || info_.width != bridge_.image.cols)
146  {
148  {
149  int old_width = info_.width;
150  int old_height = info_.height;
152  ROS_INFO_ONCE("Camera calibration automatically rescaled from %dx%d to %dx%d",
153  old_width, old_height, bridge_.image.cols, bridge_.image.rows);
154  }
155  else
156  {
157  ROS_WARN_ONCE("Calibration resolution %dx%d does not match camera resolution %dx%d. "
158  "Use rescale_camera_info param for rescaling",
159  info_.width, info_.height, bridge_.image.cols, bridge_.image.rows);
160  }
161  }
162  info_.header.stamp = stamp;
163  info_.header.frame_id = frame_id_;
164 
165  return true;
166  }
167  return false;
168 }
169 
171 {
173 }
174 
175 bool Capture::setPropertyFromParam(int property_id, const std::string &param_name)
176 {
177  if (cap_.isOpened())
178  {
179  double value = 0.0;
180  if (node_.getParam(param_name, value))
181  {
182  ROS_INFO("setting property %s = %lf", param_name.c_str(), value);
183  return cap_.set(property_id, value);
184  }
185  }
186  return true;
187 }
188 
189 } // namespace cv_camera
image_transport::ImageTransport it_
ROS image transport utility.
Definition: capture.h:169
bool param(const std::string &param_name, T &param_val, const T &default_val)
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
bool capture()
capture an image and store.
Definition: capture.cpp:130
sensor_msgs::CameraInfo getCameraInfo(void)
#define ROS_INFO_ONCE(...)
Capture(ros::NodeHandle &node, const std::string &topic_name, int32_t buffer_size, const std::string &frame_id)
costruct with ros node and topic settings
Definition: capture.cpp:12
bool loadCameraInfo(const std::string &url)
void open()
Open default camera device.
Definition: capture.cpp:104
ros::NodeHandle node_
node handle for advertise.
Definition: capture.h:164
ROS cv camera device exception.
Definition: exception.h:16
void publish()
Publish the image that is already captured by capture().
Definition: capture.cpp:170
std::string encoding
image_transport::CameraPublisher pub_
image publisher created by image_transport::ImageTransport.
Definition: capture.h:188
void loadCameraInfo()
Load camera info from file.
Definition: capture.cpp:24
bool validateURL(const std::string &url)
int32_t buffer_size_
size of publisher buffer
Definition: capture.h:183
CameraPublisher advertiseCamera(const std::string &base_topic, uint32_t queue_size, bool latch=false)
#define ROS_WARN_ONCE(...)
#define ROS_INFO(...)
camera_info_manager::CameraInfoManager info_manager_
camera info manager
Definition: capture.h:210
bool param(const std::string &param_name, T &param_val, const T &default_val) const
namespace of this package
Definition: capture.h:20
cv_bridge::CvImage bridge_
this stores last captured image.
Definition: capture.h:198
void openFile(const std::string &file_path)
open video file instead of capture device.
Definition: capture.cpp:109
std::string topic_name_
name of topic without namespace (usually "image_raw").
Definition: capture.h:174
cv::VideoCapture cap_
capture device.
Definition: capture.h:193
bool setPropertyFromParam(int property_id, const std::string &param_name)
set CV_PROP_*
Definition: capture.cpp:175
bool getParam(const std::string &key, std::string &s) const
static Time now()
ros::Duration capture_delay_
capture_delay param value
Definition: capture.h:220
std::string frame_id_
header.frame_id for publishing images.
Definition: capture.h:179
void rescaleCameraInfo(int width, int height)
rescale camera calibration to another resolution
Definition: capture.cpp:59
#define ROS_ERROR_STREAM(args)
const sensor_msgs::ImagePtr getImageMsgPtr() const
accessor of ROS Image message.
Definition: capture.h:126
sensor_msgs::CameraInfo info_
this stores last captured image info.
Definition: capture.h:205
std_msgs::Header header
bool rescale_camera_info_
rescale_camera_info param value
Definition: capture.h:215


cv_camera
Author(s): Takashi Ogura
autogenerated on Fri May 10 2019 02:14:17