Skip to content

Commit a44805c

Browse files
refactor(yabloc_image_processing): apply static analysis (#7489)
* refactor based on linter Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> * style(pre-commit): autofix --------- Signed-off-by: a-maumau <maumaumaumaumaumaumaumaumaumau@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 97038a1 commit a44805c

File tree

11 files changed

+112
-91
lines changed

11 files changed

+112
-91
lines changed

localization/yabloc/yabloc_image_processing/include/yabloc_image_processing/graph_segment/histogram.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct Histogram
2727
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
2828
explicit Histogram(int bin = 10) : bin(bin) { data = Eigen::MatrixXf::Zero(3, bin); }
2929

30-
Eigen::MatrixXf eval() const
30+
[[nodiscard]] Eigen::MatrixXf eval() const
3131
{
3232
float sum = data.sum();
3333
if (sum < 1e-6f) throw std::runtime_error("invalid division");
@@ -37,7 +37,9 @@ struct Histogram
3737
void add(const cv::Vec3b & rgb)
3838
{
3939
for (int ch = 0; ch < 3; ++ch) {
40-
int index = std::clamp(static_cast<int>(rgb[ch] * bin / 255.f), 0, bin - 1);
40+
int index = std::clamp(
41+
static_cast<int>(static_cast<float>(rgb[ch]) * static_cast<float>(bin) / 255.f), 0,
42+
bin - 1);
4143
data(ch, index) += 1.0f;
4244
}
4345
}

localization/yabloc/yabloc_image_processing/include/yabloc_image_processing/lanelet2_overlay/lanelet2_overlay.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Lanelet2Overlay : public rclcpp::Node
7676
void draw_overlay(
7777
const cv::Mat & image, const std::optional<Pose> & pose, const rclcpp::Time & stamp);
7878
void draw_overlay_line_segments(
79-
cv::Mat & image, const Pose & pose, const LineSegments & line_segments);
79+
cv::Mat & image, const Pose & pose, const LineSegments & near_segments);
8080

8181
void make_vis_marker(const LineSegments & ls, const Pose & pose, const rclcpp::Time & stamp);
8282
};

localization/yabloc/yabloc_image_processing/include/yabloc_image_processing/line_segment_detector/line_segment_detector.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class LineSegmentDetector : public rclcpp::Node
5151

5252
cv::Ptr<cv::LineSegmentDetector> line_segment_detector_;
5353

54-
std::vector<cv::Mat> remove_too_outer_elements(
55-
const cv::Mat & lines, const cv::Size & size) const;
54+
static std::vector<cv::Mat> remove_too_outer_elements(
55+
const cv::Mat & lines, const cv::Size & size);
5656
void on_image(const sensor_msgs::msg::Image & msg);
5757
void execute(const cv::Mat & image, const rclcpp::Time & stamp);
5858
};

localization/yabloc/yabloc_image_processing/include/yabloc_image_processing/segment_filter/segment_filter.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class SegmentFilter : public rclcpp::Node
6363
bool define_project_func();
6464

6565
pcl::PointCloud<pcl::PointNormal> project_lines(
66-
const pcl::PointCloud<pcl::PointNormal> & lines, const std::set<int> & indices,
66+
const pcl::PointCloud<pcl::PointNormal> & points, const std::set<int> & indices,
6767
bool negative = false) const;
6868

69-
std::set<int> filter_by_mask(
69+
static std::set<int> filter_by_mask(
7070
const cv::Mat & mask, const pcl::PointCloud<pcl::PointNormal> & edges);
7171

7272
cv::Point2i to_cv_point(const Eigen::Vector3f & v) const;
73-
void execute(const PointCloud2 & msg1, const Image & msg2);
73+
void execute(const PointCloud2 & line_segments_msg, const Image & segment_msg);
7474

7575
bool is_near_element(const pcl::PointNormal & pn, pcl::PointNormal & truncated_pn) const;
7676
};

localization/yabloc/yabloc_image_processing/src/graph_segment/graph_segment_core.cpp

