Skip to content

Commit dfe4a3c

Browse files
Added yolo logging bboxes feature for testing
1 parent d28c3f7 commit dfe4a3c

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

ed_sensor_integration/include/ed/kinect/segmenter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <ed/types.h>
1313

1414
#include <vector>
15+
#include <utility>
1516

1617
namespace cv
1718
{
@@ -57,9 +58,9 @@ class Segmenter
5758
* @param sensor_pose
5859
* @param clusters
5960
* @param rgb_image
60-
* @return std::vector<cv::Mat> masks // 3D pointcloud masks of all the segmented objects
61+
* @return std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> // 3D pointcloud masks and bounding boxes of all the segmented objects
6162
*/
62-
std::vector<cv::Mat> cluster(const cv::Mat& depth_image, const geo::DepthCamera& cam_model,
63+
std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> cluster(const cv::Mat& depth_image, const geo::DepthCamera& cam_model,
6364
const geo::Pose3D& sensor_pose, std::vector<EntityUpdate>& clusters, const cv::Mat& rgb_image, bool logging=false);
6465

6566
private:

ed_sensor_integration/include/ed/kinect/updater.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Updater
6969
//For displaying SAM MASK
7070
ros::Publisher mask_pub_;
7171
ros::Publisher cloud_pub_;
72+
ros::Publisher box_pub_;
7273
bool logging;
7374

7475
};

ed_sensor_integration/include/ed_sensor_integration/kinect/segmodules/sam_seg_module.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88

99
#include "ed/kinect/entity_update.h"
1010

11+
#include <utility>
1112
#include <vector>
1213

1314
/**
1415
* @brief Segmentation pipeline that processes the input image and generates segmentation masks.
1516
*
1617
* @param img The input RGB image to segment.
17-
* @return std::vector<cv::Mat> The generated segmentation masks.
18+
* @return std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> The generated segmentation masks and bounding boxes.
1819
*/
19-
std::vector<cv::Mat> SegmentationPipeline(const cv::Mat& img, tue::Configuration& config);
20+
std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> SegmentationPipeline(const cv::Mat& img, tue::Configuration& config);
2021

2122
/**
2223
* @brief Overlay segmentation masks on the RGB image for visualization purposes.
@@ -33,10 +34,12 @@ void overlayMasksOnImage_(cv::Mat& rgb, const std::vector<cv::Mat>& masks);
3334
* @param rgb The RGB image to publish.
3435
* @param sensor_pose The pose of the sensor.
3536
* @param clustered_images The clustered segmentation masks.
37+
* @param boxes The bounding boxes to visualize.
3638
* @param mask_pub_ The ROS publisher for the mask images.
3739
* @param cloud_pub_ The ROS publisher for the point cloud data.
3840
* @param res_updates The entity updates to publish.
3941
*/
4042
void publishSegmentationResults(const cv::Mat& filtered_depth_image, const cv::Mat& rgb,
4143
const geo::Pose3D& sensor_pose, std::vector<cv::Mat>& clustered_images,
42-
ros::Publisher& mask_pub_, ros::Publisher& cloud_pub_, std::vector<EntityUpdate>& res_updates);
44+
const std::vector<cv::Rect>& boxes,
45+
ros::Publisher& box_pub_, ros::Publisher& mask_pub_, ros::Publisher& cloud_pub_, std::vector<EntityUpdate>& res_updates);

ed_sensor_integration/src/kinect/sam_seg_module.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cv_bridge/cv_bridge.h>
44
#include <filesystem>
5+
#include <utility>
56
#include <pcl/point_cloud.h>
67
#include <pcl/point_types.h>
78
#include <pcl_conversions/pcl_conversions.h>
@@ -11,7 +12,7 @@
1112
#include <yolo_onnx_ros/detection.hpp>
1213

1314

