《PCL点云库学习&VS2010(X64)》Part 45 点云压缩算法—扫描线(DouglasPeuckerAlgorithm)
道格拉斯-普克算法主要应用有点云滤波、点云压缩、点云分割、轮廓线提取等,还可用于曲线拟合、曲线平滑、轨迹线压缩等。前期在做滤波算法,查阅论文时发现这个算法的介绍,出于好奇就在网上搜了一下,资源蛮多,找到了一个与点云相关的算法,将其稍微修改了下,贴出来供大家参考。
1、main.cpp:
#include <vector>
#include <iostream>
#include <string>
#include <stdio.h>
#include "DouglasPeucker.h"
using namespace std;
void readin(vector<MyPointStruct> &Points, const char * filename)
{
MyPointStruct SinglePoint;
FILE *fp = fopen(filename, "r");
while (fscanf(fp, "%lf%lf", &SinglePoint.X, &SinglePoint.Y) != EOF)
{
Points.push_back(SinglePoint);
}
}
void DouglasPeuckerAlgorithm(vector<MyPointStruct> &Points, int &tolerance, const char*filename)
{
DouglasPeucker Instance(Points, tolerance);
Instance.WriteData(filename);
}
void DumpOut1()
{
printf("done!\n");
}
void DumpOut2()
{
printf("need 3 command line parameter:"
"\n[0]executable file name;"
"\n[1] file name of the input data;"
"\n[2] file name of the output data; "
"\n[3] threshold.\n");
}
int main(int argc, char** argv)
{
if (argc == 4)
{
vector<MyPointStruct> Points;
readin(Points, argv[1]);//argv[1]-第二个参数为文件名
int threshold = atoi(argv[3]);//argv[3]-第四个参数为距离阈值
DouglasPeuckerAlgorithm(Points, threshold, argv[2]);//argv[2]-第三个参数为输出文件
DumpOut1();
}
else
{
DumpOut2();
}
}
2、DouglasPeucker.h:
#pragma once
#include <vector>
#include <stdio.h>
#include <iostream>
using namespace std;
struct MyPointStruct//点云结构体
{
public:
double X;
double Y;
double Z;
MyPointStruct()
{
this->X = 0;
this->Y = 0;
this->Z = 0;
};
MyPointStruct(double x, double y, double z)
{
this->X = x;
this->Y = y;
this->Z = z;
};
~MyPointStruct(){};
};
class DouglasPeucker:public MyPointStruct
{
public:
//MyPointStruct pointXYZ;
vector<MyPointStruct> PointStruct;
vector<bool> myTag; // 标记特征点的一个bool数组
vector<int> PointNum;//离散化得到的点号
public:
DouglasPeucker(void);
DouglasPeucker(vector<MyPointStruct>& Points, int tolerance);
~DouglasPeucker();
void WriteData(const char *filename);
private:
void DouglasPeuckerReduction(int firstPoint, int lastPoint, double tolerance);
double PerpendicularDistance(MyPointStruct &point1, MyPointStruct &point2, MyPointStruct &point3);
MyPointStruct& myConvert(int index);
};
3、DouglasPeucker.cpp:
#include "DouglasPeucker.h"
#include <stdio.h>
DouglasPeucker::DouglasPeucker()
{
}
DouglasPeucker::DouglasPeucker(vector<MyPointStruct>& Points, int tolerance)
{
PointStruct = Points;
int totalPointNum = Points.size();
myTag.resize(totalPointNum, 0);
DouglasPeuckerReduction(0, totalPointNum - 1, tolerance);
for (int index = 0; index<totalPointNum;index++)
{
if (myTag[index])PointNum.push_back(index);
}
}
DouglasPeucker::~DouglasPeucker()
{
}
void DouglasPeucker::WriteData(const char *filename)
{
FILE *fp = fopen(filename, "w");
int pSize = PointNum.size();
for (int index = 0; index < pSize; index++)
{
fprintf(fp, "%lf\t%lf\n", PointStruct[PointNum[index]].X, PointStruct[PointNum[index]].Y);
}
}
void DouglasPeucker::DouglasPeuckerReduction(int firstPoint, int lastPoint, double tolerance)
{
double maxDistance = 0;
int indexFarthest = 0; // 记录最大值时点元素在数组中的下标
for (int index = firstPoint; index < lastPoint; index++)
{
double distance = PerpendicularDistance(myConvert(firstPoint),
myConvert(lastPoint), myConvert(index));
if (distance > maxDistance)
{
maxDistance = distance;
indexFarthest = index;
}
}
if (maxDistance > tolerance && indexFarthest != 0)
{
myTag[indexFarthest] = true; // 记录特征点的索引信息
DouglasPeuckerReduction(firstPoint, indexFarthest, tolerance);
DouglasPeuckerReduction(indexFarthest, lastPoint, tolerance);
}
}
double DouglasPeucker::PerpendicularDistance(MyPointStruct &point1, MyPointStruct &point2, MyPointStruct &point3)
{
// 点到直线的距离公式法
double A, B, C, maxDist = 0;
A = point2.Y - point1.Y;
B = point1.X - point2.X;
C = point2.X * point1.Y - point1.X * point2.Y;
maxDist = fabs((A * point3.X + B * point3.Y + C) / sqrt(A * A + B *B));
return maxDist;
}
MyPointStruct& DouglasPeucker::myConvert(int index)
{
return PointStruct[index];
}
4、总结
程序运行后后得到三个点块,分别切开后会发现对应着数据的三个视图面,数据的存储内存确实减少了。但是二维数据的利用还没有新的思路,在二维点云上做形态学滤波或者其他聚类分割,获取点云的索引,能分割点云。
根据个人的理解,这种全局递归计算的方式对数据压缩可能有用,要想实现线扫描滤波分割,则需要对对数据切片或栅格化处理,最后将数据合并,则可实现分割,当然,中间需要加入一些辅助的条件。