このファイル: /home/web6047/www/cgi-bin/prj/_アルゴリズム/20180811-画像の指定色域の形状を得る/canvas.html
1
<!DOCTYPE html>
2
<head>
3
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
4
<title>エッジ検出 プログラム</title>
5
<script>
6
7
console.clear();
8
console.log( "=============== script ==============" );
9
function con() { console.log( Array.prototype.join.call( arguments, " " ) ); }
10
function $( id ) { return document.getElementById( id ); }
11
var HereDocument = /\/\*\s*([^]*?)\s*\*\//;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function onloadx() {
30
31
canvasEL = $( "canvasELID" );
32
canvas = canvasEL.getContext( '2d' );
33
screenW = canvas.canvas.width;
34
screenH = canvas.canvas.height;
35
36
shape = new Array();
37
38
39
shapeSeek = -1;
40
shapesSeek = 0;
41
toggle = true;
42
43
44
45
46
image = new Image();
47
image.onload = function() {
48
49
50
canvas.clearRect( 0, 0, screenW, screenH );
51
canvas.drawImage( image, 0, 0 );
52
var ID = canvas.getImageData( 0, 0, screenW, screenH );
53
54
55
shapes = getShapes( ID, 0, 0, 0, 255, 120 );
56
57
58
draw( canvas );
59
};
60
image.src = "Sheet1.png";
61
62
63
onkeydown = function( e ) {
64
switch( e.which ) {
65
case 37: toggle = false; shapeSeek-= e.target ? 1 : 10; break;
66
case 39: toggle = false; shapeSeek+= e.target ? 1 : 10; break;
67
case 38: toggle = true; shapesSeek--; shapeSeek = 0; break;
68
case 40: toggle = true; shapesSeek++; shapeSeek = 0; break;
69
case 32: toggle = true; shapesSeek++; shapeSeek = 0; break;
70
default:
71
console.log( e.which );
72
return true;
73
}
74
75
76
if( shapesSeek > shapes.length - 1 ) shapesSeek = 0;
77
if( shapesSeek < 0 ) shapesSeek = shapes.length - 1;
78
79
if( shapeSeek >= shapes[ shapesSeek ].length ) shapeSeek = shapes[ shapesSeek ].length - 1;
80
if( shapeSeek < 0 ) shapeSeek = 0;
81
82
83
console.log( "shape: ", shapesSeek, "/", shapes.length - 1, " ", shapeSeek, "/", shapes[ shapesSeek ].length - 1 );
84
85
draw( canvas );
86
return false;
87
}
88
}
89
90
function draw( cc ) {
91
92
cc.clearRect( 0, 0, screenW, screenH );
93
cc.drawImage( image, 0, 0 );
94
95
96
var shape = shapes[ shapesSeek ];
97
for( var i = 0; i < ( toggle ? shape.length : shapeSeek + 1 ); i++ ) {
98
var dot = shape[ i ];
99
cc.fillStyle = "red";
100
cc.fillRect( dot.x - 1, dot.y - 1, 2, 2 );
101
}
102
}
103
104
function getShapes( ID, R, G, B, A, tolerance ) {
105
var shapes = new Array();
106
var checkedAreas = new Array();
107
108
109
110
var isOK = function( x, y ) {
111
var idx = ( y * ID.width + x ) * 4;
112
var dr = ID.data[ idx ];
113
var dg = ID.data[ idx + 1 ];
114
var db = ID.data[ idx + 2 ];
115
var da = ID.data[ idx + 3 ];
116
return da > A - tolerance
117
&& da < A + tolerance
118
&& dr > R - tolerance
119
&& dr < R + tolerance
120
&& dg > G - tolerance
121
&& dg < G + tolerance
122
&& db > B - tolerance
123
&& db < B + tolerance;
124
};
125
126
127
128
for( var y = 0; y < ID.height; y++ ) {
129
for( var x = 0; x < ID.width; x++ ) {
130
131
132
var ischecked = false;
133
for( var i = 0; i < checkedAreas.length; i++ ) {
134
var ca = checkedAreas[ i ];
135
if( x >= ca.sx && x <= ca.ex && y >= ca.sy && y <= ca.ey ) {
136
ischecked = true;
137
break;
138
}
139
}
140
if( ischecked ) continue;
141
142
143
144
if( isOK( x, y ) ) {
145
console.log( "start ", x, y );
146
var area = new Object();
147
shapes.push( _getShape( x, y, area, isOK ) );
148
checkedAreas.push( area );
149
150
}
151
}
152
}
153
154
155
if( 0 ) {
156
for( var i = 0; i < checkedAreas.length; i++ ) {
157
var ca = checkedAreas[ i ];
158
canvas.strokeStyle = "red";
159
canvas.strokeRect( ca.sx, ca.sy, ca.ex - ca.sx, ca.ey - ca.sy );
160
}
161
alert( "debug.. " + checkedAreas.length );
162
}
163
164
return shapes;
165
}
166
167
function _getShape( sx, sy, area, isOK ) {
168
169
170
area.sx = Infinity;
171
area.sy = Infinity;
172
area.ex = 0;
173
area.ey = 0;
174
175
var result = new Array();
176
177
var dir = -1;
178
var cx = sx;
179
var cy = sy;
180
181
182
do {
183
184
185
result.push( { x : cx, y : cy } );
186
187
188
area.sx = Math.min( area.sx, cx );
189
area.sy = Math.min( area.sy, cy );
190
area.ex = Math.max( area.ex, cx );
191
area.ey = Math.max( area.ey, cy );
192
193
194
for( var i = dir - 1; i < dir + 3; i++ ) {
195
var i2 = ( 4 + i ) % 4;
196
var tmpX = cx + ( i2 == 2 ) - ( i2 == 0 );
197
var tmpY = cy + ( i2 == 3 ) - ( i2 == 1 );
198
199
if( isOK( tmpX, tmpY ) ) {
200
dir = i2;
201
cx = tmpX;
202
cy = tmpY;
203
break;
204
}
205
}
206
207
} while( cx != sx || cy != sy );
208
209
console.log( "end.", cx, cy, dir );
210
211
return result;
212
}
213
</script>
214
<style>
215
</style>
216
</head>
217
<body onload="onloadx();" style="
218
background-color : lightgray;
219
">
220
<div style="text-align:center;">
221
Edge detection.
222
</div>
223
<canvas id="canvasELID" width="512" height="448" style="
224
display : block;
225
margin : auto;
226
background-color : white;
227
border : solid 1px black;
228
229
230
">There is no canvas.</canvas>
231
<div style="text-align:center;">
232
[space] : <a href="javascript:onkeydown({which:32});void(0);">change focus</a>
233
/ [arrow left, right] : dot <a href="javascript:onkeydown({which:39});void(0);">go</a>,
234
<a href="javascript:onkeydown({which:37});void(0);">back</a>
235
</div>
236
</body>
237
</html>