-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[GSoC] Point Cloud Object Fitting #2584
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
Open
devanshbatra04
wants to merge
8
commits into
opencv:4.x
Choose a base branch
from
devanshbatra04:ptcloud
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d59f7c
initial commit for ptcloud object fitting
devanshbatra04 325ce02
corrected build issue and added sample data and code
devanshbatra04 cb009e4
added highgui as dependency and removed imgproc
devanshbatra04 1a40d52
include DOXYGEN docs
devanshbatra04 3b0990b
remove redundant doxygen param
devanshbatra04 806549f
completed cylinder fitting
devanshbatra04 1283fee
added code and sample program for segmentation
devanshbatra04 5eea165
removed trailing whitespace and minor warning corrections
devanshbatra04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
set(the_description "Point Cloud Object Fitting API") | ||
ocv_define_module(ptcloud opencv_core opencv_highgui opencv_viz WRAP python) | ||
|
||
# add test data from samples dir to contrib/ptcloud | ||
ocv_add_testdata(samples/ contrib/ptcloud FILES_MATCHING PATTERN "*.ply") | ||
|
||
# add data point cloud files to installation | ||
file(GLOB POINTCLOUD_DATA samples/*.ply) | ||
install(FILES ${POINTCLOUD_DATA} DESTINATION ${OPENCV_OTHER_INSTALL_PATH}/ptcloud COMPONENT libs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//! @addtogroup ptcloud | ||
//! @{ | ||
|
||
Point Cloud Module, Object Fitting API | ||
======================================= | ||
|
||
|
||
To Do | ||
----------------------------------------- | ||
- Cylinder Model Fitting | ||
- Segmentation | ||
- Integrate with Maksym's work | ||
|
||
//! @} |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#ifndef OPENCV_PTCLOUD_HPP | ||
#define OPENCV_PTCLOUD_HPP | ||
|
||
#include "opencv2/ptcloud/sac_segmentation.hpp" | ||
|
||
#endif |
195 changes: 195 additions & 0 deletions
195
modules/ptcloud/include/opencv2/ptcloud/sac_segmentation.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#ifndef OPENCV_PTCLOUD_SAC_SEGMENTATION | ||
#define OPENCV_PTCLOUD_SAC_SEGMENTATION | ||
|
||
#include <vector> | ||
#include <utility> | ||
#include "opencv2/viz.hpp" | ||
#define PLANE_MODEL 1 | ||
#define SPHERE_MODEL 2 | ||
#define CYLINDER_MODEL 3 | ||
#define SAC_METHOD_RANSAC 1 | ||
using namespace std; | ||
|
||
namespace cv | ||
{ | ||
namespace ptcloud | ||
{ | ||
//! @addtogroup ptcloud | ||
//! @{ | ||
class CV_EXPORTS_W SACModel: public Algorithm { | ||
public: | ||
std::vector<double> ModelCoefficients; | ||
|
||
SACModel() { | ||
|
||
} | ||
|
||
SACModel(std::vector<double> ModelCoefficients); | ||
|
||
virtual ~SACModel() | ||
{ | ||
|
||
} | ||
|
||
}; | ||
|
||
class CV_EXPORTS_W SACPlaneModel : public SACModel { | ||
private: | ||
Point3d center; | ||
Vec3d normal; | ||
Size2d size = Size2d(2.0, 2.0); | ||
public: | ||
~ SACPlaneModel() | ||
{ | ||
|
||
} | ||
|
||
SACPlaneModel() { | ||
|
||
} | ||
|
||
/** @brief Create a plane model based on the given coefficients and a center point. | ||
|
||
@param coefficients coefficients in the plane equations of type Ax + By + Cz + D = 0. Also obtained using SACModelFitting. | ||
@param center the center point of the plane. | ||
@param size the size of the plane. | ||
*/ | ||
|
||
SACPlaneModel(Vec4d coefficients, Point3d center, Size2d size=Size2d(2.0, 2.0)); | ||
|
||
/** @brief Create a plane model based on the given coefficients and an arbitrary center point. | ||
|
||
@param coefficients coefficients in the plane equations Ax + By + Cz + D = 0. | ||
@param size the size of the plane. | ||
*/ | ||
SACPlaneModel(Vec4d coefficients, Size2d size=Size2d(2.0, 2.0)); | ||
|
||
/** @brief Create a plane model based on the given coefficients and an arbitrary center point. | ||
|
||
@param coefficients coefficients in the plane equations Ax + By + Cz + D = 0. | ||
@param size the size of the plane. | ||
*/ | ||
SACPlaneModel(std::vector<double> coefficients, Size2d size=Size2d(2.0, 2.0)); | ||
|
||
viz::WPlane WindowWidget (); | ||
|
||
std::pair<double, double> getInliers(Mat cloud, std::vector<unsigned> indices, const double threshold, std::vector<unsigned>& inliers); | ||
}; | ||
|
||
|
||
class CV_EXPORTS_W SACSphereModel : public SACModel { | ||
public: | ||
Point3d center; | ||
double radius; | ||
|
||
~ SACSphereModel() | ||
{ | ||
|
||
} | ||
|
||
SACSphereModel() { | ||
|
||
} | ||
|
||
/** @brief Create a spherical model based on the given center and radius. | ||
|
||
@param center the center point of the sphere | ||
@param radius the radius of the sphere. | ||
*/ | ||
|
||
SACSphereModel(Point3d center, double radius); | ||
|
||
/** @brief Create a spherical model based on the parametric coefficients. | ||
|
||
This is very helpful for creating a model for the fit models using SACModelFitting class. | ||
|
||
@param Coefficients parametric coefficients for the Sphere model | ||
*/ | ||
|
||
SACSphereModel(const std::vector<double> Coefficients); | ||
|
||
SACSphereModel(Vec4d coefficients); | ||
|
||
viz::WSphere WindowWidget (); | ||
|
||
double euclideanDist(Point3d& p, Point3d& q); | ||
|
||
std::pair<double, double> getInliers(Mat cloud, std::vector<unsigned> indices, const double threshold, std::vector<unsigned>& inliers); | ||
}; | ||
|
||
class CV_EXPORTS_W SACModelFitting { | ||
private: | ||
Mat cloud; | ||
int model_type; | ||
int method_type; | ||
double threshold; | ||
long unsigned max_iters; | ||
|
||
public: | ||
// cv::Mat remainingCloud; // will be used while segmentation | ||
|
||
// Inlier indices only, not the points themselves. It would work like a mask output for segmentation in 2d. | ||
vector<vector<unsigned>> inliers; | ||
vector<SACModel> model_instances; | ||
|
||
/** @brief Initializes SACModelFitting class. | ||
|
||
Threshold and Iterations may also be set separately. | ||
|
||
@param cloud input Point Cloud. | ||
@param model_type type of model fitting to attempt - values can be either PLANE_MODEL, SPHERE_MODEL, or CYLINDER_MODEL. | ||
@param method_type which method to use - currently, only RANSAC is supported (use value SAC_METHOD_RANSAC). | ||
@param threshold set the threshold while choosing inliers. | ||
@param max_iters number of iterations for Sampling. | ||
*/ | ||
|
||
SACModelFitting (Mat cloud, int model_type = PLANE_MODEL, int method_type = SAC_METHOD_RANSAC, double threshold = 20,int max_iters = 1000); | ||
// :cloud(cloud), model_type(model_type), method_type(method_type), threshold(threshold), max_iters(max_iters) {} | ||
|
||
/** @brief Initializes SACModelFitting class. | ||
|
||
Threshold and Iterations may also be set separately. | ||
|
||
@param model_type type of model fitting to attempt - values can be either PLANE_MODEL, SPHERE_MODEL, or CYLINDER_MODEL. | ||
@param method_type which method to use - currently, only RANSAC is supported (use value SAC_METHOD_RANSAC). | ||
@param threshold set the threshold while choosing inliers. | ||
@param max_iters number of iterations for Sampling. | ||
*/ | ||
SACModelFitting (int model_type = PLANE_MODEL, int method_type = SAC_METHOD_RANSAC, double threshold = 20,int max_iters = 1000); | ||
// :model_type(model_type), method_type(method_type), threshold(threshold), max_iters(max_iters) {} | ||
|
||
/** @brief Get one model (plane), this function would get the best fitting model on the given set of points. | ||
|
||
note: This stores the model in the public class member model_instances, and the mask for inliers in inliers. | ||
*/ | ||
void fit_once(); | ||
|
||
/** @brief Set the threshold for the fitting. | ||
The threshold is usually the distance from the boundary of model, but may vary from model to model. | ||
|
||
This may be helpful when multiple fitting operations are to be performed. | ||
@param threshold the threshold to set. | ||
*/ | ||
void set_threshold (double threshold); | ||
|
||
/** @brief Set the number of iterations for the fitting. | ||
|
||
This may be helpful when multiple fitting operations are to be performed. | ||
@param iterations the threshold to set. | ||
*/ | ||
void set_iterations (long unsigned iterations); | ||
}; | ||
|
||
bool getSphereFromPoints(const Vec3f*&, const vector<unsigned int>&, Point3d&, double&); | ||
|
||
Vec4d getPlaneFromPoints(const Vec3f*&, const std::vector<unsigned int>&, cv::Point3d&); | ||
|
||
double euclideanDist(Point3d& p, Point3d& q); | ||
|
||
} // ptcloud | ||
} // cv | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <opencv2/viz.hpp> | ||
#include <opencv2/highgui.hpp> | ||
#include <opencv2/viz/widgets.hpp> | ||
#include <opencv2/ptcloud.hpp> | ||
#include <cassert> | ||
#include <numeric> | ||
#include <cmath> | ||
#include <string> | ||
|
||
using namespace cv; | ||
using namespace std; | ||
|
||
int main() { | ||
Mat cloud = cv::viz::readCloud("./data/CobbleStones.obj"); | ||
cv::ptcloud::SACModelFitting planar_segmentation(cloud); | ||
|
||
// add original cloud to window | ||
viz::Viz3d window("original cloud"); | ||
viz::WCloud original_cloud(cloud); | ||
window.showWidget("cloud", original_cloud); | ||
|
||
|
||
planar_segmentation.set_threshold(0.001); | ||
planar_segmentation.set_iterations(1000); | ||
planar_segmentation.fit_once(); | ||
|
||
// Adds segmented (int this case fit, since only once) plane to window | ||
|
||
const Vec3f* points = cloud.ptr<Vec3f>(0); | ||
vector<unsigned> inlier_vec = planar_segmentation.inliers.at(0); | ||
cv::Mat fit_cloud(1, inlier_vec.size(), CV_32FC3); | ||
for(int j=0; j<fit_cloud.cols; ++j) | ||
fit_cloud.at<Vec3f>(0, j) = points[inlier_vec.at(j)]; | ||
|
||
viz::Viz3d fitted("fitted cloud"); | ||
viz::WCloud cloud_widget2(fit_cloud, viz::Color::green()); | ||
fitted.showWidget("fit plane", cloud_widget2); | ||
|
||
window.showWidget("fit plane", cloud_widget2); | ||
|
||
vector<double> model_coefficients = planar_segmentation.model_instances.at(0).ModelCoefficients; | ||
cv::ptcloud::SACPlaneModel SACplane (model_coefficients); | ||
|
||
window.spin(); | ||
fitted.spin(); | ||
waitKey(1); | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <opencv2/viz.hpp> | ||
#include <opencv2/highgui.hpp> | ||
#include <opencv2/viz/widgets.hpp> | ||
#include <opencv2/ptcloud.hpp> | ||
#include <cassert> | ||
#include <numeric> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace cv; | ||
using namespace std; | ||
|
||
int main() { | ||
Mat cloud = cv::viz::readCloud("./data/sphere-big.obj"); | ||
cv::ptcloud::SACModelFitting sphere_segmentation(cloud, 2); | ||
|
||
/// Adds original cloud to window | ||
viz::Viz3d window("original cloud"); | ||
viz::WCloud cloud_widget1(cloud); | ||
window.showWidget("cloud 1", cloud_widget1); | ||
|
||
sphere_segmentation.set_threshold(0.001); | ||
sphere_segmentation.set_iterations(10000); | ||
|
||
viz::Viz3d fitted("fitted cloud"); | ||
|
||
sphere_segmentation.fit_once(); | ||
vector<double> model_coefficients = sphere_segmentation.model_instances.at(0).ModelCoefficients; | ||
cout << sphere_segmentation.model_instances.at(0).ModelCoefficients.size(); | ||
cv::ptcloud::SACSphereModel sphere (model_coefficients); | ||
cout << sphere.center; | ||
cout << sphere.radius; | ||
sphere.radius *= 0.75; | ||
|
||
viz::WSphere model = sphere.WindowWidget(); | ||
window.showWidget("model", model); | ||
|
||
window.spin(); | ||
waitKey(1); | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
#ifndef OPENCV_PTCLOUD_PRECOMP_HPP | ||
#define OPENCV_PTCLOUD_PRECOMP_HPP | ||
#include <opencv2/core.hpp> | ||
#include "opencv2/ptcloud/sac_segmentation.hpp" | ||
|
||
#include <iostream> | ||
#include <stdio.h> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enums are a better choice than defines