8#include <RobotsIO/Camera/SegmentationCamera.h>
9#include <RobotsIO/Camera/CameraParameters.h>
13using namespace RobotsIO::Camera;
17SegmentationCamera::SegmentationCamera(
const CameraParameters& camera_parameters,
const std::string& object_mesh_path,
const double& threshold) :
18 parameters_(camera_parameters),
22 SICAD::ModelPathContainer model;
23 model[
"object"] = object_mesh_path;
24 renderer_ = std::unique_ptr<SICAD>
26 new SICAD(model, parameters_.width(), parameters_.height(), parameters_.fx(), parameters_.fy(), parameters_.cx(), parameters_.cy(), 1)
28 renderer_->setOglToCam({1.0, 0.0, 0.0,
static_cast<float>(M_PI)});
32SegmentationCamera::~SegmentationCamera()
36std::pair<bool, cv::Mat>
SegmentationCamera::mask(
const MatrixXf& scene_depth,
const Transform<double, 3, Affine> object_transform)
38 return render_mask(scene_depth, object_transform);
42std::pair<bool, cv::Mat>
SegmentationCamera::mask(std::shared_ptr<Camera> camera, std::shared_ptr<RobotsIO::Utils::Transform> object_transform)
45 if (!object_transform->freeze(
true))
47 std::cout << log_name_ <<
"::mask. Error: cannot get object pose." << std::endl;
48 return std::make_pair(
false, cv::Mat());
50 Transform<double, 3, Affine> transform = object_transform->transform();
53 bool valid_depth =
false;
54 Eigen::MatrixXf depth;
55 std::tie(valid_depth, depth) = camera->depth(
true);
58 std::cout << log_name_ <<
"::mask. Error: cannot get depth." << std::endl;
59 return std::make_pair(
false, cv::Mat());
62 return render_mask(depth, transform);
65std::pair<bool, cv::Mat> SegmentationCamera::render_mask(
const Eigen::MatrixXf& scene_depth,
const Eigen::Transform<double, 3, Affine> object_transform)
68 SICAD::ModelPose pose;
69 pose.push_back(object_transform.translation()(0));
70 pose.push_back(object_transform.translation()(1));
71 pose.push_back(object_transform.translation()(2));
73 AngleAxisd rotation(object_transform.rotation());
74 Vector3d axis = rotation.axis();
75 pose.push_back(axis(0));
76 pose.push_back(axis(1));
77 pose.push_back(axis(2));
78 pose.push_back(rotation.angle());
80 SICAD::ModelPoseContainer pose_container;
81 pose_container.emplace(
"object", pose);
85 double cam_x [4] = {0.0, 0.0, 0.0};
86 double cam_o [4] = {1.0, 0.0, 0.0, 0.0};
89 cv::Mat rendered_depth;
90 if (!(renderer_->superimpose(pose_container, cam_x, cam_o, placeholder, rendered_depth)))
92 std::cout << log_name_ <<
"::render_mask. Error: cannot render depth." << std::endl;
93 return std::make_pair(
false, cv::Mat());
98#if CV_MAJOR_VERSION >= 4
99 cv::threshold(rendered_depth,
mask, 0.001, 255, cv::THRESH_BINARY);
101 cv::threshold(rendered_depth,
mask, 0.001, 255, CV_THRESH_BINARY);
106 cv::Mat non_zero_coordinates;
107 findNonZero(
mask, non_zero_coordinates);
108 for (std::size_t i = 0; i < non_zero_coordinates.total(); i++)
110 const cv::Point& p = non_zero_coordinates.at<cv::Point>(i);
112 if (std::abs(scene_depth(p.y, p.x) - rendered_depth.at<
float>(p)) > threshold_)
113 mask.at<uchar>(p) = 0.0;
116 return std::make_pair(
true,
mask);
std::pair< bool, cv::Mat > mask(const Eigen::MatrixXf &scene_depth, const Eigen::Transform< double, 3, Eigen::Affine > object_transform)