Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimization "DepthImage" algorithm stage #47

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cpp/deplex/include/deplex/utils/depth_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ class DepthImage {
*/
Eigen::MatrixX3f toPointCloud(Eigen::Matrix3f const& intrinsics) const;

void reset(std::string const& image_path);

private:
std::unique_ptr<unsigned short> image_;
int32_t width_;
int32_t height_;

Eigen::VectorXf column_indices_;
Eigen::VectorXf row_indices_;
};
} // namespace utils
} // namespace deplex
32 changes: 24 additions & 8 deletions cpp/deplex/src/deplex/utils/depth_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,47 @@ DepthImage::DepthImage(std::string const& image_path) : width_(0), height_(0) {
throw std::runtime_error("Error: Couldn't read image " + image_path);
}
image_.reset(data_ptr);

column_indices_ = Eigen::VectorXf::LinSpaced(width_, 0.0, width_ - 1).replicate(height_, 1);
row_indices_ = Eigen::VectorXf::LinSpaced(height_, 0.0, height_ - 1).replicate(1, width_).reshaped<Eigen::RowMajor>();
}

void DepthImage::reset(std::string const& image_path) {
int32_t actual_channels = 0;
auto data_ptr = stbi_load_16(image_path.c_str(), &width_, &height_, &actual_channels, STBI_grey);
if (data_ptr == nullptr) {
throw std::runtime_error("Error: Couldn't read image " + image_path);
}
image_.reset(data_ptr);
}

int32_t DepthImage::getWidth() const { return width_; }

int32_t DepthImage::getHeight() const { return height_; }

Eigen::MatrixX3f DepthImage::toPointCloud(Eigen::Matrix3f const& intrinsics) const {
Eigen::VectorXf column_indices = Eigen::VectorXf::LinSpaced(width_, 0.0, width_ - 1).replicate(height_, 1);
Eigen::VectorXf row_indices =
Eigen::VectorXf::LinSpaced(height_, 0.0, height_ - 1).replicate(1, width_).reshaped<Eigen::RowMajor>();

Eigen::Matrix<float, Eigen::Dynamic, 3, Eigen::RowMajor> pcd_points(width_ * height_, 3);

float fx = intrinsics.row(0)[0];
float fy = intrinsics.row(1)[1];
float cx = intrinsics.row(0)[2];
float fy = intrinsics.row(1)[1];
float cy = intrinsics.row(1)[2];

typedef std::remove_reference<decltype(*image_)>::type t_image;
pcd_points.col(2) = Eigen::Map<Eigen::Vector<t_image, Eigen::Dynamic>>(image_.get(), width_ * height_).cast<float>();
pcd_points.col(0) = (column_indices.array() - cx) * pcd_points.col(2).array() / fx;
pcd_points.col(1) = (row_indices.array() - cy) * pcd_points.col(2).array() / fy;

#pragma omp parallel default(none) shared(pcd_points, column_indices, row_indices, cx, cy, fx, fy)
{
#pragma omp sections
{
#pragma omp section
{ pcd_points.col(0) = (column_indices_.array() - cx) * pcd_points.col(2).array() / fx; }
#pragma omp section
{ pcd_points.col(1) = (row_indices_.array() - cy) * pcd_points.col(2).array() / fy; }
}
}

return pcd_points;
}

} // namespace utils
} // namespace deplex