最近工作需要对提取的矢量图斑进行平滑(shp格式),所以就对这里进行了一点小小的研究,主要是对Chaikin-curve算法进行改进,其实还有很多优秀的算法可以拿来用,迫于时间,就没有继续深入,
这篇博客,权当是抛砖引玉,希望真正搞平滑算法的”同志们“,能够展示出自己真正的优秀算法。我们知道,当多边形数量为几百个,几千个,可能cpu串行和并行计算效率差距不大,但是当数量突破万个,几十万个,几百万个呢?
串行明显就不行了,所以我这里探索了并行矢量平滑算法。。。我在后面开源了代码,如果各位对代码有疑问或者需要理解的,qq:1044625113,备注:矢量并行处理
我计算了12万个多边形, 计算效率对比,如下表所示:
| 计算模式 | 计算时间(秒) |
| 串行 | 70 |
| 并行(四核) | 20 |
节省了整整三倍啊,兄弟们,这个很爽啊!

图 原始矢量图斑

图 平滑后矢量图斑
下面贴上矢量平滑的主函数代码:
% chaikin-curve ????????????????
% written by Mr zhipan Wang,Email:1044625113@qq.com,BeiJing,2019-10-21
% refer:https://www.cnblogs.com/hongru/archive/2011/10/27/2226946.html
clear
tic
%% read shape file
ShpFileName = '????????????.shp';
[shp,attribute] = shaperead(ShpFileName);
Scale = 3; % ??????????????????
Iter = 6; % ????????????????
% figure,mapshow(shp),title('original shapefile!')
%% curve smooth
numPolygon = length(shp);
STR = 'struct(''Geometry'',values ,''X'', values,''Y'', values,''ID'',values)';
values = cell(numPolygon, 1); % ????????帳??????,??????????????????????????????????,????????????????dbf??????,??????????????????????
newSHP = eval(STR);
parfor i = 1:numPolygon % ??????????????
Latitude_arrary = shp(i).Y;
Longitude_array = shp(i).X;
[Smooth_Lati, Smooth_Longi] = ChaikinCurve_Smooth(Latitude_arrary, Longitude_array, Scale, Iter);
newSHP(i).X = Smooth_Longi;
newSHP(i).Y = Smooth_Lati;
newSHP(i).ID = i-1;
newSHP(i).Geometry = 'Polygon';
fprintf(['??????????',num2str(numPolygon),'??????????????, ','????', num2str(i), '??????????????????????????...\n']);
end
clear shp
% figure,mapshow(newSHP),title('smooth shpfile!') % ????????????ν??????????????????????,????????????????????????????????
%% export shape file
shapewrite(newSHP,'smoothSHP.shp');
toc
贴上实现的函数代码:
function [Smooth_Lati, Smooth_Longi] = ChaikinCurve_Smooth(Latitude_arrary, Longitude_array, Scale, Iter)
% CK 曲线平滑算法的核心实现, Email:1044625113@qq.com,BeiJing,2019-10-21!
% Latitude_arrary: 纬度数组
% Longitude_array: 经度数组
% Scale: 尺度参数, 正整数
% Iter: 迭代次数,一般四次即可!
if length(Latitude_arrary) ~= length(Longitude_array)
fprintf('数组大小不一致...\n');
return;
end
if Scale < 1
fprintf('尺度参数应该大于1...\n');
return;
end
% 迭代实现
for i = 1:Iter
[Latitude_arrary, Longitude_array] = addPoint(Latitude_arrary, Longitude_array, Scale);
end
Smooth_Lati = Latitude_arrary;
Smooth_Longi = Longitude_array;
end
总的来说,只需要设置迭代次数就可以了,平滑度参数默认3即可,迭代次数设置成3-6次基本上够用了,先写到这里吧