disparity_color_publisher.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Roboception GmbH
3  * All rights reserved
4  *
5  * Author: Heiko Hirschmueller
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its contributors
18  * may be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
35 
37 #include <rc_genicam_api/config.h>
38 
40 
41 namespace rc
42 {
44  std::function<void()>& sub_changed)
45  : GenICam2RosPublisher(frame_id)
46 {
47  sub_callback = sub_changed;
48  pub = it.advertise("disparity_color", 1, boost::bind(&GenICam2RosPublisher::subChangedIt, this, _1),
49  boost::bind(&GenICam2RosPublisher::subChangedIt, this, _1));
50 }
51 
53 {
54  return pub.getNumSubscribers() > 0;
55 }
56 
57 void DisparityColorPublisher::requiresComponents(int& components, bool& color)
58 {
59  if (pub.getNumSubscribers() > 0)
60  {
61  components |= ComponentDisparity;
62  }
63 }
64 
65 void DisparityColorPublisher::publish(const rcg::Buffer* buffer, uint32_t part, uint64_t pixelformat)
66 {
67  if (nodemap && pub.getNumSubscribers() > 0 && pixelformat == Coord3D_C16)
68  {
69  // create image and initialize header
70 
71  sensor_msgs::ImagePtr im = boost::make_shared<sensor_msgs::Image>();
72 
73  uint64_t time = buffer->getTimestampNS();
74 
75  im->header.seq = 0;
76  im->header.stamp.sec = time / 1000000000ul;
77  im->header.stamp.nsec = time % 1000000000ul;
78  im->header.frame_id = frame_id;
79 
80  // set image size
81 
82  im->width = static_cast<uint32_t>(buffer->getWidth(part));
83  im->height = static_cast<uint32_t>(buffer->getHeight(part));
84  im->is_bigendian = rcg::isHostBigEndian();
85 
86  // get pointer to image data in buffer
87 
88  size_t px = buffer->getXPadding(part);
89  const uint8_t* ps = static_cast<const uint8_t*>(buffer->getBase(part));
90 
91  // get disparity range
92 
93  bool bigendian = buffer->isBigEndian();
94 
95  rcg::setEnum(nodemap, "ChunkComponentSelector", "Disparity", true);
96  double f = rcg::getFloat(nodemap, "ChunkScan3dFocalLength", 0, 0, true);
97  double t = rcg::getFloat(nodemap, "ChunkScan3dBaseline", 0, 0, true);
98  float scale = rcg::getFloat(nodemap, "ChunkScan3dCoordinateScale", 0, 0, true);
99 
100  double mindepth = rcg::getFloat(nodemap, "DepthMinDepth", 0, 0, true);
101  mindepth = std::max(mindepth, 2.5*t);
102 
103  double maxdepth = rcg::getFloat(nodemap, "DepthMaxDepth", 0, 0, true);
104  maxdepth = std::max(mindepth, maxdepth);
105 
106  int dmin=static_cast<int>(std::floor(f*t/maxdepth));
107  int dmax=static_cast<int>(std::ceil(f*t/mindepth));
108  int drange=dmax-dmin+1;
109 
110  // convert image data
111 
112  im->encoding = sensor_msgs::image_encodings::RGB8;
113  im->step = 3 * im->width * sizeof(uint8_t);
114 
115  im->data.resize(im->step * im->height);
116  uint8_t* pt = reinterpret_cast<uint8_t*>(&im->data[0]);
117 
118  for (uint32_t k = 0; k < im->height; k++)
119  {
120  for (uint32_t i = 0; i < im->width; i++)
121  {
122  uint16_t d;
123 
124  if (bigendian)
125  {
126  d = (ps[0] << 8) | ps[1];
127  }
128  else
129  {
130  d = (ps[1] << 8) | ps[0];
131  }
132 
133  ps += 2;
134 
135  if (d != 0)
136  {
137  double v = (scale * d - dmin) / drange;
138  v = v / 1.15 + 0.1;
139 
140  double r = std::max(0.0, std::min(1.0, (1.5 - 4 * fabs(v - 0.75))));
141  double g = std::max(0.0, std::min(1.0, (1.5 - 4 * fabs(v - 0.5))));
142  double b = std::max(0.0, std::min(1.0, (1.5 - 4 * fabs(v - 0.25))));
143 
144  *pt++ = 255 * r + 0.5;
145  *pt++ = 255 * g + 0.5;
146  *pt++ = 255 * b + 0.5;
147  }
148  else
149  {
150  *pt++ = 0;
151  *pt++ = 0;
152  *pt++ = 0;
153  }
154  }
155 
156  ps += px;
157  }
158 
159  // publish message
160 
161  pub.publish(im);
162  }
163 }
164 
165 } // namespace rc
Interface for all publishers relating to images, point clouds or other stereo-camera data...
d
bool used() override
Returns true if there are subscribers to the topic.
f
static const int ComponentDisparity
std::function< void()> sub_callback
Coord3D_C16
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
size_t getXPadding(std::uint32_t part) const
uint32_t getNumSubscribers() const
size_t getWidth(std::uint32_t part) const
bool setEnum(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, const char *value, bool exception=false)
void publish(const rcg::Buffer *buffer, uint32_t part, uint64_t pixelformat) override
Offers a buffer for publication.
image_transport::Publisher pub
void * getBase(std::uint32_t part) const
void publish(const sensor_msgs::Image &message) const
DisparityColorPublisher(image_transport::ImageTransport &it, const std::string &frame_id, std::function< void()> &sub_changed)
double getFloat(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, double *vmin=0, double *vmax=0, bool exception=false, bool igncache=false)
uint64_t getTimestampNS() const
bool isBigEndian() const
bool isHostBigEndian()
void subChangedIt(const image_transport::SingleSubscriberPublisher &pub)
Called by publisher if subscription changed.
std::shared_ptr< GenApi::CNodeMapRef > nodemap
void requiresComponents(int &components, bool &color) override
Adds components and if color images are required to the given values.
size_t getHeight(std::uint32_t part) const


rc_genicam_driver
Author(s): Heiko Hirschmueller , Felix Ruess
autogenerated on Sat Feb 13 2021 03:55:07