c道格拉斯-普克算法 [1]  (Douglas–Peucker algorithm,亦称为拉默-道格拉斯-普克算法、迭代适应点算法、分裂与合并算法)是将曲线近似表示为一系列点,并减少点的数量的一种算法。它的优点是具有平移和旋转不变性,给定曲线与阈值后,抽样结果一定。

算法的基本思路是:对每一条曲线的首末点虚连一条直线,求所有点与直线的距离,并找出最大距离值dmax ,用dmax与限差D相比:若dmax <D,这条曲线上的中间点全部舍去;若dmax ≥D,保留dmax 对应的坐标点,并以该点为界,把曲线分为两部分,对这两部分重复使用该方法。

详细步骤:

(1) 在曲线首尾两点间虚连一条直线,求出其余各点到该直线的距离,如右图(1)。

(2)选其最大者与阈值相比较,若大于阈值,则离该直线距离最大的点保留,否则将直线两端点间各点全部舍去,如右图(2),第4点保留。

(3)依据所保留的点,将已知曲线分成两部分处理,重复第1、2步操作,迭代操作,即仍选距离最大者与阈值比较,依次取舍,直到无点可舍去,最后得到满足给定精度限差的曲线点坐标,如图(3)、(4)依次保留第6点、第7点,舍去其他点,即完成线的化简。

matlab 代码实现:

function curve = dp(pnts)
%dp点抽稀算法
clc;
close all;
clear all;x =  1:0.01:  2;
num = size(x,2);pnts = 2*sin(8*pi*x) ; % + rand(1,num)*0.8;
pnts = x .* sin(8*pi*x) + rand(1,num)*0.5;
figure; plot( x, pnts,'-.'); title('pnts');head = [x(1), pnts(1)] ;
tail = [x(end), pnts(end)] ;%距离阈值
dist_th = 0.1; hold on;
max_dist = 0;
max_dist_i = 0;
%把起始点和终止点加进去
curve = [head; tail];
pnt_index = [1, num];while(1)         %目前抽稀后的曲线上的点数curve_num = size(curve,1);%标记是否添加了新点,若有,表明还要继续处理add_new_pnt = 0; %  对区间头尾连线,然后计算各点到这条直线的距离 for nx = 1:curve_num-1cur_pnt = curve(nx,:);%下一个抽稀了的曲线上的点next_pnt = curve(nx+1,:);pnt_d = next_pnt - cur_pnt ;th = atan2(pnt_d(2), pnt_d(1));angle = th*180/pi;%直线方程% y = kx + bk = tan(th);b = cur_pnt(2) - k*cur_pnt(1);k2 = k*k;deno = sqrt(1 + k *k) ;max_dist = 0;pnt_index(nx);pnt_index(nx+1);%对这一区间的点计算到直线的距离for i= pnt_index(nx) : pnt_index(nx+1)dist = abs(pnts(i) - k*x(i) - b)/deno ;if(dist> max_dist)      max_dist = dist;max_dist_i = i;        end end max_dist;max_dist_i; far_pnt = [x(max_dist_i), pnts(max_dist_i)];%最远的点加进去if(max_dist > dist_th)curve = [curve(1:nx,:); far_pnt; curve(nx+1:end,:)]; pnt_index = [pnt_index(1:nx), max_dist_i, pnt_index(nx+1:end)];%标记添加了新点,可能还要继续处理add_new_pnt = 1;        end end close all ;figure; plot( x, pnts,'-.'); title('pnts');hold on;plot(curve(:,1), curve(:,2), '-g*');drawnow;%如果各点到直线距离都没有超过阈值则退出%处理完毕了,ok了,退出if(0 == add_new_pnt)break;end
end 

c++ 代码实现:

