最近工做須要對提取的矢量圖斑進行平滑(shp格式),因此就對這裏進行了一點小小的研究,主要是對Chaikin-curve算法進行改進,其實還有不少優秀的算法能夠拿來用,迫於時間,就沒有繼續深刻,javascript
這篇博客,權當是拋磚引玉,但願真正搞平滑算法的」同志們「,可以展現出本身真正的優秀算法。咱們知道,當多邊形數量爲幾百個,幾千個,可能cpu串行和並行計算效率差距不大,可是當數量突破萬個,幾十萬個,幾百萬個呢?html
串行明顯就不行了,因此我這裏探索了並行矢量平滑算法。。。我在後面開源了代碼,若是各位對代碼有疑問或者須要理解的,qq:1044625113,備註:矢量並行處理java
我計算了12萬個多邊形, 計算效率對比,以下表所示:git
計算模式 | 計算時間(秒) |
串行 | 70 |
並行(四核) | 20 |
節省了整整三倍啊,兄弟們,這個很爽啊!算法
圖 原始矢量圖斑數組
圖 平滑後矢量圖斑函數
下面貼上矢量平滑的主函數代碼:htm
% 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
貼上實現的函數代碼:blog
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次基本上夠用了,先寫到這裏吧ip