1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-25 21:33:04 +04:00
Files
opencv/samples/cpp/tutorial_code/snippets/3d_sac_segmentation.cpp
T
Ruan 80c5d18f9c Merge pull request #21276 from No-Plane-Cannot-Be-Detected:5.x-ptcloud
Add support for 3D point cloud segmentation, using the USAC framework.

* Modify the RANSAC framework in usac such that RANSAC can be used in 3D point cloud segmentation.

* 1. Add support for 3D point cloud segmentation, using the USAC framework.
2. Add solvers, error estimators for plane model and sphere model.

* Added code samples to the comments of class SACSegmentation.

* 1. Update the segment interface parameters of SACSegmentation.
2. Fix some errors in variable naming.

* Add tests for plane detection.

* 1. Add tests for sphere segmentation.
2. Fix some bugs found by tests.
3. Rename "segmentation" to "sac segmentation".
4. Rename "detect" to "segment".
TODO: Too much duplicate code, the structure of the test needs to be rebuilt.

* 1. Use SIMD acceleration for plane model and sphere model error estimation.
2. Optimize the RansacQualityImpl#getScore function to avoid multiple calls to the error#getError function.
3. Fix a warning in test_sac_segmentation.cpp.

* 1. Fix the warning of ModelConstraintFunction ambiguity.
2. Fix warning: no previous declaration for'void cv::usac::modelParamsToUsacConfig(cv::Ptr<cv::usac::SimpleUsacConfig>&, const cv::Ptr<const cv::usac::Model>& )

* Fix a warning in test_sac_segmentation.cpp about direct comparison of different types of data.

* Add code comments related to the interpretation of model coefficients.

* Update the use of custom model constraint functions.

* Simplified test code structure.

* Update the method of checking plane models.

* Delete test for cylinder.

* Add some comments about UniversalRANSAC.

* 1. The RANSAC paper in the code comments is referenced using the bibtex format.
2. The sample code in the code comments is replaced using @snippet.
3. Change the public API class SACSegmentation to interface.
4. Clean up the old useless code.

* fix warning(no previous declaration) in 3d_sac_segmentation.cpp.

* Fix compilation errors caused by 3d_sac_segmentation.cpp.

* Move the function sacModelMinimumSampleSize() from ptcloud.hpp to sac_segmentation.cpp.

* 1. Change the interface for setting the number of threads to the interface for setting whether to be parallel.
2. Move interface implementation code in ptcloud_utils.hpp to ptcloud_utils.cpp.

* SACSegmentation no longer inherits Algorithm.

* Add the constructor and destructor of SACSegmentation.

* 1. For the declaration of the common API, the prefix and suffix of the parameter names no longer contain underscores.
2. Rename the function _getMatFromInputArray -> getPointsMatFromInputArray.
3. Change part of CV_CheckDepth to CV_CheckDepthEQ.
4. Remove the doxygen flag from the source code.
5. Update the loop termination condition of SIMD in the point cloud section of 3D module.

* fix warning: passing 'bool' chooses 'int' over 'size_t {aka unsigned int}' .

* fix warning: passing 'bool' chooses 'int' over 'size_t {aka unsigned int}' .
2021-12-30 15:54:06 +00:00

90 lines
2.9 KiB
C++

/**
* @file 3d_sac_segmentation.cpp
* @brief It demonstrates the usage of cv::SACSegmentation.
*
* It shows how to implement 3D point cloud plane segmentation
* using the RANSAC algorithm via cv::SACSegmentation, and
* the construction of cv::SACSegmentation::ModelConstraintFunction
*
* @author Yechun Ruan
* @date December 2021
*/
#include <opencv2/core.hpp>
#include <opencv2/3d.hpp>
bool customFunc(const std::vector<double> &model_coefficients);
void usageExampleSacModelConstraintFunction();
int planeSegmentationUsingRANSAC(const cv::Mat &pt_cloud,
std::vector<cv::Vec4d> &planes_coeffs, std::vector<char> &labels);
//! [usageExampleSacModelConstraintFunction]
bool customFunc(const std::vector<double> &model_coefficients)
{
// check model_coefficients
// The plane needs to pass through the origin, i.e. ax+by+cz+d=0 --> d==0
return model_coefficients[3] == 0;
} // end of function customFunc()
void usageExampleSacModelConstraintFunction()
{
using namespace cv;
SACSegmentation::ModelConstraintFunction func_example1 = customFunc;
SACSegmentation::ModelConstraintFunction func_example2 =
[](const std::vector<double> &model_coefficients) {
// check model_coefficients
// The plane needs to pass through the origin, i.e. ax+by+cz+d=0 --> d==0
return model_coefficients[3] == 0;
};
// Using local variables
float x0 = 0.0, y0 = 0.0, z0 = 0.0;
SACSegmentation::ModelConstraintFunction func_example3 =
[x0, y0, z0](const std::vector<double> &model_coeffs) -> bool {
// check model_coefficients
// The plane needs to pass through the point (x0, y0, z0), i.e. ax0+by0+cz0+d == 0
return model_coeffs[0] * x0 + model_coeffs[1] * y0 + model_coeffs[2] * z0
+ model_coeffs[3] == 0;
};
// Next, use the constructed SACSegmentation::ModelConstraintFunction func_example1, 2, 3 ......
}
//! [usageExampleSacModelConstraintFunction]
//! [planeSegmentationUsingRANSAC]
int planeSegmentationUsingRANSAC(const cv::Mat &pt_cloud,
std::vector<cv::Vec4d> &planes_coeffs, std::vector<char> &labels)
{
using namespace cv;
Ptr<SACSegmentation> sacSegmentation =
SACSegmentation::create(SAC_MODEL_PLANE, SAC_METHOD_RANSAC);
sacSegmentation->setDistanceThreshold(0.21);
// The maximum number of iterations to attempt.(default 1000)
sacSegmentation->setMaxIterations(1500);
sacSegmentation->setNumberOfModelsExpected(2);
Mat planes_coeffs_mat;
// Number of final resultant models obtained by segmentation.
int model_cnt = sacSegmentation->segment(pt_cloud,
labels, planes_coeffs_mat);
planes_coeffs.clear();
for (int i = 0; i < model_cnt; ++i)
{
planes_coeffs.push_back(planes_coeffs_mat.row(i));
}
return model_cnt;
}
//! [planeSegmentationUsingRANSAC]
int main()
{
}