27 #include <sensor_msgs/PointCloud2.h> 28 #include <sensor_msgs/Image.h> 30 #include <sensor_msgs/CameraInfo.h> 32 #include <visualization_msgs/Marker.h> 33 #include <visualization_msgs/MarkerArray.h> 36 #include <opencv2/opencv.hpp> 41 #include <pcl/point_cloud.h> 42 #include <pcl/point_types.h> 47 #include <cob_object_detection_msgs/DetectionArray.h> 63 std::cout <<
"\n========== ObjectDetectionVisualizer Parameters ==========\n";
108 const unsigned int detection_array_size = object_detection_msg->detections.size();
109 const unsigned int markers_per_detection = 3+1;
110 const unsigned int marker_array_size = markers_per_detection * detection_array_size;
116 for (
unsigned int i = 0; i < detection_array_size; ++i)
118 for (
unsigned int j = 0; j < 3; ++j)
120 unsigned int idx = markers_per_detection * i + j;
154 marker_array_msg_.markers[idx].pose = object_detection_msg->detections[i].pose.pose;
161 for (
unsigned int j = 3; j < 4; ++j)
163 unsigned int idx = markers_per_detection * i + j;
174 marker_array_msg_.markers[idx].pose = object_detection_msg->detections[i].pose.pose;
177 marker_array_msg_.markers[idx].scale.x = object_detection_msg->detections[i].bounding_box_lwh.x;
178 marker_array_msg_.markers[idx].scale.y = object_detection_msg->detections[i].bounding_box_lwh.y;
179 marker_array_msg_.markers[idx].scale.z = object_detection_msg->detections[i].bounding_box_lwh.z;
185 prev_marker_array_size_ = marker_array_size;
199 ROS_ERROR(
"ObjectDetectionVisualizer::convertColorImageMessageToMat: cv_bridge exception: %s", e.what());
202 image = image_ptr->image;
207 bool convertPclMessageToMat(
const sensor_msgs::PointCloud2::ConstPtr& pointcloud_msg, pcl::PointCloud<pcl::PointXYZRGB>& pointcloud, cv::Mat& color_image)
210 color_image.create(pointcloud.height, pointcloud.width, CV_8UC3);
211 for (
int v = 0; v < (int)pointcloud.height; v++)
213 for (
int u = 0; u < (int)pointcloud.width; u++)
215 const pcl::PointXYZRGB& point = pointcloud(u, v);
216 color_image.at<cv::Vec3b>(v,u) = cv::Vec3b(point.b, point.g, point.r);
227 pcl::PointCloud<pcl::PointXYZRGB> pointcloud;
242 cv::resize(image, display, cv::Size(), 0.5, 0.5);
243 cv::imshow(
"object detections", display);
246 std::stringstream file;
247 file <<
"object_detection_visualizer/" <<
image_counter_ <<
".png";
248 cv::imwrite(file.str().c_str(), image);
252 void detectionImageCallback(
const cob_object_detection_msgs::DetectionArray::ConstPtr& object_detection_msg,
const sensor_msgs::PointCloud2::ConstPtr& pointcloud_msg)
261 pcl::PointCloud<pcl::PointXYZRGB> pointcloud;
267 cv::resize(image, display, cv::Size(), 0.5, 0.5);
268 cv::imshow(
"object detections", display);
271 std::stringstream file;
272 file <<
"object_detection_visualizer/" <<
image_counter_ <<
".png";
273 cv::imwrite(file.str().c_str(), image);
277 bool renderDetections(cv::Mat& image,
const cob_object_detection_msgs::DetectionArray::ConstPtr& object_detection_msg)
281 ROS_WARN(
"Did not receive a projection matrix yet.");
287 if (object_detection_msg->detections.size() <= 0)
290 for (
size_t o=0; o<object_detection_msg->detections.size(); ++o)
292 const cob_object_detection_msgs::Detection& detection = object_detection_msg->detections[o];
305 std::cout <<
"New box corners for object " << detection.label <<
": \n";
306 Eigen::Affine3d pose;
308 std::vector<cv::Vec3d> corners_3d;
309 for (
int k=0; k<2; ++k)
312 for (
int j=-1; j<2; j+=2)
314 for (
int i=-1; i<2; i+=2)
316 Eigen::Affine3d corner = pose * Eigen::Translation3d(i*s*detection.bounding_box_lwh.x, j*detection.bounding_box_lwh.y, k*detection.bounding_box_lwh.z);
317 corners_3d.push_back(cv::Vec3d(corner.translation()(0), corner.translation()(1), corner.translation()(2)));
325 std::vector<cv::Point> corners_2d(corners_3d.size());
326 for (
size_t i=0; i<corners_3d.size(); ++i)
334 ROS_ERROR(
"Unable to draw detected bounding boxes!");
339 cv::Point center_top;
340 for (
int i=4; i<8; ++i)
341 center_top += corners_2d[i];
343 cv::putText(image, detection.label, center_top, cv::FONT_HERSHEY_SIMPLEX, 0.75,
object_name_to_color_map_[detection.label], 2);
353 ROS_WARN(
"Did not receive a projection matrix yet.");
357 return cv::Point(uvw.at<
double>(0,0)/uvw.at<
double>(2,0), uvw.at<
double>(1,0)/uvw.at<
double>(2,0));
360 bool drawDetectedModel3D(cv::Mat image,
const std::vector<cv::Point>& corners_2d,
const cv::Scalar& color)
363 for (
unsigned int j=0; j<3; ++j)
365 cv::line(image, corners_2d[j], corners_2d[j+1], color, 3);
367 cv::line(image, corners_2d[3], corners_2d[0], color, 3);
370 for (
unsigned int j=4; j<corners_2d.size()-1; j++)
372 cv::line(image, corners_2d[j], corners_2d[j+1], color, 3);
374 cv::line(image, corners_2d[7], corners_2d[4], color, 3);
377 for (
unsigned int j=0; j<4; j++)
379 cv::line(image, corners_2d[j], corners_2d[j+4], color, 3);
388 for (
int i = 0; i < 9; i++)
389 f_ptr[i] = data->K[i];
391 ROS_INFO(
"[object_detection_visualizer] Received new projection matrix:");
392 std::cout <<
"\t... / " << std::setw(8) <<
projection_matrix_.at<
double>(0, 0) <<
" ";
394 std::cout << std::setw(8) <<
projection_matrix_.at<
double>(0, 2) <<
" \\ " << std::endl;
395 std::cout <<
"\t... | " << std::setw(8) <<
projection_matrix_.at<
double>(1, 0) <<
" ";
397 std::cout << std::setw(8) <<
projection_matrix_.at<
double>(1, 2) <<
" | "<< std::endl;;
398 std::cout <<
"\t... \\ " << std::setw(8) <<
projection_matrix_.at<
double>(2, 0) <<
" ";
400 std::cout << std::setw(8) <<
projection_matrix_.at<
double>(2, 2) <<
" / "<< std::endl << std::endl;
437 int main(
int argc,
char** argv)
440 ros::init(argc, argv,
"object_detection_visualizer");
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
bool renderDetections(cv::Mat &image, const cob_object_detection_msgs::DetectionArray::ConstPtr &object_detection_msg)
void publish(const boost::shared_ptr< M > &message) const
void objectDetectionDisplayCallback(const cob_object_detection_msgs::DetectionArray::ConstPtr &object_detection_msg)
ros::Subscriber pointcloud_info_sub_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ObjectDetectionVisualizer(ros::NodeHandle &nh)
void poseMsgToEigen(const geometry_msgs::Pose &m, Eigen::Affine3d &e)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Subscriber detection_array_sub_
unsigned int prev_marker_array_size_
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
bool convertPclMessageToMat(const sensor_msgs::PointCloud2::ConstPtr &pointcloud_msg, pcl::PointCloud< pcl::PointXYZRGB > &pointcloud, cv::Mat &color_image)
cv::Mat projection_matrix_
ROSCPP_DECL void spin(Spinner &spinner)
void pointcloudInfoCallback(const sensor_msgs::CameraInfoConstPtr &data)
ros::NodeHandle node_handle_
~ObjectDetectionVisualizer()
bool convertImageMessageToMat(const sensor_msgs::Image::ConstPtr &image_msg, cv_bridge::CvImageConstPtr &image_ptr, cv::Mat &image)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
void objectDetectionArrayCallback(const cob_object_detection_msgs::DetectionArray::ConstPtr &object_detection_msg)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int main(int argc, char **argv)
visualization_msgs::MarkerArray marker_array_msg_
ros::Subscriber pointcloud_sub_
bool drawDetectedModel3D(cv::Mat image, const std::vector< cv::Point > &corners_2d, const cv::Scalar &color)
void pointcloudCallback(const sensor_msgs::PointCloud2::ConstPtr &pointcloud_msg)
bool display_rviz_markers_
void detectionImageCallback(const cob_object_detection_msgs::DetectionArray::ConstPtr &object_detection_msg, const sensor_msgs::PointCloud2::ConstPtr &pointcloud_msg)
ros::Publisher marker_array_publisher_
cv::Point projectPoint(const cv::Vec3d &point_3d)
bool projection_matrix_received_
boost::mutex color_image_mutex_
bool display_detection_image_
std::map< std::string, cv::Scalar > object_name_to_color_map_