matlab进行道格拉斯筛选,matlab Douglas-Peucker 道格拉斯-普克算法

function [ps,ix] = dpsimplify(p,tol)

% Recursive Douglas-Peucker Polyline Simplification, Simplify

%

% [ps,ix] = dpsimplify(p,tol)

%

% dpsimplify uses the recursive Douglas-Peucker line simplification

% algorithm to reduce the number of vertices in a piecewise linear curve

% according to a specified tolerance. The algorithm is also know as

% Iterative Endpoint Fit. It works also for polylines and polygons

% in higher dimensions.

%

% In case of nans (missing vertex coordinates) dpsimplify assumes that

% nans separate polylines. As such, dpsimplify treats each line

% separately.

%

% For additional information on the algorithm follow this link

% http://www.doczj.com/doc/7c10ce3153d380eb6294dd88d0d233d4b14e3fcf.html/wiki/Ramer-Douglas-Peucker_algorithm

%

% Input arguments

%

% p polyline n*d matrix with n vertices in d

% dimensions.

% tol tolerance (maximal euclidean distance allowed

% between the new line and a vertex)

%

% Output arguments

%

% ps simplified line

% ix linear index of the vertices retained in p (ps = p(ix))

%

% Examples

%

% 1. Simplify line

%

% tol = 1;

% x = 1:0.1:8*pi;

% y = sin(x) + randn(size(x))*0.1;

% p = [x' y'];

% ps = dpsimplify(p,tol);

%

% plot(p(:,1),p(:,2),'k')

% hold on

% plot(ps(:,1),ps(:,2),'r','LineWidth',2);

% legend('original polyline','simplified')

%

% 2. Reduce polyline so that only knickpoints remain by

% choosing a very low tolerance

%

% p = [(1:10)' [1 2 3 2 4 6 7 8 5 2]'];

% p2 = dpsimplify(p,eps);

% plot(p(:,1),p(:,2),'k+--')

% hold on

% plot(p2(:,1),p2(:,2),'ro','MarkerSize',10);

% legend('original line','knickpoints')

%

% 3. Simplify a 3d-curve

%

% x = sin(1:0.01:20)';

% y = cos(1:0.01:20)';

% z = x.*y.*(1:0.01:20)';

% ps = dpsimplify([x y z],0.1);

% plot3(x,y,z);

% hold on

% plot3(ps(:,1),ps(:,2),ps(:,3),'k*-');

%

%

%

% Author: Wolfgang Schwanghart, 13. July, 2010.

% w.schwanghart[at]unibas.ch

if nargin == 0

help dpsimplify

return

end

% error(nargchk(2, 2, nargin))

narginchk(2, 2);

% error checking

if ~isscalar(tol) || tol<0;

error('tol must be a positive scalar')

end

% nr of dimensions

nrvertices = size(p,1);

dims = size(p,2);

% anonymous function for starting point and end point comparision

% using a relative tolerance test

compare = @(a,b) abs(a-b)/max(abs(a),abs(b)) <= eps;

% what happens, when there are NaNs?

% NaNs divide polylines.

Inan = any(isnan(p),2);

% any NaN at all?

Inanp = any(Inan);

% if there is only one vertex

if nrvertices == 1 || isempty(p);

ps = p;

ix = 1;

% if there are two

elseif nrvertices == 2 && ~Inanp;

% when the line has no vertices (except end and start point of the

% line) check if the distance between both is less than the tolerance.

% If so, return the center.

if dims == 2;

d =

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值