解决.tiff文件转.pcd文件滤波后转回.tiff文件点的序列被打乱的问题

解决.tiff文件转.pcd文件滤波后转回.tiff文件点的序列被打乱的问题

解决思路

1、.tiff转点云.pcd格式,定义数据结构记录对应点的三轴坐标与对应的行列信息

.tiff存放点云信息,将点的三轴坐标作为Mat的三个通道的值,三个通道分别对应了点的Z、Y、X坐标

struct pointindex{
    float x;
    float y;
    float z;
    int i;
    int j;
};

std::vector<pointindex> pointList;


//tiff转点云
pcl::PointCloud<pcl::PointXYZ>::Ptr tiff2Cloud(cv::Mat src)
{
    //创建点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
    if (src.type() == CV_32FC3){
        for (int i = 0; i < src.rows; i++){
            const float *p_src = src.ptr<float>(i);

            for (int j = 0; j < src.cols; j++){
                if(p_src[3*j]!=0){
                    pcl::PointXYZ point;
                    point.x = p_src[3*j+2];
                    point.y = p_src[3*j+1];
                    point.z = p_src[3*j];
                    cloud->push_back(point);
                    pointindex point_;
                    point_.x = point.x = p_src[3*j+2];
                    point_.y = p_src[3*j+1];
                    point_.z = p_src[3*j];
                    point_.i = i;
                    point_.j = j;
                    pointList.push_back(point_);
                }
            }
        }
    }
    return cloud;
}
2、点云离群滤波

点云的可视化尽量采用pcl::visualization::PCLVisualizer::Ptr这个类

//点云可视化
void showCloud(std::string windowname,pcl::PointCloud<pcl::PointXYZ>::Ptr cloud){
    pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer (windowname));
    viewer->setBackgroundColor (0.5, 0.5, 0.5);  //设置背景
    viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");  //显示点云
    viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");  //设置点尺寸
    viewer->addCoordinateSystem (100.0);  //设置坐标轴尺寸
//    while (!viewer->wasStopped ())
//    {
//      viewer->spinOnce (100);
//      boost::this_thread::sleep (boost::posix_time::microseconds (100000));
//    }
    cout<<"Point couting in "<<windowname<<": "<<cloud->size()<<endl;
}


