Apollo 10.0
自动驾驶开放平台
camera_preprocess_general_util.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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 *****************************************************************************/
17
18#include <math.h>
19
20#include "cyber/common/log.h"
21
22namespace apollo {
23namespace perception {
24namespace camera {
25
26cv::Mat GetImage(const std::string &path, bool to_rgb) {
27 cv::Mat img = cv::imread(path);
28
29 if (!img.data) {
30 throw std::runtime_error("Failed to read image: " + path);
31 }
32
33 if (to_rgb && img.channels() == 3) {
34 cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
35 }
36 return img;
37}
38
39// Resize use opencv lib
40void Resize(const cv::Mat &input_img, cv::Mat *out_img, int width, int height,
41 double fx, double fy, int interpolation) {
42 ACHECK(nullptr != out_img);
43 cv::resize(input_img, *out_img, cv::Size(width, height), fx, fy,
44 interpolation);
45}
46
47void Normalize(const std::vector<float> &mean, const std::vector<float> &std,
48 float scale, cv::Mat *img) {
49 float eps = 1e-6;
50 ACHECK(nullptr != img);
51 ACHECK(3 == std.size());
52 for (const auto std_value : std) {
53 ACHECK(fabs(std_value) > eps);
54 }
55 ACHECK(fabs(scale) > eps);
56 (*img).convertTo(*img, CV_32FC3, scale);
57 for (int h = 0; h < img->rows; h++) {
58 for (int w = 0; w < img->cols; w++) {
59 img->at<cv::Vec3f>(h, w)[0] =
60 (img->at<cv::Vec3f>(h, w)[0] - mean[0]) / std[0];
61 img->at<cv::Vec3f>(h, w)[1] =
62 (img->at<cv::Vec3f>(h, w)[1] - mean[1]) / std[1];
63 img->at<cv::Vec3f>(h, w)[2] =
64 (img->at<cv::Vec3f>(h, w)[2] - mean[2]) / std[2];
65 }
66 }
67}
68
69std::vector<float> HWC2CHW(const cv::Mat &input_img) {
70 int channel = input_img.channels();
71 int width = input_img.cols;
72 int height = input_img.rows;
73
74 std::vector<cv::Mat> input_channels(channel);
75 cv::split(input_img, input_channels);
76
77 std::vector<float> result(channel * width * height);
78 auto data = result.data();
79 int channel_length = width * height;
80
81 for (int i = 0; i < channel; ++i) {
82 memcpy(data, input_channels[i].data, channel_length * sizeof(float));
83 data += channel_length;
84 }
85 return result;
86}
87} // namespace camera
88} // namespace perception
89} // namespace apollo
#define ACHECK(cond)
Definition log.h:80
void Resize(cv::Mat *img, cv::Mat *img_n, int width, int height)
Image resize function
Definition preprocess.cc:24
void Normalize(const std::vector< float > &mean, const std::vector< float > &std, float scale, cv::Mat *img)
Image normalize function
Definition preprocess.cc:33
cv::Mat GetImage(const std::string &path, bool to_rgb)
std::vector< float > HWC2CHW(const cv::Mat &input_img)
class register implement
Definition arena_queue.h:37
Definition future.h:29