+22-17
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ namespace yabloc::graph_segment
2525
{
2626
GraphSegment::GraphSegment(const rclcpp::NodeOptions & options)
2727
: Node("graph_segment", options),
28-
target_height_ratio_(declare_parameter<float>("target_height_ratio")),
29-
target_candidate_box_width_(declare_parameter<int>("target_candidate_box_width"))
28+
target_height_ratio_(static_cast<float>(declare_parameter<float>("target_height_ratio"))),
29+
target_candidate_box_width_(
30+
static_cast<int>(declare_parameter<int>("target_candidate_box_width")))
3031
{
3132
using std::placeholders::_1;
3233

@@ -38,8 +39,8 @@ GraphSegment::GraphSegment(const rclcpp::NodeOptions & options)
3839
pub_debug_image_ = create_publisher<Image>("~/debug/segmented_image", 10);
3940

4041
const double sigma = declare_parameter<double>("sigma");
41-
const float k = declare_parameter<float>("k");
42-
const int min_size = declare_parameter<double>("min_size");
42+
const float k = static_cast<float>(declare_parameter<float>("k"));
43+
const int min_size = static_cast<int>(declare_parameter<double>("min_size"));
4344
segmentation_ = cv::ximgproc::segmentation::createGraphSegmentation(sigma, k, min_size);
4445

4546
// additional area pickup module
@@ -52,24 +53,28 @@ GraphSegment::GraphSegment(const rclcpp::NodeOptions & options)
5253
cv::Vec3b random_hsv(int index)
5354
{
5455
// It generates colors that are not too bright or too vivid, but rich in hues.
55-
double base = static_cast<double>(index);
56-
return cv::Vec3b(std::fmod(base * 0.7, 1.0) * 180, 0.7 * 255, 0.5 * 255);
56+
auto base = static_cast<double>(index);
57+
return {
58+
static_cast<unsigned char>(std::fmod(base * 0.7, 1.0) * 180),
59+
static_cast<unsigned char>(0.7 * 255), static_cast<unsigned char>(0.5 * 255)};
5760
};
5861

5962
int GraphSegment::search_most_road_like_class(const cv::Mat & segmented) const
6063
{
61-
const int W = target_candidate_box_width_;
62-
const float R = target_height_ratio_;
63-
cv::Point2i target_px(segmented.cols * 0.5, segmented.rows * R);
64-
cv::Rect2i rect(target_px + cv::Point2i(-W, -W), target_px + cv::Point2i(W, W));
64+
const int bw = target_candidate_box_width_;
65+
const float r = target_height_ratio_;
66+
cv::Point2i target_px(
67+
static_cast<int>(static_cast<float>(segmented.cols) * 0.5),
68+
static_cast<int>(static_cast<float>(segmented.rows) * r));
69+
cv::Rect2i rect(target_px + cv::Point2i(-bw, -bw), target_px + cv::Point2i(bw, bw));
6570

6671
std::unordered_map<int, int> areas;
6772
std::unordered_set<int> candidates;
6873
for (int h = 0; h < segmented.rows; h++) {
6974
const int * seg_ptr = segmented.ptr<int>(h);
7075
for (int w = 0; w < segmented.cols; w++) {
7176
int key = seg_ptr[w];
72-
if (areas.count(key) == 0) areas[key] = 0;
77+
areas.try_emplace(key, 0);
7378
areas[key]++;
7479
if (rect.contains(cv::Point2i{w, h})) candidates.insert(key);
7580
}
@@ -111,8 +116,8 @@ void GraphSegment::on_image(const Image & msg)
111116
cv::Mat debug_image = cv::Mat::zeros(resized.size(), CV_8UC3);
112117
for (int h = 0; h < resized.rows; h++) {
113118
// NOTE: Accessing through ptr() is faster than at()
114-
cv::Vec3b * const debug_image_ptr = debug_image.ptr<cv::Vec3b>(h);
115-
uchar * const output_image_ptr = output_image.ptr<uchar>(h);
119+
auto * const debug_image_ptr = debug_image.ptr<cv::Vec3b>(h);
120+
auto * const output_image_ptr = output_image.ptr<uchar>(h);
116121
const int * const segmented_image_ptr = segmented.ptr<int>(h);
117122

118123
for (int w = 0; w < resized.cols; w++) {
@@ -148,10 +153,10 @@ void GraphSegment::draw_and_publish_image(
148153

149154
// Draw target rectangle
150155
{
151-
const int W = target_candidate_box_width_;
152-
const float R = target_height_ratio_;
153-
cv::Point2i target(size.width / 2, size.height * R);
154-
cv::Rect2i rect(target + cv::Point2i(-W, -W), target + cv::Point2i(W, W));
156+
const int w = target_candidate_box_width_;
157+
const float r = target_height_ratio_;
158+
cv::Point2i target(size.width / 2, static_cast<int>(static_cast<float>(size.height) * r));
159+
cv::Rect2i rect(target + cv::Point2i(-w, -w), target + cv::Point2i(w, w));
155160
cv::rectangle(show_image, rect, cv::Scalar::all(0), 2);
156161
}
157162

localization/yabloc/yabloc_image_processing/src/graph_segment/similar_area_searcher.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ std::set<int> SimilarAreaSearcher::search(
3737

3838
for (int h = 0; h < rgb_image.rows; h++) {
3939
const int * seg_ptr = segmented.ptr<int>(h);
40-
const cv::Vec3b * rgb_ptr = rgb_image.ptr<cv::Vec3b>(h);
40+
const auto * rgb_ptr = rgb_image.ptr<cv::Vec3b>(h);
4141

4242
for (int w = 0; w < rgb_image.cols; w++) {
4343
int key = seg_ptr[w];

localization/yabloc/yabloc_image_processing/src/lanelet2_overlay/lanelet2_overlay_core.cpp

+25-21
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ void Lanelet2Overlay::on_image(const sensor_msgs::msg::Image & msg)
7272
// Search synchronized pose
7373
float min_abs_dt = std::numeric_limits<float>::max();
7474
std::optional<Pose> synched_pose{std::nullopt};
75-
for (auto pose : pose_buffer_) {
75+
for (const auto & pose : pose_buffer_) {
7676
auto dt = (rclcpp::Time(pose.header.stamp) - stamp);
7777
auto abs_dt = std::abs(dt.seconds());
7878
if (abs_dt < min_abs_dt) {
79-
min_abs_dt = abs_dt;
79+
min_abs_dt = static_cast<float>(abs_dt);
8080
synched_pose = pose.pose;
8181
}
8282
}
@@ -93,19 +93,17 @@ void Lanelet2Overlay::on_line_segments(const PointCloud2 & msg)
9393
// Search synchronized pose
9494
float min_dt = std::numeric_limits<float>::max();
9595
geometry_msgs::msg::PoseStamped synched_pose;
96-
for (auto pose : pose_buffer_) {
96+
for (const auto & pose : pose_buffer_) {
9797
auto dt = (rclcpp::Time(pose.header.stamp) - stamp);
9898
auto abs_dt = std::abs(dt.seconds());
9999
if (abs_dt < min_dt) {
100-
min_dt = abs_dt;
100+
min_dt = static_cast<float>(abs_dt);
101101
synched_pose = pose;
102102
}
103103
}
104104
if (min_dt > 0.1) return;
105105
auto latest_pose_stamp = rclcpp::Time(pose_buffer_.back().header.stamp);
106106

107-
std::vector<int> a;
108-
109107
LineSegments line_segments_cloud;
110108
pcl::fromROSMsg(msg, line_segments_cloud);
111109
make_vis_marker(line_segments_cloud, synched_pose.pose, stamp);
@@ -139,32 +137,35 @@ void Lanelet2Overlay::draw_overlay_line_segments(
139137
if (!camera_extrinsic_.has_value()) return;
140138
if (!info_.has_value()) return;
141139

142-
Eigen::Matrix3f K =
140+
Eigen::Matrix3f k =
143141
Eigen::Map<Eigen::Matrix<double, 3, 3> >(info_->k.data()).cast<float>().transpose();
144-
Eigen::Affine3f T = camera_extrinsic_.value();
142+
Eigen::Affine3f t = camera_extrinsic_.value();
145143

146144
Eigen::Affine3f transform = ground_plane_.align_with_slope(common::pose_to_affine(pose));
147145

148-
auto projectLineSegment =
149-
[K, T, transform](
146+
auto project_line_segment =
147+
[k, t, transform](
150148
const Eigen::Vector3f & p1,
151149
const Eigen::Vector3f & p2) -> std::tuple<bool, cv::Point2i, cv::Point2i> {
152-
Eigen::Vector3f from_camera1 = K * T.inverse() * transform.inverse() * p1;
153-
Eigen::Vector3f from_camera2 = K * T.inverse() * transform.inverse() * p2;
154-
constexpr float EPSILON = 0.1f;
155-
bool p1_is_visible = from_camera1.z() > EPSILON;
156-
bool p2_is_visible = from_camera2.z() > EPSILON;
150+
Eigen::Vector3f from_camera1 = k * t.inverse() * transform.inverse() * p1;
151+
Eigen::Vector3f from_camera2 = k * t.inverse() * transform.inverse() * p2;
152+
constexpr float epsilon = 0.1f;
153+
bool p1_is_visible = from_camera1.z() > epsilon;
154+
bool p2_is_visible = from_camera2.z() > epsilon;
157155
if ((!p1_is_visible) && (!p2_is_visible)) return {false, cv::Point2i{}, cv::Point2i{}};
158156

159-
Eigen::Vector3f uv1, uv2;
157+
Eigen::Vector3f uv1;
158+
Eigen::Vector3f uv2;
160159
if (p1_is_visible) uv1 = from_camera1 / from_camera1.z();
161160
if (p2_is_visible) uv2 = from_camera2 / from_camera2.z();
162161

163162
if ((p1_is_visible) && (p2_is_visible))
164-
return {true, cv::Point2i(uv1.x(), uv1.y()), cv::Point2i(uv2.x(), uv2.y())};
163+
return {
164+
true, cv::Point2i(static_cast<int>(uv1.x()), static_cast<int>(uv1.y())),
165+
cv::Point2i(static_cast<int>(uv2.x()), static_cast<int>(uv2.y()))};
165166

166167
Eigen::Vector3f tangent = from_camera2 - from_camera1;
167-
float mu = (EPSILON - from_camera1.z()) / (tangent.z());
168+
float mu = (epsilon - from_camera1.z()) / (tangent.z());
168169
if (!p1_is_visible) {
169170
from_camera1 = from_camera1 + mu * tangent;
170171
uv1 = from_camera1 / from_camera1.z();
@@ -173,11 +174,13 @@ void Lanelet2Overlay::draw_overlay_line_segments(
173174
from_camera2 = from_camera1 + mu * tangent;
174175
uv2 = from_camera2 / from_camera2.z();
175176
}
176-
return {true, cv::Point2i(uv1.x(), uv1.y()), cv::Point2i(uv2.x(), uv2.y())};
177+
return {
178+
true, cv::Point2i(static_cast<int>(uv1.x()), static_cast<int>(uv1.y())),
179+
cv::Point2i(static_cast<int>(uv2.x()), static_cast<int>(uv2.y()))};
177180
};
178181

179182
for (const pcl::PointNormal & pn : near_segments) {
180-
auto [success, u1, u2] = projectLineSegment(pn.getVector3fMap(), pn.getNormalVector3fMap());
183+
auto [success, u1, u2] = project_line_segment(pn.getVector3fMap(), pn.getNormalVector3fMap());
181184
if (success) cv::line(image, u1, u2, cv::Scalar(0, 255, 255), 2);
182185
}
183186
}
@@ -197,7 +200,8 @@ void Lanelet2Overlay::make_vis_marker(
197200
marker.color.a = 0.7f;
198201

199202
for (const auto pn : ls) {
200-
geometry_msgs::msg::Point p1, p2;
203+
geometry_msgs::msg::Point p1;
204+
geometry_msgs::msg::Point p2;
201205
p1.x = pn.x;
202206
p1.y = pn.y;
203207
p1.z = pn.z;

localization/yabloc/yabloc_image_processing/src/line_segment_detector/line_segment_detector_core.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ void LineSegmentDetector::execute(const cv::Mat & image, const rclcpp::Time & st
6767
std::vector<cv::Mat> filtered_lines = remove_too_outer_elements(lines, image.size());
6868

6969
for (const cv::Mat & xy_xy : filtered_lines) {
70-
Eigen::Vector3f xy1, xy2;
70+
Eigen::Vector3f xy1;
71+
Eigen::Vector3f xy2;
7172
xy1 << xy_xy.at<float>(0), xy_xy.at<float>(1), 0;
7273
xy2 << xy_xy.at<float>(2), xy_xy.at<float>(3), 0;
7374
pcl::PointNormal pn;
@@ -79,7 +80,7 @@ void LineSegmentDetector::execute(const cv::Mat & image, const rclcpp::Time & st
7980
}
8081

8182
std::vector<cv::Mat> LineSegmentDetector::remove_too_outer_elements(
82-
const cv::Mat & lines, const cv::Size & size) const
83+
const cv::Mat & lines, const cv::Size & size)
8384
{
8485
std::vector<cv::Rect2i> rect_vector;
8586
rect_vector.emplace_back(0, 0, size.width, 3);

localization/yabloc/yabloc_image_processing/src/line_segments_overlay/line_segments_overlay_core.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ LineSegmentsOverlay::LineSegmentsOverlay(const rclcpp::NodeOptions & options)
3030
using std::placeholders::_1;
3131

3232
auto cb_image = std::bind(&LineSegmentsOverlay::on_image, this, _1);
33-
auto cb_line_segments_ = std::bind(&LineSegmentsOverlay::on_line_segments, this, _1);
33+
auto cb_line_segments = std::bind(&LineSegmentsOverlay::on_line_segments, this, _1);
3434

3535
sub_image_ = create_subscription<Image>("~/input/image_raw", 10, cb_image);
3636
sub_line_segments_ =
37-
create_subscription<PointCloud2>("~/input/line_segments", 10, cb_line_segments_);
37+
create_subscription<PointCloud2>("~/input/line_segments", 10, cb_line_segments);
3838

3939
pub_debug_image_ = create_publisher<Image>("~/debug/image_with_colored_line_segments", 10);
4040
}
@@ -73,8 +73,7 @@ void LineSegmentsOverlay::on_line_segments(const PointCloud2::ConstSharedPtr & l
7373
LineSegments line_segments_cloud;
7474
pcl::fromROSMsg(*line_segments_msg, line_segments_cloud);
7575

76-
for (size_t index = 0; index < line_segments_cloud.size(); ++index) {
77-
const LineSegment & pn = line_segments_cloud.at(index);
76+
for (auto & pn : line_segments_cloud) {
7877
Eigen::Vector3f xy1 = pn.getVector3fMap();
7978
Eigen::Vector3f xy2 = pn.getNormalVector3fMap();
8079

@@ -83,7 +82,9 @@ void LineSegmentsOverlay::on_line_segments(const PointCloud2::ConstSharedPtr & l
8382
color = cv::Scalar(0, 0, 255); // Red
8483
}
8584

86-
cv::line(image, cv::Point(xy1(0), xy1(1)), cv::Point(xy2(0), xy2(1)), color, 2);
85+
cv::line(
86+
image, cv::Point(static_cast<int>(xy1(0)), static_cast<int>(xy1(1))),
87+
cv::Point(static_cast<int>(xy2(0)), static_cast<int>(xy2(1))), color, 2);
8788
}
8889

8990
common::publish_image(*pub_debug_image_, image, stamp);

0 commit comments

Comments
 (0)