#include
#include
#include "DouglasPeucker.h"
using namespace std;
void readin(vector &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 &Points,inttolerance,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, const char *argv[])
{
if(argc==4)
{vector Points;readin(Points,argv[1]);int threshold = atoi(argv[3]); DouglasPeuckerAlgorithm(Points,threshold,argv[2]);DumpOut1();
}
else
{DumpOut2();
}
return 0;
}//////#ifndef DOUGLAGPEUCKER#include
#include
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:
vector PointStruct;
vector myTag; // 标记特征点的一个bool数组
vector PointNum;//离散化得到的点号
DouglasPeucker(void){};
DouglasPeucker(vector &Points,int tolerance);
~DouglasPeucker(){};void WriteData(const char *filename);
private:
void DouglasPeuckerReduction(int firstPoint, int lastPoint, doubletolerance);
double PerpendicularDistance(MyPointStruct &point1, MyPointStruct&point2, MyPointStruct &point3);
MyPointStruct myConvert(int index);
};#define DOUGLAGPEUCKER
#endif////////#include "DouglasPeucker.h"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];
}void DouglasPeucker::DouglasPeuckerReduction(int firstPoint, intlastPoint, 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);
}
}DouglasPeucker::DouglasPeucker(vector &Points,inttolerance)
{
PointStruct = Points;
int totalPointNum =Points.size();myTag.resize(totalPointNum,0);DouglasPeuckerReduction(0, totalPointNum-1, tolerance);for (int index = 0; index
{if(myTag[index])PointNum.push_back(index);
}
}
void DouglasPeucker::WriteData(const char *filename)
{
FILE *fp = fopen(filename,"w");
int pSize = PointNum.size();
for(int index=0;index
{fprintf(fp,"%lf\t%lf\n",PointStruct[PointNum[index]].X,PointStruct[PointNum[index]].Y);
}
}

matlab Douglas-Peucker 道格拉斯-普克算法相关推荐

  1. 道格拉斯-普克算法(Douglas–Peucker algorithm)

    道格拉斯-普克算法(Douglas–Peucker algorithm,亦称为拉默-道格拉斯-普克算法.迭代适应点算法.分裂与合并算法)是将曲线近似表示为一系列点,并减少点的数量的一种算法.该算法的原 ...

  2. c++多边形扫描线填充算法_基于MATLAB的道格拉斯普克算法递归实现

    道格拉斯普克算法 (道格拉斯-普克)Douglas-Peukcer算法由D.Douglas和T.Peueker于1973年提出,是线状要素抽稀的经典算法.用它处理大量冗余的几何数据点,既可以达到数据量 ...

  3. 道格拉斯算法 matlab,OpenCV 学习笔记03 凸包convexHull、道格拉斯-普克算法Douglas-Peucker algorithm、approxPloyDP 函数...

    凸形状内部的任意两点的连线都应该在形状里面. 1 道格拉斯-普克算法 Douglas-Peucker algorithm 这个算法在其他文章中讲述的非常详细,此处就详细撰述. 下图是引用维基百科的.ε ...

  4. 170316.道格拉斯-普克算法

    道格拉斯-普克算法 道格拉斯-普克算法 (Douglas–Peucker algorithm,亦称为拉默-道格拉斯-普克算法.迭代适应点算法.分裂与合并算法)是乌尔斯·拉默(Urs Ramer)于19 ...

  5. opencv 凸包convexHull、道格拉斯-普克算法Douglas-Peucker algorithm、approxPloyDP 函数

    凸包convexHull.道格拉斯-普克算法Douglas-Peucker algorithm.approxPloyDP 函数 道格拉斯-普克算法(Douglas–Peucker algorithm) ...

  6. OpenCV 学习笔记03 凸包convexHull、道格拉斯-普克算法Douglas-Peucker algorithm、approxPloyDP 函数...

    凸形状内部的任意两点的连线都应该在形状里面. 1 道格拉斯-普克算法 Douglas-Peucker algorithm 这个算法在其他文章中讲述的非常详细,此处就详细撰述. 下图是引用维基百科的.ε ...

  7. 道格拉斯-普克算法(经纬度或坐标点抽稀)

    起因 最近在做一个车联网项目,有一个场景是车辆定时上报当前所在经纬度等位置信息上报给平台,平台通过web页面在高德地图上展示车辆行驶路径. 由于车辆上报规则是每隔4s上报一次,一个小时也就是900个点 ...

  8. 【图像处理】道格拉斯-普克算法(曲线的折线段逼近)

    目录 一.提要 二.为什么要道格拉斯-普克算法 三.算法描述 四.代码实现 4.1 Python代码1 4.2 python代码2 五.结论 该文的应用见文章:[Halcon算子]get_contou ...

  9. C语言程序实现道格拉斯—普克算法(Douglas--Peucker)

    算法简介 道格拉斯-普克算法(Douglas-Peucker)也称,线简化算法.作用在于,删除冗余数据,减少数据的存贮量,节省存贮空间,加快后继处理的速度. 格拉斯-普克算法(Douglas-Peuc ...

最新文章

  1. 动态指定超链接参数的几种方法(Passing a JavaScript variable into href of )
  2. OPENCV中的数据结构总结
  3. mongodb转json
  4. c++类的实例化,有没有new的区别
  5. 不区分大小写的内容比较
  6. 启明智显分享| 关于ESP32-S3系列串口屏应用时配网需知
  7. React 入门学习笔记2
  8. Python 下划线
  9. Starter Kit for ASP.NET 2.0 家族又添新丁!
  10. 修改Jupyter Notebook默认工作目录
  11. ApplicationMaster启动流程
  12. Mycat安全权限配置user_配置mycat用户只读数据---MyCat分布式数据库集群架构工作笔记0031
  13. 自己写的一个简单的php快速开发框架(JPrass)
  14. 华为手机字体改简体_华为繁体字体怎么更换 华为手机字体改简体
  15. ps 如何制作一寸白底照片
  16. Spark面试题、答案
  17. python中scroll的用法_Python_关于self.cur.scroll()的使用理解
  18. 只有170字节,最小的64位Hello World程序这样写成
  19. 简单的KMeans聚类C++代码实现及解析
  20. NOIP2004初赛普及组-C++

热门文章

  1. 小米电视系统服务器升级,小米电视怎么更新系统 升级步骤图文详解
  2. 什么是FTP及FTP服务器
  3. C语言对文件的读取和写入操作代码
  4. 接口规范、容错处理规则、aph备份数据规则
  5. 咸鱼的白日梦(疯~的美丽日记)
  6. 虚拟机nat方式联网
  7. nginx的location和rewrite
  8. python中向字典循环加入键值对
  9. Glide(二)Glide的with,load,into
  10. 向用户发送短信验证码