forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tensorrt_yolox): add run length encoding for sematic segmentation…
… mask (autowarefoundation#7905) * fix: add rle compress Signed-off-by: badai-nguyen <[email protected]> * fix: rle compress Signed-off-by: badai-nguyen <[email protected]> * fix: move rle into utils Signed-off-by: badai-nguyen <[email protected]> * chore: pre-commit Signed-off-by: badai-nguyen <[email protected]> * Update perception/autoware_tensorrt_yolox/src/utils.cpp Co-authored-by: Yukihiro Saito <[email protected]> * fix: remove unused variable Signed-off-by: badai-nguyen <[email protected]> * Update perception/autoware_tensorrt_yolox/src/utils.cpp Co-authored-by: Manato Hirabayashi <[email protected]> * style(pre-commit): autofix * feat: add unit test for utils Signed-off-by: badai-nguyen <[email protected]> * style(pre-commit): autofix * fix: unit test Signed-off-by: badai-nguyen <[email protected]> * chore: change to explicit index Signed-off-by: badai-nguyen <[email protected]> * style(pre-commit): autofix * fix: cuda cmake Signed-off-by: badai-nguyen <[email protected]> * fix: separate unit test into different PR Signed-off-by: badai-nguyen <[email protected]> --------- Signed-off-by: badai-nguyen <[email protected]> Co-authored-by: Yukihiro Saito <[email protected]> Co-authored-by: Manato Hirabayashi <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_ | ||
#define AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_ | ||
#include <opencv2/opencv.hpp> | ||
|
||
#include <utility> | ||
#include <vector> | ||
|
||
namespace autoware::tensorrt_yolox | ||
{ | ||
std::vector<std::pair<uint8_t, int>> runLengthEncoder(const cv::Mat & mask); | ||
cv::Mat runLengthDecoder(const std::vector<uint8_t> & rle_data, const int rows, const int cols); | ||
} // namespace autoware::tensorrt_yolox | ||
|
||
#endif // AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 TIER IV, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "autoware/tensorrt_yolox/utils.hpp" | ||
|
||
namespace autoware::tensorrt_yolox | ||
{ | ||
|
||
std::vector<std::pair<uint8_t, int>> runLengthEncoder(const cv::Mat & image) | ||
{ | ||
std::vector<std::pair<uint8_t, int>> compressed_data; | ||
const int rows = image.rows; | ||
const int cols = image.cols; | ||
compressed_data.emplace_back(image.at<uint8_t>(0, 0), 0); | ||
for (int i = 0; i < rows; ++i) { | ||
for (int j = 0; j < cols; ++j) { | ||
uint8_t current_value = image.at<uint8_t>(i, j); | ||
if (compressed_data.back().first == current_value) { | ||
++compressed_data.back().second; | ||
} else { | ||
compressed_data.emplace_back(current_value, 1); | ||
} | ||
} | ||
} | ||
return compressed_data; | ||
} | ||
|
||
cv::Mat runLengthDecoder(const std::vector<uint8_t> & rle_data, const int rows, const int cols) | ||
{ | ||
cv::Mat mask(rows, cols, CV_8UC1, cv::Scalar(0)); | ||
int idx = 0; | ||
int step = sizeof(uint8_t) + sizeof(int); | ||
for (size_t i = 0; i < rle_data.size(); i += step) { | ||
uint8_t value; | ||
int length; | ||
std::memcpy(&value, &rle_data[i], sizeof(uint8_t)); | ||
std::memcpy( | ||
&length, &rle_data[i + 1], | ||
sizeof( | ||
int)); // under the condition that we know rle_data[i] only consume 1 element of the vector | ||
for (int j = 0; j < length; ++j) { | ||
int row_idx = static_cast<int>(idx / cols); | ||
int col_idx = static_cast<int>(idx % cols); | ||
mask.at<uint8_t>(row_idx, col_idx) = value; | ||
idx++; | ||
if (idx > rows * cols) { | ||
break; | ||
} | ||
} | ||
} | ||
return mask; | ||
} | ||
|
||
} // namespace autoware::tensorrt_yolox |