processor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 #include "image_proc/processor.h"
36 #include <ros/console.h>
37 
38 namespace image_proc {
39 
41 
42 bool Processor::process(const sensor_msgs::ImageConstPtr& raw_image,
44  ImageSet& output, int flags) const
45 {
46  static const int MONO_EITHER = MONO | RECT;
47  static const int COLOR_EITHER = COLOR | RECT_COLOR;
48  if (!(flags & ALL)) return true;
49 
50  // Check if raw_image is color
51  const std::string& raw_encoding = raw_image->encoding;
52  int raw_type = CV_8UC1;
53  if (raw_encoding == enc::BGR8 || raw_encoding == enc::RGB8) {
54  raw_type = CV_8UC3;
55  output.color_encoding = raw_encoding;
56  } else if (raw_encoding == enc::TYPE_32FC1) {
57  // Float depth
58  raw_type = CV_32FC1;
59  } else if (raw_encoding == enc::MONO16) {
60  // Uint16 depth
61  raw_type = CV_16UC1;
62  }
63  // Construct cv::Mat pointing to raw_image data
64  const cv::Mat raw(raw_image->height, raw_image->width, raw_type,
65  const_cast<uint8_t*>(&raw_image->data[0]), raw_image->step);
66 
68  // Construct colorized (unrectified) images from raw //
70 
71  // Bayer case
72  if (raw_encoding.find("bayer") != std::string::npos) {
73  // Convert to color BGR
75  int code = 0;
76  if (raw_encoding == enc::BAYER_RGGB8)
77  code = cv::COLOR_BayerBG2BGR;
78  else if (raw_encoding == enc::BAYER_BGGR8)
79  code = cv::COLOR_BayerRG2BGR;
80  else if (raw_encoding == enc::BAYER_GBRG8)
81  code = cv::COLOR_BayerGR2BGR;
82  else if (raw_encoding == enc::BAYER_GRBG8)
83  code = cv::COLOR_BayerGB2BGR;
84  else {
85  ROS_ERROR("[image_proc] Unsupported encoding '%s'", raw_encoding.c_str());
86  return false;
87  }
88  cv::cvtColor(raw, output.color, code);
89  output.color_encoding = enc::BGR8;
90 
91  if (flags & MONO_EITHER)
92  cv::cvtColor(output.color, output.mono, cv::COLOR_BGR2GRAY);
93  }
94  // Color case
95  else if (raw_type == CV_8UC3) {
96  output.color = raw;
97  if (flags & MONO_EITHER) {
98  int code = (raw_encoding == enc::BGR8) ? cv::COLOR_BGR2GRAY : cv::COLOR_RGB2GRAY;
99  cv::cvtColor(output.color, output.mono, code);
100  }
101  }
102  // Mono case
103  else if (raw_encoding == enc::MONO8) {
104  output.mono = raw;
105  if (flags & COLOR_EITHER) {
106  output.color_encoding = enc::MONO8;
107  output.color = raw;
108  }
109  }
110  // Depth
111  else if (raw_encoding == enc::TYPE_32FC1 || raw_encoding == enc::MONO16) {
112  output.mono = raw;
113  if (flags & COLOR_EITHER) {
114  output.color_encoding = raw_encoding;
115  output.color = raw;
116  }
117  }
118  // 8UC3 does not specify a color encoding. Is it BGR, RGB, HSV, XYZ, LUV...?
119  else if (raw_encoding == enc::TYPE_8UC3) {
120  ROS_ERROR("[image_proc] Ambiguous encoding '8UC3'. The camera driver "
121  "should set the encoding to 'bgr8' or 'rgb8'.");
122  return false;
123  }
124  // Something else we can't handle
125  else {
126  ROS_ERROR("[image_proc] Unsupported encoding '%s'", raw_encoding.c_str());
127  return false;
128  }
129 
131  // Construct rectified images from colorized images //
133 
136  if (flags & RECT)
137  model.rectifyImage(output.mono, output.rect, interpolation_);
138  if (flags & RECT_COLOR)
139  model.rectifyImage(output.color, output.rect_color, interpolation_);
140 
141  return true;
142 }
143 
144 } //namespace image_proc
std::string color_encoding
Definition: processor.h:45
void rectifyImage(const cv::Mat &raw, cv::Mat &rectified, int interpolation=cv::INTER_LINEAR) const
bool process(const sensor_msgs::ImageConstPtr &raw_image, const image_geometry::PinholeCameraModel &model, ImageSet &output, int flags=ALL) const
Definition: processor.cpp:42
#define ROS_ERROR(...)


image_proc
Author(s): Patrick Mihelich, Kurt Konolige, Jeremy Leibs
autogenerated on Wed Dec 7 2022 03:25:23