//离群滤波
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr remove (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
    sor.setInputCloud (cloud);
    sor.setMeanK (50);
    sor.setStddevMulThresh (1.0);
    sor.filter(*cloud_filtered);
    showCloud("filtered",cloud_filtered);
    sor.setNegative(true);
    sor.filter(*remove);
3、拿离群点的X、Y、Z三轴坐标与前面记录的作比对,找出离群点在原Mat中对应的行列信息,复原离群点在原图中的情况
//点云转tiff
cv::Mat cloud2Tiff(std::vector<pointindex> list,int width,int height){
    cv::Mat output = cv::Mat::zeros(cv::Size(height,width) ,CV_32FC3);
    for(auto&p:list){
        output.ptr<float>(p.i)[3*p.j+2] = p.x;
        output.ptr<float>(p.i)[3*p.j+1] = p.y;
        output.ptr<float>(p.i)[3*p.j] = p.z;
    }
    return output;
}


std::vector<pointindex> filteredpoints;
    for(int num = 0;num<remove->points.size();num++){
        for (int index = 0;index<pointList.size();index++){
            if (std::abs(remove->points[num].x - pointList[index].x) < 0.000001
                && std::abs(remove->points[num].y - pointList[index].y) < 0.000001
                && std::abs(remove->points[num].z - pointList[index].z) < 0.000001){
                filteredpoints.push_back(pointList[index]);
            }
        }
    }
    //离群点云转图
    cv::Mat src = cloud2Tiff(filteredpoints,img.rows,img.cols);
4、离群点Mat与原Mat通道相减
//通道相减
cv::Mat channelSubtract(cv::Mat img,cv::Mat src){
    if (src.rows==img.rows && src.cols==img.cols){
        for(int i=0;i<src.rows;i++){
            for(int j=0;j<src.cols;j++){
                src.ptr<float>(i)[3*j] = img.ptr<float>(i)[3*j] - src.ptr<float>(i)[3*j];
                src.ptr<float>(i)[3*j+1] = img.ptr<float>(i)[3*j+1] - src.ptr<float>(i)[3*j+1];
                src.ptr<float>(i)[3*j+2] = img.ptr<float>(i)[3*j+2] - src.ptr<float>(i)[3*j+2];
            }
        }
    }
    return src;
}

//离群点云图与原图通道相减
    src = channelSubtract(img,src);
效果

请添加图片描述

请添加图片描述

完整代码
#include <QCoreApplication>
#include<opencv2/opencv.hpp>
#include <pcl/visualization/pcl_visualizer.h>
#include<iostream>//标准C++库中的输入输出类相关头文件。
#include<pcl/io/io.h>
#include<pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include<pcl/io/ply_io.h>
#include<pcl/point_types.h> //PCL中支持的点类型头文件。
#include <pcl/filters/statistical_outlier_removal.h>
#include <string.h>


struct pointindex{
    float x;
    float y;
    float z;
    int i;
    int j;
};

std::vector<pointindex> pointList;


//tiff转点云
pcl::PointCloud<pcl::PointXYZ>::Ptr tiff2Cloud(cv::Mat src)
{
    //创建点云
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
    if (src.type() == CV_32FC3){
        for (int i = 0; i < src.rows; i++){
            const float *p_src = src.ptr<float>(i);

            for (int j = 0; j < src.cols; j++){
                if(p_src[3*j+2]>0){
                    pcl::PointXYZ point;
                    point.x = p_src[3*j];
                    point.y = p_src[3*j+1];
                    point.z = p_src[3*j+2];
                    cloud->push_back(point);
                    pointindex point_;
                    point_.x = point.x = p_src[3*j];
                    point_.y = p_src[3*j+1];
                    point_.z = p_src[3*j+2];
                    point_.i = i;
                    point_.j = j;
                    pointList.push_back(point_);
                }
            }
        }
    }
    return cloud;
}


//点云转tiff
cv::Mat cloud2Tiff(std::vector<pointindex> list,int width,int height){
    cv::Mat output = cv::Mat::zeros(cv::Size(height,width) ,CV_32FC3);
    for(auto&p:list){
        output.ptr<float>(p.i)[3*p.j] = p.x;
        output.ptr<float>(p.i)[3*p.j+1] = p.y;
        output.ptr<float>(p.i)[3*p.j+2] = p.z;
    }
    return output;
}


//点云可视化
void showCloud(std::string windowname,pcl::PointCloud<pcl::PointXYZ>::Ptr cloud){
    pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer (windowname));
    viewer->setBackgroundColor (0.5, 0.5, 0.5);  //设置背景
    viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");  //显示点云
    viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");  //设置点尺寸
    viewer->addCoordinateSystem (100.0);  //设置坐标轴尺寸
//    while (!viewer->wasStopped ())
//    {
//      viewer->spinOnce (100);
//      boost::this_thread::sleep (boost::posix_time::microseconds (100000));
//    }
    cout<<"Point couting in "<<windowname<<": "<<cloud->size()<<endl;
}


