4 #include <ros/console.h>
9 #include <opencv2/core.hpp>
10 #include <opencv2/imgcodecs.hpp>
35 const image_geometry::PinholeCameraModel& cam_model = image.
cam_model_;
37 if (cam_model.initialized())
40 a << cam_model.fx() << cam_model.fy();
41 a << cam_model.cx() << cam_model.cy();
42 a << cam_model.Tx() << cam_model.Ty();
43 a << cam_model.fullResolution().width << cam_model.fullResolution().height;
47 ROS_ERROR_NAMED(
"serialization",
"rgbd::serialize: cam_model not initialized");
75 rgb_params[0] = cv::IMWRITE_JPEG_QUALITY;
81 if (!cv::imencode(
".jpg", image.
rgb_image_, rgb_data, rgb_params)) {
82 ROS_ERROR_NAMED(
"serialization",
"RGB image compression failed");
86 a << (int)rgb_data.
size();
87 a.write((
const char*)&rgb_data[0], rgb_data.
size());
91 ROS_ERROR_STREAM_NAMED(
"serialization",
"Unsupported RGB STORAGE TYPE: " << rgb_type);
118 float depthQuantA = depthZ0 * (depthZ0 + 1.0f);
119 float depthQuantB = 1.0f - depthQuantA / depthMax;
121 a << depthQuantA << depthQuantB;
124 cv::Mat invDepthImg(depth_image.size(), CV_16UC1);
127 cv::MatConstIterator_<float> itDepthImg = depth_image.begin<
float>(),
128 itDepthImg_end = depth_image.end<
float>();
129 cv::MatIterator_<unsigned short> itInvDepthImg = invDepthImg.begin<
unsigned short>(),
130 itInvDepthImg_end = invDepthImg.end<
unsigned short>();
133 for (; (itDepthImg != itDepthImg_end) && (itInvDepthImg != itInvDepthImg_end); ++itDepthImg, ++itInvDepthImg) {
135 if (*itDepthImg < depthMax){
136 *itInvDepthImg = depthQuantA / *itDepthImg + depthQuantB;
146 params[0] = cv::IMWRITE_PNG_COMPRESSION;
151 if (!cv::imencode(
".png", invDepthImg, depth_data, params)) {
152 ROS_ERROR_NAMED(
"serialization",
"Depth image compression failed");
156 a << static_cast<int>(depth_data.
size());
157 a.write((
const char*)&depth_data[0], depth_data.
size());
161 ROS_ERROR_NAMED(
"serialization",
"Unsupported DEPTH_STORAGE_TYPE");
194 double fx, fy, cx, cy, tx, ty;
200 a >> width >> height;
202 sensor_msgs::CameraInfo cam_info_msg;
204 cam_info_msg.D.resize(5, 0.0);
205 cam_info_msg.K.fill(0.0);
206 cam_info_msg.K[0] = fx;
207 cam_info_msg.K[2] = cx;
208 cam_info_msg.K[4] = fy;
209 cam_info_msg.K[5] = cy;
210 cam_info_msg.K[8] = 1.0;
212 cam_info_msg.R.fill(0.0);
213 cam_info_msg.R[0] = 1.0;
214 cam_info_msg.R[4] = 1.0;
215 cam_info_msg.R[8] = 1.0;
217 cam_info_msg.P.fill(0.0);
218 cam_info_msg.P[0] = fx;
219 cam_info_msg.P[2] = cx;
220 cam_info_msg.P[3] = tx;
221 cam_info_msg.P[5] = fy;
222 cam_info_msg.P[6] = cy;
223 cam_info_msg.P[7] = ty;
224 cam_info_msg.P[10] = 1.0;
226 cam_info_msg.distortion_model =
"plumb_bob";
228 cam_info_msg.width = width;
229 cam_info_msg.height = height;
231 image.
cam_model_.fromCameraInfo(cam_info_msg);
235 ROS_ERROR_STREAM_NAMED(
"serialization",
"rgbd::deserialize: Unsupported camera model: " << cam_type);
253 int size = width * height * 3;
254 image.
rgb_image_ = cv::Mat(height, width, CV_8UC3);
255 for(
int i = 0; i < size; ++i)
264 for(
int i = 0; i < rgb_size; ++i)
267 image.
rgb_image_ = cv::imdecode(rgb_data, cv::IMREAD_UNCHANGED);
271 ROS_ERROR_STREAM_NAMED(
"serialization",
"rgbd::deserialize: Unsupported rgb storage format: " << rgb_type);
289 int size = width * height * 4;
291 for(
int i = 0; i < size; ++i)
296 float depthQuantA, depthQuantB;
297 a >> depthQuantA >> depthQuantB;
303 for(
int i = 0; i < depth_size; ++i)
306 cv::Mat decompressed = cv::imdecode(depth_data, cv::IMREAD_UNCHANGED);
308 depth_image = cv::Mat(decompressed.size(), CV_32FC1);
311 cv::MatIterator_<float> itDepthImg = depth_image.begin<
float>(),
312 itDepthImg_end = depth_image.end<
float>();
313 cv::MatConstIterator_<unsigned short> itInvDepthImg = decompressed.begin<
unsigned short>(),
314 itInvDepthImg_end = decompressed.end<
unsigned short>();
316 for (; (itDepthImg != itDepthImg_end) && (itInvDepthImg != itInvDepthImg_end); ++itDepthImg, ++itInvDepthImg) {
318 if (*itInvDepthImg) {
319 *itDepthImg = depthQuantA / ((float)*itInvDepthImg - depthQuantB);
327 ROS_ERROR_STREAM_NAMED(
"serialization",
"rgbd::deserialize: Unsupported depth storage format: " << depth_type);