pcl-2 pcl结合opencv做svm分类(高程数据)

    pcl自动的ml文件夹内存在svm分类器,但是遗憾的是,我并不能掌握应用方法,因此借用opencv的cv空间进行了点云坐标的转移,使用opencv的svm功能进行调试。现在是最开始的版本
   仅仅使用了高程数据,并且没有做归一化等操作
 

#include 
#include 
#include 
#include 
#include 

#include 
#include 

#include 
#include 


int main() {
    // 读取初始点云
    pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
    pcl::PCDReader reader;
    reader.read("svmtest.pcd", *cloud);

    cout << "初始点云读取完成" << endl;

    // 读取带标签的点云
    pcl::PointCloud::Ptr labeledCloud(new pcl::PointCloud);
    reader.read("svmlearn_xyzl.pcd", *labeledCloud);

    cout << "标签点云读取完成" << endl;

    // 准备训练数据和标签
    cv::Mat trainingData(labeledCloud->size(), 3, CV_32FC1);
    cv::Mat labels(labeledCloud->size(), 1, CV_32SC1);

    for (size_t i = 0; i < labeledCloud->size(); ++i) {
        trainingData.at(i, 0) = labeledCloud->points[i].x;
        trainingData.at(i, 1) = labeledCloud->points[i].y;
        trainingData.at(i, 2) = labeledCloud->points[i].z;

        // 根据点的标签设置标签数据
        labels.at(i, 0) = labeledCloud->points[i].label;
    }

    cout << "根据点的标签设置标签数据完成" << endl;

    // 创建并训练SVM分类器
    cv::Ptr svm = cv::ml::SVM::create();
    svm->setType(cv::ml::SVM::C_SVC);
    svm->setKernel(cv::ml::SVM::RBF);
    svm->setC(10);
    svm->setGamma(0.001);
    svm->train(trainingData, cv::ml::ROW_SAMPLE, labels);


    cout << "创建并训练SVM分类器完成,正在开始对点云进行分类" << endl;

    // 对初始点云进行分类
    cv::Mat testData(cloud->size(), 3, CV_32FC1);

    for (size_t i = 0; i < cloud->size(); ++i) 
    {
        testData.at(i, 0) = cloud->points[i].x;
        testData.at(i, 1) = cloud->points[i].y;
        testData.at(i, 2) = cloud->points[i].z;
    }

    cv::Mat predictedLabels;
    svm->predict(testData, predictedLabels);

    cout << "正在将分类结果添加到点云中" << endl;

    // 将分类结果添加到点云中
    pcl::PointCloud::Ptr classifiedCloud(new pcl::PointCloud);
    classifiedCloud->resize(cloud->size());

     

    for (size_t i = 0; i < cloud->size(); ++i) {
        classifiedCloud->points[i].x = cloud->points[i].x;
        classifiedCloud->points[i].y = cloud->points[i].y;
        classifiedCloud->points[i].z = cloud->points[i].z;

        // 修正标签值(假设标签是 0 或 1)
        classifiedCloud->points[i].label = static_cast(predictedLabels.at(i, 0)) + 1;
    }

    pcl::PCDWriter writer;

    writer.write("lable.pcd", *classifiedCloud);

    cout << "lable.pcd已完成储存,请查看" << endl;

   
        //----------------------------根据分类标签可视化-----------------------------
        boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
        pcl::visualization::PointCloudColorHandlerGenericFieldfildColor(classifiedCloud, "label");
        viewer->setBackgroundColor(0, 0, 0);
        viewer->setWindowName("点云按分类标签显示");
        viewer->addText("Point clouds are shown by label", 50, 50, 0, 1, 0, "v1_text");
        viewer->addPointCloud(classifiedCloud, fildColor, "sample cloud");
        viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");

        while (!viewer->wasStopped())
        {
            viewer->spinOnce(100);
            boost::this_thread::sleep(boost::posix_time::microseconds(100000));
        }


    return 0;
}

你可能感兴趣的:(opencv,支持向量机,分类)