rgbd
image.cpp
Go to the documentation of this file.
1 #include "rgbd/image.h"
2 
3 #include <opencv2/core/check.hpp>
4 
5 #include <sensor_msgs/CameraInfo.h>
6 
7 
8 namespace rgbd
9 {
10 
11 // ----------------------------------------------------------------------------------------------------
12 
13 Image::Image() : timestamp_(0)
14 {
15 }
16 
17 // ----------------------------------------------------------------------------------------------------
18 
19 Image::Image(const cv::Mat& rgb_image,
20  const cv::Mat& depth_image,
21  const image_geometry::PinholeCameraModel& cam_model,
22  const std::string& frame_id,
23  double timestamp) :
24  rgb_image_(rgb_image),
25  depth_image_(depth_image),
26  frame_id_(frame_id),
27  timestamp_(timestamp)
28 {
29  setCameraModel(cam_model);
30 }
31 
32 // ----------------------------------------------------------------------------------------------------
33 
34 void Image::setCameraInfo(sensor_msgs::CameraInfo cam_info)
35 {
36  cam_info.header.frame_id.clear();
37  cam_info.header.seq = 0;
38  cam_info.header.stamp.fromSec(0);
39  cam_model_.fromCameraInfo(cam_info);
40 }
41 
42 
43 // ----------------------------------------------------------------------------------------------------
44 
45 void Image::setCameraModel(const image_geometry::PinholeCameraModel& cam_model)
46 {
47  sensor_msgs::CameraInfo cam_info = cam_model.cameraInfo();
48  cam_info.header.frame_id.clear();
49  cam_info.header.seq = 0;
50  cam_info.header.stamp.fromSec(0);
51  cam_model_.fromCameraInfo(cam_info);
52 }
53 
54 // ----------------------------------------------------------------------------------------------------
55 
57 {
58  rgbd::Image image;
59  image.rgb_image_ = rgb_image_.clone();
60  image.depth_image_ = depth_image_.clone();
61  image.frame_id_ = frame_id_;
62  image.timestamp_ = timestamp_;
63  if (cam_model_.initialized())
65 
66  return image;
67 }
68 
69 // ----------------------------------------------------------------------------------------------------
70 
71 bool Image::operator==(const rgbd::Image& other) const
72 {
73  if (getTimestamp() > 0 && std::abs<double>(getTimestamp()-other.getTimestamp()) > 1e-9)
74  return false;
75  if (!getFrameId().empty() && getFrameId() != other.getFrameId())
76  return false;
77  if (getCameraModel().cameraInfo() != other.getCameraModel().cameraInfo())
78  return false;
79 
80  const cv::Mat& this_depth = getDepthImage();
81  const cv::Mat& other_depth = other.getDepthImage();
82  if (this_depth.data != other_depth.data)
83  {
84  cv::Mat dst;
85  cv::bitwise_xor(this_depth, other_depth, dst);
86  if (cv::countNonZero(dst) != 0)
87  return false;
88  }
89 
90  const cv::Mat& this_rgb = getRGBImage();
91  const cv::Mat& other_rgb = other.getRGBImage();
92  if (this_rgb.data != other_rgb.data)
93  {
94  cv::Mat dst, dst2;
95  cv::bitwise_xor(this_rgb, other_rgb, dst);
96  cv::transform(dst, dst2, cv::Matx<int, 1, 3>(1,1,1));
97  if (cv::countNonZero(dst2) != 0)
98  return false;
99  }
100 
101  return true;
102 }
103 
104 // ----------------------------------------------------------------------------------------------------
105 
107 {
108  std::streamsize ss = out.precision();
109  out << "Depth: " << image.depth_image_.size << "@(" << cv::typeToString(image.depth_image_.type()) << ")" << std::endl
110  << "color: " << image.rgb_image_.size << "@(" << cv::typeToString(image.rgb_image_.type()) << ")" << std::endl
111  << "frame_id: " << image.frame_id_ << std::endl
112  << "timestamp: " << std::setprecision(32) << image.timestamp_ << std::setprecision(ss) << std::endl
113  << "camera model: " << std::endl << image.cam_model_.cameraInfo();
114  return out;
115 }
116 
117 // ----------------------------------------------------------------------------------------------------
118 
119 } // end namespace rgbd
std::setprecision
T setprecision(T... args)
std::string
rgbd::Image::getDepthImage
const cv::Mat & getDepthImage() const
Get the depth image.
Definition: image.h:72
rgbd::Image::cam_model_
image_geometry::PinholeCameraModel cam_model_
Camera model Header in camera info should always be empty to prevent inconsistent behaviour of equali...
Definition: image.h:179
rgbd::Image::getTimestamp
double getTimestamp() const
Get the timestamp.
Definition: image.h:90
rgb_image
cv::Mat rgb_image
Definition: record_to_video.cpp:24
rgbd::Image::operator==
bool operator==(const rgbd::Image &other) const
Equality operator.
Definition: image.cpp:71
rgbd::Image::frame_id_
std::string frame_id_
Definition: image.h:181
rgbd::Image
Definition: image.h:43
std::streamsize
rgbd::operator<<
std::ostream & operator<<(std::ostream &out, const rgbd::Image &image)
Definition: image.cpp:106
std::ostream
rgbd::Image::setCameraModel
void setCameraModel(const image_geometry::PinholeCameraModel &cam_model)
Set the camera model. The frame_id and timestamp in the camera info inside the camera model will be e...
Definition: image.cpp:45
rgbd
Definition: client.h:24
image.h
rgbd::Image::getRGBImage
const cv::Mat & getRGBImage() const
Get the RGB color image.
Definition: image.h:78
rgbd::Image::Image
Image()
Empty constructor.
Definition: image.cpp:13
rgbd::Image::clone
Image clone() const
Create a clone of this image. The depth and RGB images will be cloned, so the new image will not shar...
Definition: image.cpp:56
rgbd::Image::depth_image_
cv::Mat depth_image_
Definition: image.h:173
std::endl
T endl(T... args)
rgbd::Image::timestamp_
double timestamp_
Definition: image.h:182
rgbd::Image::getCameraModel
const image_geometry::PinholeCameraModel & getCameraModel() const
Get the camera model.
Definition: image.h:96
rgbd::Image::getFrameId
const std::string & getFrameId() const
Get the frame_id.
Definition: image.h:84
rgbd::Image::setCameraInfo
void setCameraInfo(sensor_msgs::CameraInfo cam_info)
Set the camera model by using a camera info message. The frame_id and timestamp of the message will b...
Definition: image.cpp:34
std::ostream::precision
T precision(T... args)
rgbd::Image::rgb_image_
cv::Mat rgb_image_
Definition: image.h:172