Region Growing
Runnable OpenLayers example: Region Growing. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:区域生长
要点:以点击处为种子,按数值相近的邻域逐圈扩散,结果叠加在影像上
依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
替代:官方对多光谱影像做区域生长,这里用页面内生成的数值场做同构算法
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>区域生长</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>容差 <b id="tolv">0.06</b></span>
<input type="range" id="tol" min="1" max="20" value="6" style="width:140px">
<span>区域格数 <b id="cnt">0</b></span>
<span>点击影像选种子点</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (!window.ol) {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
return;
}
var R = 6378137;
var DEG = 180 / Math.PI;
var LON0 = 115.6, LON1 = 117.2, LAT0 = 39.2, LAT1 = 40.6;
var GW = 220, GH = 180;
var grid = new Float32Array(GW * GH);
var mask = new Uint8Array(GW * GH);
function valueAt(lon, lat) {
return 0.5 + 0.32 * Math.sin(lon * 2.1) * Math.cos(lat * 1.9)
+ 0.12 * Math.sin(lon * 6.3 + 0.8) * Math.cos(lat * 5.1);
}
for (var gy = 0; gy < GH; gy++) {
for (var gx = 0; gx < GW; gx++) {
var lon = LON0 + (gx + 0.5) / GW * (LON1 - LON0);
var lat = LAT0 + (gy + 0.5) / GH * (LAT1 - LAT0);
grid[gy * GW + gx] = valueAt(lon, lat);
}
}
var tolerance = 0.06;
var canvasFunction = function (extent, resolution, pixelRatio, size) {
var w = Math.max(1, Math.round(size[0]));
var h = Math.max(1, Math.round(size[1]));
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext('2d');
var img = ctx.createImageData(w, h);
var sx = (extent[2] - extent[0]) / w;
var sy = (extent[3] - extent[1]) / h;
for (var y = 0; y < h; y++) {
var my = extent[3] - (y + 0.5) * sy;
var lat = (2 * Math.atan(Math.exp(my / R)) - Math.PI / 2) * DEG;
var fy = (lat - LAT0) / (LAT1 - LAT0);
for (var x = 0; x < w; x++) {
var lon = (extent[0] + (x + 0.5) * sx) / R * DEG;
var fx = (lon - LON0) / (LON1 - LON0);
var i = (y * w + x) * 4;
if (fx < 0 || fx >= 1 || fy < 0 || fy >= 1) {
img.data[i + 3] = 0;
continue;
}
var gxi = Math.floor(fx * GW);
var gyi = Math.floor(fy * GH);
var v = grid[gyi * GW + gxi];
var isMask = mask[gyi * GW + gxi];
img.data[i] = isMask ? 226 : 60 + v * 120;
img.data[i + 1] = isMask ? 78 : 130 + v * 90;
img.data[i + 2] = isMask ? 60 : 90 + v * 70;
img.data[i + 3] = 220;
}
}
ctx.putImageData(img, 0, 0);
return canvas;
};
var source = new ol.source.ImageCanvas({ canvasFunction: canvasFunction });
var layer = new ol.layer.Image({ source: source, opacity: 0.85 });
var map = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }), layer],
view: new ol.View({
center: ol.proj.fromLonLat([(LON0 + LON1) / 2, (LAT0 + LAT1) / 2]),
zoom: 8
}),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
/* 4 邻域广度优先,值差不超过容差才并入 */
function grow(startX, startY) {
mask.fill(0);
var seed = grid[startY * GW + startX];
var queue = [startY * GW + startX];
var seen = new Uint8Array(GW * GH);
seen[startY * GW + startX] = 1;
var total = 0;
while (queue.length) {
var idx = queue.pop();
mask[idx] = 1;
total++;
var cx = idx % GW;
var cy = (idx - cx) / GW;
var nb = [[cx - 1, cy], [cx + 1, cy], [cx, cy - 1], [cx, cy + 1]];
for (var k = 0; k < 4; k++) {
var nx = nb[k][0], ny = nb[k][1];
if (nx < 0 || ny < 0 || nx >= GW || ny >= GH) continue;
var nIdx = ny * GW + nx;
if (seen[nIdx]) continue;
seen[nIdx] = 1;
if (Math.abs(grid[nIdx] - seed) <= tolerance) queue.push(nIdx);
}
}
document.getElementById('cnt').textContent = total;
source.changed();
}
map.on('singleclick', function (evt) {
var ll = ol.proj.toLonLat(evt.coordinate);
var fx = (ll[0] - LON0) / (LON1 - LON0);
var fy = (ll[1] - LAT0) / (LAT1 - LAT0);
if (fx < 0 || fx >= 1 || fy < 0 || fy >= 1) return;
grow(Math.floor(fx * GW), Math.floor(fy * GH));
});
document.getElementById('tol').addEventListener('input', function () {
tolerance = Number(this.value) / 100;
document.getElementById('tolv').textContent = tolerance.toFixed(2);
mask.fill(0);
document.getElementById('cnt').textContent = 0;
source.changed();
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.