10 #include <opencv2/core/core.hpp> 11 #include <opencv2/core/cuda.hpp> 12 #include <opencv2/core/eigen.hpp> 13 #include <opencv2/cudaimgproc.hpp> 14 #include <opencv2/imgproc/imgproc.hpp> 18 using namespace Eigen;
30 observation_model_(std::move(observation_model)),
31 likelihood_gain_(likelihood_gain),
32 num_cuda_stream_(num_cuda_stream),
33 num_img_stream_(observation_model_->getOGLTilesNumber()),
34 cuda_stream_(std::vector<cuda::Stream>(num_cuda_stream))
38 unsigned int img_width = observation_model_->getCamWidth();
39 unsigned int img_height = observation_model_->getCamHeight();
40 unsigned int ogl_tiles_cols = observation_model_->getOGLTilesCols();
41 unsigned int ogl_tiles_rows = observation_model_->getOGLTilesRows();
42 unsigned int feature_dim = (img_width/block_size*2-1) * (img_height/block_size*2-1) * bin_number * 4;
44 cuda_hog_ = cuda::HOG::create(Size(img_width, img_height), Size(block_size, block_size), Size(block_size/2, block_size/2), Size(block_size/2, block_size/2), bin_number);
45 cuda_hog_->setDescriptorFormat(cuda::HOG::DESCR_FORMAT_ROW_BY_ROW);
46 cuda_hog_->setGammaCorrection(
true);
47 cuda_hog_->setWinStride(Size(img_width, img_height));
49 for (
int s = 0; s < num_cuda_stream_; ++s)
51 hand_rendered_.emplace_back (Mat( Size(img_width * ogl_tiles_cols, img_height* ogl_tiles_rows), CV_8UC3));
52 cuda_img_.emplace_back (cuda::GpuMat(Size(img_width * ogl_tiles_cols, img_height* ogl_tiles_rows), CV_8UC3));
53 cuda_img_alpha_.emplace_back (cuda::GpuMat(Size(img_width * ogl_tiles_cols, img_height* ogl_tiles_rows), CV_8UC4));
54 cuda_descriptors_.emplace_back(cuda::GpuMat(Size(num_img_stream_, feature_dim), CV_32F ));
55 cpu_descriptors_.emplace_back (Mat( Size(num_img_stream_, feature_dim), CV_32F ));
65 for (
int s = 0; s < num_cuda_stream_; ++s)
66 observation_model_->observe(pred_states.block(0, s * num_img_stream_, 7, num_img_stream_), hand_rendered_[s]);
68 for (
int s = 0; s < num_cuda_stream_; ++s)
70 cuda_img_[s].upload(hand_rendered_[s], cuda_stream_[s]);
71 cuda::cvtColor(cuda_img_[s], cuda_img_alpha_[s], COLOR_BGR2BGRA, 4, cuda_stream_[s]);
72 cuda_hog_->compute(cuda_img_alpha_[s], cuda_descriptors_[s], cuda_stream_[s]);
75 for (
int s = 0; s < num_cuda_stream_; ++s)
77 cuda_stream_[s].waitForCompletion();
79 cuda_descriptors_[s].download(cpu_descriptors_[s], cuda_stream_[s]);
81 for (
int i = 0; i < num_img_stream_; ++i)
85 auto it_cam = (
static_cast<const std::vector<float>*
>(measurements.getObj()))->begin();
86 auto it_cam_end = (
static_cast<const std::vector<float>*
>(measurements.getObj()))->end();
88 while (it_cam < it_cam_end)
90 sum_diff += abs((*it_cam) - cpu_descriptors_[s].at<float>(i, j));
97 innovations(s * num_img_stream_ + i, 0) = sum_diff;
105 return exp(-likelihood_gain_ * innovations.cast<
double>().coeff(0, 0));
111 return *observation_model_;
117 throw std::runtime_error(
"ERROR::VISUALUPDATEPARTICLES::SETVISUALOBSERVATIONMODEL\nERROR:\n\tClass under development. Do not invoke this method.");
119 VisualObservationModel* tmp_observation_model = visual_observation_model.release();
121 observation_model_ = std::unique_ptr<VisualProprioception>(
dynamic_cast<VisualProprioception*
>(tmp_observation_model));
126 Ref<MatrixXf> cor_states, Ref<VectorXf> cor_weights)
128 VectorXf innovations(pred_states.cols());
129 innovation(pred_states, measurements, innovations);
131 cor_states = pred_states;
133 for (
int i = 0; i < innovations.rows(); ++i)
135 cor_weights(i) = pred_weights(i) * likelihood(innovations.row(i));
137 if (cor_weights(i) <= 0)
138 cor_weights(i) = std::numeric_limits<float>::min();
void innovation(const Eigen::Ref< const Eigen::MatrixXf > &pred_states, cv::InputArray measurements, Eigen::Ref< Eigen::MatrixXf > innovations) override
VisualUpdateParticles(std::unique_ptr< VisualProprioception > observation_model) noexcept
~VisualUpdateParticles() noexcept
void correctStep(const Eigen::Ref< const Eigen::MatrixXf > &pred_states, const Eigen::Ref< const Eigen::VectorXf > &pred_weights, cv::InputArray measurements, Eigen::Ref< Eigen::MatrixXf > cor_states, Eigen::Ref< Eigen::VectorXf > cor_weights) override
void setVisualObservationModel(std::unique_ptr< bfl::VisualObservationModel > visual_observation_model) override
double likelihood(const Eigen::Ref< const Eigen::MatrixXf > &innovations) override
bfl::VisualObservationModel & getVisualObservationModel() override