Apollo 11.0
自动驾驶开放平台
affine_transform.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 *****************************************************************************/
16
18
19#include <cmath>
20#include <npp.h>
21#include <nppdefs.h>
22#include <cuda_runtime.h>
23
24#include "cyber/common/log.h"
27
28namespace apollo {
29namespace prediction {
30
32
34 for (auto it = pointer_table_.begin(); it != pointer_table_.end(); it++) {
35 if (*it) {
36 cudaFreeHost(*it);
37 }
38 }
39}
40
41bool AffineTransform::Init(cv::Size size, int type) {
42 size_ = size;
43 type_ = type;
44
45 auto src_mat = cv::Mat(size_, type_, cv::Scalar(0, 0, 0));
46 cv::Mat out_mat;
47 cv::Point2i center = cv::Point2i(src_mat.cols / 2, src_mat.rows / 2);
48 double angle = -90.0;
49 double scale = 1.0;
50 AffineTransformsFromMat(src_mat, center, angle, scale, &out_mat);
51
52 return true;
53}
54
55
56int AffineTransform::GetCoeffs(const double heading,
57 const cv::Point2i& center_point,
58 const double scale,
59 double coeffs[2][3]) {
60 double alpha, beta, angle;
61
62 angle = heading * M_PI / 180;
63 alpha = std::cos(angle) * scale;
64 beta = std::sin(angle) * scale;
65
66 *((*(coeffs+0))+0) = alpha;
67 *((*(coeffs+0))+1) = beta;
68 *((*(coeffs+0))+2) = (1 - alpha) * center_point.x - beta * center_point.y;
69 *((*(coeffs+1))+0) = -beta;
70 *((*(coeffs+1))+1) = alpha;
71 *((*(coeffs+1))+2) = beta * center_point.x + (1 - alpha) * center_point.y;
72
73 return 0;
74}
75
76int AffineTransform::AffineTransformsFromMat(const cv::Mat& input_img,
77 const cv::Point2i& center_point, const double heading,
78 const double scale, cv::Mat* output_img) {
79 double coeffs[2][3];
80 void* process_buff;
81 void* output_buff;
82
83 cudaMalloc(&process_buff, size_.width * size_.height * 3);
84 cudaMalloc(&output_buff, size_.width * size_.height * 3);
85 // auto output_buff = out_pool_->GetObject();
86 // auto stream = stream_pool_->GetObject();
87
88 // perform affine transform of the whole image
89 NppiRect src_ROI{0, 0, input_img.size().width, input_img.size().height};
90 NppiRect dst_ROI{0, 0, input_img.size().width, input_img.size().height};
91 NppiSize image_size{input_img.size().width, input_img.size().height};
92
93 if (GetCoeffs(heading, center_point, scale, coeffs)) {
94 AERROR << "Fail to get rotation matrix";
95 return -1;
96 }
97
98 cudaMemcpy(process_buff, (unsigned char*) input_img.data,
99 input_img.size().width * input_img.size().height * 3,
100 cudaMemcpyHostToDevice);
101
102 NppStatus status = nppiWarpAffine_8u_C3R(
103 static_cast<Npp8u*>(process_buff),
104 image_size, input_img.step, src_ROI,
105 static_cast<Npp8u*>(output_buff),
106 input_img.step, dst_ROI, coeffs, NPPI_INTER_NN);
107 if (status) {
108 AERROR << "Affine transform failed, error: " << status;
109 return -1;
110 }
111
112 *output_img = cv::Mat(input_img.size(),
113 input_img.type(), cv::Scalar(0, 0, 0));
114
115 cudaMemcpy((unsigned char*) output_img->data, output_buff,
116 input_img.size().width * input_img.size().height * 3,
117 cudaMemcpyDeviceToHost);
118
119 cudaFree(process_buff);
120 cudaFree(output_buff);
121
122 return 0;
123}
124
125} // namespace prediction
126} // namespace apollo
int GetCoeffs(const double heading, const cv::Point2i &center_point, const double scale, double coeffs[2][3])
calculate the coeffs martix
int AffineTransformsFromMat(const cv::Mat &input_img, const cv::Point2i &center_point, const double heading, const double scale, cv::Mat *output_img)
perform the affine transform from cv mat
bool Init(cv::Size size, int type)
init function to the class
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37