14-
std::vector<cv::Mat> SegmentationPipeline(const cv::Mat& img, tue::Configuration& config)
15+
std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> SegmentationPipeline(const cv::Mat& img, tue::Configuration& config)
1516
{
1617
////////////////////////// YOLO //////////////////////////////////////
1718
std::unique_ptr<YOLO_V8> yoloDetector;
@@ -52,7 +53,7 @@ std::vector<cv::Mat> SegmentationPipeline(const cv::Mat& img, tue::Configuration
5253

5354
SegmentAnything(samSegmentors, params_encoder, params_decoder, img, resSam, res);
5455

55-
return std::move(res.masks);
56+
return std::make_pair(std::move(res.masks), std::move(res.boxes));
5657
}
5758

5859

@@ -111,11 +112,23 @@ void overlayMasksOnImage_(cv::Mat& rgb, const std::vector<cv::Mat>& masks)
111112

112113
void publishSegmentationResults(const cv::Mat& filtered_depth_image, const cv::Mat& rgb,
113114
const geo::Pose3D& sensor_pose, std::vector<cv::Mat>& clustered_images,
114-
ros::Publisher& mask_pub_, ros::Publisher& cloud_pub_, std::vector<EntityUpdate>& res_updates)
115+
const std::vector<cv::Rect>& boxes,
116+
ros::Publisher& box_pub_, ros::Publisher& mask_pub_, ros::Publisher& cloud_pub_, std::vector<EntityUpdate>& res_updates)
115117
{
116118
// Overlay masks on the RGB image
117119
cv::Mat visualization = rgb.clone();
118120

121+
// Box visualization
122+
cv::Mat box_visualization = rgb.clone();
123+
for(const auto& box : boxes)
124+
{
125+
cv::rectangle(box_visualization, box, cv::Scalar(0, 255, 0), 2);
126+
}
127+
128+
sensor_msgs::ImagePtr box_msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", box_visualization).toImageMsg();
129+
box_msg->header.stamp = ros::Time::now();
130+
box_pub_.publish(box_msg);
131+
119132
// Create a path to save the image using platform-independent temp directory
120133
std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
121134
cv::imwrite((temp_dir / "visualization.png").string(), visualization);

ed_sensor_integration/src/kinect/segmenter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,16 @@ cv::Mat Segmenter::preprocessRGBForSegmentation(const cv::Mat& rgb_image,
199199
}
200200
// ----------------------------------------------------------------------------------------------------
201201

202-
std::vector<cv::Mat> Segmenter::cluster(const cv::Mat& depth_image, const geo::DepthCamera& cam_model,
202+
std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> Segmenter::cluster(const cv::Mat& depth_image, const geo::DepthCamera& cam_model,
203203
const geo::Pose3D& sensor_pose, std::vector<EntityUpdate>& clusters, const cv::Mat& rgb_image, bool logging)
204204
{
205205
int width = depth_image.cols;
206206
int height = depth_image.rows;
207207
ROS_DEBUG("Cluster with depth image of size %i, %i", width, height);
208208

209-
std::vector<cv::Mat> masks = SegmentationPipeline(rgb_image.clone(), config_);
209+
std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> seg_result = SegmentationPipeline(rgb_image.clone(), config_);
210+
std::vector<cv::Mat>& masks = seg_result.first;
211+
210212
ROS_DEBUG("Creating clusters");
211213

212214
// Pre-allocate temporary storage (one per mask, avoid push_back races)
@@ -325,5 +327,5 @@ std::vector<cv::Mat> Segmenter::cluster(const cv::Mat& depth_image, const geo::D
325327
}
326328
}
327329

328-
return masks;
330+
return seg_result;
329331
}

ed_sensor_integration/src/kinect/updater.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ Updater::Updater(tue::Configuration config) : logging(false)
222222
{
223223
ros::NodeHandle nh("~");
224224
mask_pub_ = nh.advertise<sensor_msgs::Image>("segmentation_masks", 1);
225-
cloud_pub_ = nh.advertise<sensor_msgs::PointCloud2>("point_cloud_ooo", 1);
225+
cloud_pub_ = nh.advertise<sensor_msgs::PointCloud2>("point_cloud_sam", 1);
226+
box_pub_ = nh.advertise<sensor_msgs::Image>("bounding_boxes_yolo", 1);
226227
}
227228
}
228229

@@ -426,10 +427,13 @@ bool Updater::update(const ed::WorldModel& world, const rgbd::ImageConstPtr& ima
426427
// - - - - - - - - - - - - - - - - - - - - - - - -
427428
// Cluster
428429
filtered_rgb_image = segmenter_->preprocessRGBForSegmentation(rgb, filtered_depth_image);
429-
std::vector<cv::Mat> clustered_images = segmenter_->cluster(filtered_depth_image, cam_model, sensor_pose, res.entity_updates, filtered_rgb_image, logging);
430+
std::pair<std::vector<cv::Mat>, std::vector<cv::Rect>> cluster_result = segmenter_->cluster(filtered_depth_image, cam_model, sensor_pose, res.entity_updates, filtered_rgb_image, logging);
431+
432+
std::vector<cv::Mat>& clustered_images = cluster_result.first;
433+
430434
if (logging)
431435
{
432-
publishSegmentationResults(filtered_depth_image, filtered_rgb_image, sensor_pose, clustered_images, mask_pub_, cloud_pub_, res.entity_updates);
436+
publishSegmentationResults(filtered_depth_image, filtered_rgb_image, sensor_pose, clustered_images, cluster_result.second, box_pub_, mask_pub_, cloud_pub_, res.entity_updates);
433437
}
434438
// - - - - - - - - - - - - - - - - - - - - - - - -
435439
// Merge the detected clusters if they overlap in XY or Z

0 commit comments

Comments
 (0)