Apollo 10.0
自动驾驶开放平台
compress_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <exception>
20#include <iostream>
21#include <fstream>
22
23#include "opencv2/core/core.hpp"
24#include "opencv2/highgui/highgui.hpp"
25#include "opencv2/imgproc/imgproc.hpp"
26
27namespace apollo {
28namespace drivers {
29namespace camera {
30
32 if (!GetProtoConfig(&config_)) {
33 AERROR << "Parse config file failed: " << ConfigFilePath();
34 return false;
35 }
36 AINFO << "Camera config: \n" << config_.DebugString();
37 try {
38 image_pool_.reset(new CCObjectPool<CompressedImage>(
39 config_.compress_conf().image_pool_size()));
40 image_pool_->ConstructAll();
41 } catch (const std::bad_alloc& e) {
42 AERROR << e.what();
43 return false;
44 }
45
46 width_ = config_.compress_conf().width();
47 height_ = config_.compress_conf().height();
48
49 writer_ = node_->CreateWriter<CompressedImage>(
50 config_.compress_conf().output_channel());
51
52 return true;
53}
54
55bool CompressComponent::Proc(const std::shared_ptr<Image>& image) {
56 ADEBUG << "procing compressed";
58 image->height() != height_ || image->width() != width_)) {
59 AERROR << "Setting resolution is " << width_ << "x" << height_ <<
60 ", but received " << image->width() << "x" << image->height();
61 return false;
62 }
63 auto compressed_image = image_pool_->GetObject();
64 compressed_image->mutable_header()->CopyFrom(image->header());
65 compressed_image->set_frame_id(image->frame_id());
66 compressed_image->set_measurement_time(image->measurement_time());
67 compressed_image->set_height(height_);
68 compressed_image->set_width(width_);
69
70 compressed_image->set_format(image->encoding() + "; jpeg compressed bgr8");
71
72 std::vector<int> params;
73 params.resize(3, 0);
74 params[0] = cv::IMWRITE_JPEG_QUALITY;
75 params[1] = 95;
76
77 try {
78 cv::Mat mat_image(image->height(), image->width(), CV_8UC3,
79 const_cast<char*>(image->data().data()), image->step());
80 cv::Mat tmp_mat;
81 cv::cvtColor(mat_image, tmp_mat, cv::COLOR_RGB2BGR);
82 std::vector<uint8_t> compress_buffer;
83 if (!cv::imencode(".jpg", tmp_mat, compress_buffer, params)) {
84 AERROR << "cv::imencode (jpeg) failed on input image";
85 return false;
86 }
87 compressed_image->set_data(compress_buffer.data(), compress_buffer.size());
88 } catch (std::exception& e) {
89 AERROR << "cv::imencode (jpeg) exception :" << e.what();
90 return false;
91 }
92
93 writer_->Write(compressed_image);
94 return true;
95}
96
97} // namespace camera
98} // namespace drivers
99} // namespace apollo
bool GetProtoConfig(T *config) const
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
bool Proc(const std::shared_ptr< Image > &image) override
#define cyber_unlikely(x)
Definition macros.h:30
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
optional CompressConfig compress_conf
Definition config.proto:58