//通道相减
cv::Mat channelSubtract(cv::Mat img,cv::Mat src){
    if (src.rows==img.rows && src.cols==img.cols){
        for(int i=0;i<src.rows;i++){
            for(int j=0;j<src.cols;j++){
                src.ptr<float>(i)[3*j] = img.ptr<float>(i)[3*j] - src.ptr<float>(i)[3*j];
                src.ptr<float>(i)[3*j+1] = img.ptr<float>(i)[3*j+1] - src.ptr<float>(i)[3*j+1];
                src.ptr<float>(i)[3*j+2] = img.ptr<float>(i)[3*j+2] - src.ptr<float>(i)[3*j+2];
            }
        }
    }
    return src;
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //tiff转pcd
    cv::Mat img = cv::imread("D:\\Qt_Project\\model123.tiff",cv::IMREAD_UNCHANGED|cv::IMREAD_ANYCOLOR|cv::IMREAD_ANYDEPTH);

    cv::imshow("before filter",img);

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
    //图转点云
    cloud = tiff2Cloud(img);

    //显示点云
    showCloud("before filter",cloud);

    //离群滤波
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::PointCloud<pcl::PointXYZ>::Ptr remove (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
    sor.setInputCloud (cloud);
    sor.setMeanK (50);
    sor.setStddevMulThresh (1.0);
    sor.filter(*cloud_filtered);
    showCloud("filtered",cloud_filtered);
    sor.setNegative(true);
    sor.filter(*remove);


    std::vector<pointindex> filteredpoints;
    for(int num = 0;num<remove->points.size();num++){
        for (int index = 0;index<pointList.size();index++){
            if (std::abs(remove->points[num].x - pointList[index].x) < 0.000001
                && std::abs(remove->points[num].y - pointList[index].y) < 0.000001
                && std::abs(remove->points[num].z - pointList[index].z) < 0.000001){
                filteredpoints.push_back(pointList[index]);
            }
        }
    }
    //离群点云转图
    cv::Mat src = cloud2Tiff(filteredpoints,img.rows,img.cols);
    //离群点云图与原图通道相减
    src = channelSubtract(img,src);
    cv::imshow("filtered",src);

    return a.exec();
}

这篇博客解决的问题是如何将.tiff文件转.pcd文件滤波后再转回.tiff文件同时保持像素的一致,下方链接中的源码仅解决了将.tiff文件转.pcd文件这个问题,注意甄别。
tiff转PCL
里面有完整的tiff转pcl代码以及用到的数据集

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 你可以使用Python中的`laspy`库来将`.las`文件换为`.pcd`文件。首先,你需要安装`laspy`库,可以使用以下命令: ``` pip install laspy ``` 然后,你可以使用以下代码将`.las`文件换为`.pcd`文件: ```python import laspy import numpy as np # 读入las文件 inFile = laspy.file.File("input_file.las", mode="r") # 获取点云信息 point_format = inFile.point_format point_records = inFile.points # 将点云信息换为numpy数组 x = point_records['X'] y = point_records['Y'] z = point_records['Z'] r = point_records['red'] g = point_records['green'] b = point_records['blue'] # 将xyz和rgb组合为一个numpy数组 cloud = np.column_stack((x, y, z, r, g, b)) # 保存为pcd文件 np.savetxt("output_file.pcd", cloud, delimiter=" ", header="VERSION .7\nFIELDS x y z rgb\nSIZE 4 4 4 4\nTYPE F F F U\nCOUNT 1 1 1 1\nDATA ascii") # 关闭las文件 inFile.close() ``` 这将读取`input_file.las`文件,将其换为包含xyz和rgb信息的numpy数组,并将其保存为`output_file.pcd`文件。 ### 回答2: Python是一种强大的编程语言,可以用于各种数据处理和换任务。要将.las文件换为.pcd文件,可以使用Python中的laspy和pypcd库。 首先,我们需要安装这两个库。可以使用以下命令来安装它们: ``` pip install laspy pypcd ``` 安装完成后,我们可以开始编写Python代码来进行换。 ```python import laspy from pypcd import pypcd # 打开.las文件 in_file = laspy.file.File("input.las", mode="r") # 创建一个新的.pcd文件对象 out_file = pypcd.PointCloud() # 从.las文件中获取点云数据 points = in_file.points # 设置.pcd文件点云数据 out_file.points = points # 设置点云属性(可选) # 例如,如果.las文件包含RGB颜色信息,可以将其移到.pcd文件中 if in_file.red is not None and in_file.green is not None and in_file.blue is not None: colors = [(r, g, b) for r, g, b in zip(in_file.red, in_file.green, in_file.blue)] out_file.pc_data["rgb"] = colors # 保存.pcd文件 out_file.save("output.pcd") ``` 在上面的代码中,首先使用laspy库打开输入的.las文件。然后,使用pypcd库创建一个新的.pcd文件对象。接下来,将从.las文件中获取的点云数据设置为.pcd文件对象的点云数据。如果.las文件中包含了RGB颜色信息,还可以将其移至.pcd文件。最后,使用save()方法将.pcd文件保存到输出文件中。 在以上的代码示例中,我使用的是laspy和pypcd库来进行.las文件到.pcd文件换。你也可以使用其他的Python库来完成这个换任务,具体实现方式可能会有所不同。 ### 回答3: 将.LAS(激光扫描点云文件换为.PCD(点云数据)文件是一个常见的任务。Python提供了许多库和工具,可以帮助我们实现这个换过程。 首先,我们需要安装一些必要的库。使用`pip`命令安装Python的点云库`pyntcloud`和LAS文件处理库`laspy`。 ``` pip install pyntcloud pip install laspy ``` 接下来,我们可以编写一个Python脚本来完成.LAS文件换为.PCD文件的任务。以下是一个简单的示例: ```python import laspy from pyntcloud import PyntCloud # 读取.LAS文件 las_file = laspy.file.File('input.las', mode='r') # 获取点云数据 points = las_file.points # 创建点云对象 cloud = PyntCloud(pd.DataFrame(points)) # 将点云保存为.PCD文件 cloud.to_file('output.pcd') ``` 在这个脚本中,我们首先使用`laspy`库打开.LAS文件并获取点云数据。然后,我们使用`pyntcloud`库创建一个点云对象,并将点云保存为.PCD文件。 在运行脚本之前,将`input.las`替换为你要换的.LAS文件路径,将`output.pcd`替换为输出.PCD文件的路径。 这就是用Python将.LAS文件换为.PCD文件的简单过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mai0026

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值