Files
openlayers/vectortile/examples/region-growing.html
Andreas Hocevar 1c53a46d7d Update vectortiles
2015-09-13 15:36:23 +09:00

392 lines
13 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" type="text/css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="./resources/prism/prism.css" type="text/css">
<link rel="stylesheet" href="../css/ol.css" type="text/css">
<link rel="stylesheet" href="./resources/layout.css" type="text/css">
<link rel="stylesheet" href="region-growing.css">
<script src="./resources/zeroclipboard/ZeroClipboard.min.js"></script>
<title>Region Growing</title>
</head>
<body>
<header class="navbar" role="navigation">
<div class="container" id="navbar-inner-container">
<a class="navbar-brand" href="./"><img src="./resources/logo-70x70.png">&nbsp;OpenLayers 3 Examples</a>
</div>
</header>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map" style="cursor: pointer"></div>
<table class="controls">
<tr>
<td>Threshold: <span id="threshold-value"></span></td>
<td><input id="threshold" type="range" min="1" max="50" value="20"></td>
</tr>
</table>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">Region Growing</h4>
<p id="shortdesc">Grow a region from a seed pixel</p>
<div id="docs"><p>Click a region on the map. The computed region will be red.</p> <p>
This example uses a <code>ol.source.Raster</code> to generate data
based on another source. The raster source accepts any number of
input sources (tile or image based) and runs a pipeline of
operations on the input data. The return from the final
operation is used as the data for the output source.
</p> <p>
In this case, a single tiled source of imagery data is used as input.
The region is calculated in a single &quot;image&quot; operation using the &quot;seed&quot;
pixel provided by the user clicking on the map. The &quot;threshold&quot; value
determines whether a given contiguous pixel belongs to the &quot;region&quot; - the
difference between a candidate pixel&#39;s RGB values and the seed values must
be below the threshold.
</p> <p>
This example also shows how an additional function can be made available
to the operation.
</p>
</div>
<div id="tags">raster, region growing</div>
<div id="api-links">Related API documentation: <ul class="inline"><li><a href="../apidoc/ol.Map.html" title="API documentation for ol.Map">ol.Map</a></li>,<li><a href="../apidoc/ol.View.html" title="API documentation for ol.View">ol.View</a></li>,<li><a href="../apidoc/ol.layer.Image.html" title="API documentation for ol.layer.Image">ol.layer.Image</a></li>,<li><a href="../apidoc/ol.layer.Tile.html" title="API documentation for ol.layer.Tile">ol.layer.Tile</a></li>,<li><a href="../apidoc/ol.proj.html" title="API documentation for ol.proj">ol.proj</a></li>,<li><a href="../apidoc/ol.source.BingMaps.html" title="API documentation for ol.source.BingMaps">ol.source.BingMaps</a></li>,<li><a href="../apidoc/ol.source.Raster.html" title="API documentation for ol.source.Raster">ol.source.Raster</a></li></ul></div>
</div>
</div>
<div class="row-fluid">
<div id="source-controls">
<a id="copy-button"><i class="fa fa-clipboard"></i> Copy</a>
<a id="jsfiddle-button"><i class="fa fa-jsfiddle"></i> Edit</a>
</div>
<form method="POST" id="jsfiddle-form" target="_blank" action="http://jsfiddle.net/api/post/jquery/1.11.0/">
<textarea class="hidden" name="js">// NOCOMPILE
function growRegion(inputs, data) {
var image = inputs[0];
var seed = data.pixel;
var delta = parseInt(data.delta);
if (!seed) {
return image;
}
seed = seed.map(Math.round);
var width = image.width;
var height = image.height;
var inputData = image.data;
var outputData = new Uint8ClampedArray(inputData);
var seedIdx = (seed[1] * width + seed[0]) * 4;
var seedR = inputData[seedIdx];
var seedG = inputData[seedIdx + 1];
var seedB = inputData[seedIdx + 2];
var edge = [seed];
while (edge.length) {
var newedge = [];
for (var i = 0, ii = edge.length; i &lt; ii; i++) {
// As noted in the Raster source constructor, this function is provided
// using the &#x60;lib&#x60; option. Other functions will NOT be visible unless
// provided using the &#x60;lib&#x60; option.
var next = nextEdges(edge[i]);
for (var j = 0, jj = next.length; j &lt; jj; j++) {
var s = next[j][0], t = next[j][1];
if (s &gt;= 0 &amp;&amp; s &lt; width &amp;&amp; t &gt;= 0 &amp;&amp; t &lt; height) {
var ci = (t * width + s) * 4;
var cr = inputData[ci];
var cg = inputData[ci + 1];
var cb = inputData[ci + 2];
var ca = inputData[ci + 3];
// if alpha is zero, carry on
if (ca === 0) {
continue;
}
if (Math.abs(seedR - cr) &lt; delta &amp;&amp; Math.abs(seedG - cg) &lt; delta &amp;&amp;
Math.abs(seedB - cb) &lt; delta) {
outputData[ci] = 255;
outputData[ci + 1] = 0;
outputData[ci + 2] = 0;
outputData[ci + 3] = 255;
newedge.push([s, t]);
}
// mark as visited
inputData[ci + 3] = 0;
}
}
}
edge = newedge;
}
return new ImageData(outputData, width, height);
}
function next4Edges(edge) {
var x = edge[0], y = edge[1];
return [
[x + 1, y],
[x - 1, y],
[x, y + 1],
[x, y - 1]
];
}
var key = &#x27;Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3&#x27;;
var imagery = new ol.layer.Tile({
source: new ol.source.BingMaps({key: key, imagerySet: &#x27;Aerial&#x27;})
});
var raster = new ol.source.Raster({
sources: [imagery.getSource()],
operationType: &#x27;image&#x27;,
operation: growRegion,
// Functions in the &#x60;lib&#x60; object will be available to the operation run in
// the web worker.
lib: {
nextEdges: next4Edges
}
});
var rasterImage = new ol.layer.Image({
opacity: 0.7,
source: raster
});
var map = new ol.Map({
layers: [imagery, rasterImage],
target: &#x27;map&#x27;,
view: new ol.View({
center: ol.proj.fromLonLat([-119.07, 47.65]),
zoom: 11
})
});
var coordinate;
map.on(&#x27;click&#x27;, function(event) {
coordinate = event.coordinate;
raster.changed();
});
raster.on(&#x27;beforeoperations&#x27;, function(event) {
// the event.data object will be passed to operations
var data = event.data;
data.delta = thresholdControl.value;
if (coordinate) {
data.pixel = map.getPixelFromCoordinate(coordinate);
}
});
var thresholdControl = document.getElementById(&#x27;threshold&#x27;);
function updateControlValue() {
document.getElementById(&#x27;threshold-value&#x27;).innerText = thresholdControl.value;
}
updateControlValue();
thresholdControl.addEventListener(&#x27;input&#x27;, function() {
updateControlValue();
raster.changed();
});
</textarea>
<textarea class="hidden" name="css">table.controls td {
min-width: 110px;
padding: 2px 5px;
}
</textarea>
<textarea class="hidden" name="html">&lt;div class=&quot;row-fluid&quot;&gt;
&lt;div class=&quot;span12&quot;&gt;
&lt;div id=&quot;map&quot; class=&quot;map&quot; style=&quot;cursor: pointer&quot;&gt;&lt;/div&gt;
&lt;table class=&quot;controls&quot;&gt;
&lt;tr&gt;
&lt;td&gt;Threshold: &lt;span id=&quot;threshold-value&quot;&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input id=&quot;threshold&quot; type=&quot;range&quot; min=&quot;1&quot; max=&quot;50&quot; value=&quot;20&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
</textarea>
<input type="hidden" name="wrap" value="l">
<input type="hidden" name="resources" value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css,https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js,http://openlayers.org/en/v3.9.0/css/ol.css,http://openlayers.org/en/v3.9.0/build/ol.js">
</form>
<pre><code id="example-source" class="language-markup">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Region Growing&lt;/title&gt;
&lt;script src="https://code.jquery.com/jquery-1.11.2.min.js"&gt;&lt;/script&gt;
&lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"&gt;
&lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"&gt;&lt;/script&gt;
&lt;link rel="stylesheet" href="http://openlayers.org/en/v3.9.0/css/ol.css" type="text/css"&gt;
&lt;script src="http://openlayers.org/en/v3.9.0/build/ol.js"&gt;&lt;/script&gt;
&lt;style&gt;
table.controls td {
min-width: 110px;
padding: 2px 5px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="container-fluid"&gt;
&lt;div class=&quot;row-fluid&quot;&gt;
&lt;div class=&quot;span12&quot;&gt;
&lt;div id=&quot;map&quot; class=&quot;map&quot; style=&quot;cursor: pointer&quot;&gt;&lt;/div&gt;
&lt;table class=&quot;controls&quot;&gt;
&lt;tr&gt;
&lt;td&gt;Threshold: &lt;span id=&quot;threshold-value&quot;&gt;&lt;/span&gt;&lt;/td&gt;
&lt;td&gt;&lt;input id=&quot;threshold&quot; type=&quot;range&quot; min=&quot;1&quot; max=&quot;50&quot; value=&quot;20&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
// NOCOMPILE
function growRegion(inputs, data) {
var image = inputs[0];
var seed = data.pixel;
var delta = parseInt(data.delta);
if (!seed) {
return image;
}
seed = seed.map(Math.round);
var width = image.width;
var height = image.height;
var inputData = image.data;
var outputData = new Uint8ClampedArray(inputData);
var seedIdx = (seed[1] * width + seed[0]) * 4;
var seedR = inputData[seedIdx];
var seedG = inputData[seedIdx + 1];
var seedB = inputData[seedIdx + 2];
var edge = [seed];
while (edge.length) {
var newedge = [];
for (var i = 0, ii = edge.length; i &lt; ii; i++) {
// As noted in the Raster source constructor, this function is provided
// using the &#x60;lib&#x60; option. Other functions will NOT be visible unless
// provided using the &#x60;lib&#x60; option.
var next = nextEdges(edge[i]);
for (var j = 0, jj = next.length; j &lt; jj; j++) {
var s = next[j][0], t = next[j][1];
if (s &gt;= 0 &amp;&amp; s &lt; width &amp;&amp; t &gt;= 0 &amp;&amp; t &lt; height) {
var ci = (t * width + s) * 4;
var cr = inputData[ci];
var cg = inputData[ci + 1];
var cb = inputData[ci + 2];
var ca = inputData[ci + 3];
// if alpha is zero, carry on
if (ca === 0) {
continue;
}
if (Math.abs(seedR - cr) &lt; delta &amp;&amp; Math.abs(seedG - cg) &lt; delta &amp;&amp;
Math.abs(seedB - cb) &lt; delta) {
outputData[ci] = 255;
outputData[ci + 1] = 0;
outputData[ci + 2] = 0;
outputData[ci + 3] = 255;
newedge.push([s, t]);
}
// mark as visited
inputData[ci + 3] = 0;
}
}
}
edge = newedge;
}
return new ImageData(outputData, width, height);
}
function next4Edges(edge) {
var x = edge[0], y = edge[1];
return [
[x + 1, y],
[x - 1, y],
[x, y + 1],
[x, y - 1]
];
}
var key = &#x27;Ak-dzM4wZjSqTlzveKz5u0d4IQ4bRzVI309GxmkgSVr1ewS6iPSrOvOKhA-CJlm3&#x27;;
var imagery = new ol.layer.Tile({
source: new ol.source.BingMaps({key: key, imagerySet: &#x27;Aerial&#x27;})
});
var raster = new ol.source.Raster({
sources: [imagery.getSource()],
operationType: &#x27;image&#x27;,
operation: growRegion,
// Functions in the &#x60;lib&#x60; object will be available to the operation run in
// the web worker.
lib: {
nextEdges: next4Edges
}
});
var rasterImage = new ol.layer.Image({
opacity: 0.7,
source: raster
});
var map = new ol.Map({
layers: [imagery, rasterImage],
target: &#x27;map&#x27;,
view: new ol.View({
center: ol.proj.fromLonLat([-119.07, 47.65]),
zoom: 11
})
});
var coordinate;
map.on(&#x27;click&#x27;, function(event) {
coordinate = event.coordinate;
raster.changed();
});
raster.on(&#x27;beforeoperations&#x27;, function(event) {
// the event.data object will be passed to operations
var data = event.data;
data.delta = thresholdControl.value;
if (coordinate) {
data.pixel = map.getPixelFromCoordinate(coordinate);
}
});
var thresholdControl = document.getElementById(&#x27;threshold&#x27;);
function updateControlValue() {
document.getElementById(&#x27;threshold-value&#x27;).innerText = thresholdControl.value;
}
updateControlValue();
thresholdControl.addEventListener(&#x27;input&#x27;, function() {
updateControlValue();
raster.changed();
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="./resources/common.js"></script>
<script src="./resources/prism/prism.min.js"></script>
<script src="loader.js?id=region-growing"></script>
</body>
</html>