1
+ // Copyright 2024 Tier IV, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include " autoware/tensorrt_yolox/utils.hpp"
16
+
17
+ namespace autoware ::tensorrt_yolox
18
+ {
19
+
20
+ std::vector<std::pair<uint8_t , int >> runLengthEncoder (const cv::Mat & image)
21
+ {
22
+ std::vector<std::pair<uint8_t , int >> compressed_data;
23
+ const int rows = image.rows ;
24
+ const int cols = image.cols ;
25
+ compressed_data.emplace_back (image.at <uint8_t >(0 , 0 ), 0 );
26
+ for (int i = 0 ; i < rows; ++i) {
27
+ for (int j = 0 ; j < cols; ++j) {
28
+ uint8_t current_value = image.at <uint8_t >(i, j);
29
+ if (compressed_data.back ().first == current_value) {
30
+ ++compressed_data.back ().second ;
31
+ } else {
32
+ compressed_data.emplace_back (current_value, 1 );
33
+ }
34
+ }
35
+ }
36
+ return compressed_data;
37
+ }
38
+
39
+
40
+ cv::Mat runLengthDecoder (
41
+ const std::vector<uint8_t > & rle_data, const int rows, const int cols)
42
+ {
43
+ cv::Mat mask (rows, cols, CV_8UC1, cv::Scalar (0 ));
44
+ int idx = 0 ;
45
+ int step = sizeof (uint8_t ) + sizeof (int );
46
+ int nb_pixels = 0 ;
47
+ for (size_t i = 0 ; i < rle_data.size (); i += step) {
48
+ uint8_t value;
49
+ int length;
50
+ std::memcpy (&value, &rle_data[i], sizeof (uint8_t ));
51
+ std::memcpy (&length, &rle_data[i + sizeof (uint8_t )], sizeof (int ));
52
+ nb_pixels += length;
53
+ for (int j = 0 ; j < length; ++j) {
54
+ int row_idx = static_cast <int >(idx / cols);
55
+ int col_idx = static_cast <int >(idx % cols);
56
+ mask.at <uint8_t >(row_idx, col_idx) = value;
57
+ idx++;
58
+ if (idx > rows * cols) {
59
+ break ;
60
+ }
61
+ }
62
+ }
63
+ return mask;
64
+ }
65
+
66
+ } // namespace tensorrt_yolox
0 commit comments