-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample.html
167 lines (148 loc) · 9.34 KB
/
sample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!doctype html>
<html>
<head>
<style type="text/css">
body > * {
margin: auto;
text-align: center; // it is a MUST
}
</style>
<script>
var testPoints = [[10.91079038931762,8.3894120169044477],[9.8750016454811416,9.9092509004598295],[7.8481223001749516,10.431748303674949],[8.5341229316176186,9.5590856089969289],[10.383168456491791,9.6187908570774621],[8.1106159523273558,9.7747176075388005],[10.021194678890739,9.5387796220292351],[9.3770585195414959,9.7085399085745756],[7.6701703352949329,9.6031523057064874],[10.943082872936237,11.762073488528063],[9.2473082328068106,10.902105550317518],[9.5473972897412729,11.361701761466367],[7.8333436673622323,10.363033995676176],[10.870459222776201,9.213348127988457],[8.2285133843786014,10.467911020064905],[12.482990276816189,9.4212281474469677],[6.5572296581531369,11.059353487844673],[7.2642592211498576,9.9842567369139612],[4.8017215916997493,7.5579129268206504],[6.8612486478820385,7.8370069734131782],[13.627244186857192,10.948300307681397],[13.655256501255671,9.9249837167692654],[9.6060906991873658,10.291987945224598],[12.435657157664703,8.8134392580786756],[10.072065604999597,9.1605715886878656],[8.3067030281298777,10.441164603848748],[8.772436599131483,10.845790913201405],[9.8414161582677995,9.8483072024049196],[15.111691841743767,12.489897865895333],[10.277424100451077,9.8565701101092902],[10.134807602975544,8.8927749442282966],[8.4265860927835678,11.300233449798775],[9.191199876775725,9.9898699491718599],[5.933268578018577,10.21740003776546],[9.6660554561100884,10.688149455362304],[5.7620912159378284,10.12453435639654],[5.224273745954072,9.9849255904770313],[10.268685374305592,10.316054753347371],[10.923767076101779,10.933515119668936],[8.9357996776517705,9.181397457544719],[2.9782144270032109,3.8354704347019952],[4.9174420096569609,2.6743399911354335],[3.0245572560941096,4.80750921258515],[3.0192261574192036,4.0418118812507169],[4.1315215453218732,2.5206046528572816],[0.41134584239056782,3.655696596939725],[5.2664435674092225,5.5948820412037197],[4.6235409897637085,1.3759190611881271],[5.6786434200947413,2.7579731226577313],[3.9054627121390175,2.1416250788315363],[8.0853526460883955,2.5883371297676727],[6.8520355834556366,3.6103190528792766],[4.2308466631394683,3.5633771154938372],[6.0429053248443729,2.3588868528912594],[4.2007728899791053,2.3823879459798203],[4.2840378930654133,7.0511425527350866],[3.8206408839932244,4.6073850522736173],[5.4176851106148423,3.4363391644029351],[8.2114630301315117,3.5706098853491399],[6.5430955441931129,-0.15007118536956821],[9.2172488607385894,2.4019367504963922],[6.6730381024092011,3.3076125390184363],[4.043040860929537,4.8498363880848245],[3.7041032664462255,2.2526297941030902],[4.9081622710571251,3.8703906812050439],[5.656217904432566,2.2435522745351482],[5.0917970663533181,3.5095001337758487],[6.334045597831155,3.517609974216283],[6.8205875674927334,3.871837205598184],[7.2094404370342788,2.8531108870521447],[2.0997237752780311,2.2560279923847917],[4.7202055872439743,2.6207007161418723],[6.2219865739446796,4.6651911155018073],[5.0769925338297881,2.3590399274401812],[3.2630277690380556,0.65206989910121349],[3.6392194745531983,2.0504866855077974],[7.2501132055579918,2.6331909348456235],[4.2869377402538857,0.74184103396503032],[4.4891766334584409,1.8473897844375247],[6.2234763142026619,2.226009921680304],[2.7326843843717614,4.02671123644833],[6.7041261547308046,1.2413786866869945],[6.4067309223606088,6.4308164268410692],[3.0821624446670679,3.6035317581130513],[3.7194311239776887,5.345215168143298],[6.190401932612164,6.9225942411420576],[8.1018832467977369,4.2838830625944286],[2.6667381509229711,1.2512486723265959],[5.1562537071314836,2.9578251206128776],[6.8322086640419437,3.0047411942450042],[-1.5236684825741538,6.8709391759421514],[-6.2780454543447775,5.0545207505005338],[-4.1300898667418302,3.3089677763715279],[-2.298773883120611,2.5243375533828245],[-0.18637298645602085,5.059834391378498],[-5.1840778445536886,5.3276147704699222],[-5.2606186558919212,6.3733369937801498],[-4.0679106911345349,4.5645019898631807],[-4.8563984442990948,3.9437116901867326],[-5.169024045849862,7.1996507954789211],[-2.8187170159076471,6.7754752644632017],[-3.013197129388995,5.3073726666286722],[-1.8402582226879556,2.4730162158653011],[-3.8060164949652995,3.0993836417365723],[-1.3538731975579088,4.6000878698600136],[-5.4228296068901809,5.5406320643563971],[-3.5718995489738825,6.3905298035588247],[-4.0379782733195757,4.7056809900741836],[-1.110354346282433,4.8094055369659765],[-3.8378779004609926,6.0290987530782898],[-6.5503857799488543,5.5118092525106581],[-5.8163449709681689,7.8139376678657921],[-4.6268949267968766,8.9798801775265158],[-3.2307793551051933,3.2955805817816426],[-4.3335692243804544,5.5933643385669578],[-3.2828968288374156,6.5901857967844855],[-7.6468921093719153,7.527347420982772],[-6.4618228471303887,5.6294483598572249],[-6.3682164249984439,7.083861848873501],[-4.28475872927197,3.8425763273570248],[-2.2962665903547554,7.2885769990473017],[1.1012781986923459,6.5487961272094948],[-5.9279427268249059,8.6550877749077486],[-3.9546023112993969,5.7336401880655785],[-3.1608765392801774,4.2674094149250283]];
function Sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
function DrawDot(context, x, y) {
var radius = 2;
context.beginPath();
context.arc(x, y, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();
}
function DrawPoints(context, points) {
var i;
for (i=0; i<points.length; ++i) {
var point = points[i];
DrawDot(context, point[0] * 20 + 200, point[1] * 20 + 200); // map point data to suitable canvas position
}
}
function ClearCanvas(canvas, context) {
context.fillStyle = '#BBBBBB';
context.fillRect(0, 0, canvas.width, canvas.height);
}
function FillText(context, text, x, y) {
context.fillStyle = 'blue';
context.font = '20px Georgia';
context.fillText(text, x, y) ;
}
window.onload = function() {
var canvas = window.canvas = document.getElementById('test_canvas');
var context = window.context = canvas.getContext('2d');
var ms = window.ms = new MeanShift();
ClearCanvas(canvas, context);
FillText(context, 'Points: ' + testPoints.length, 20, 25);
DrawPoints(context, testPoints);
}
function doCluster() {
var ITERATION_SLEEP_TIME = 1000; // ms
var shiftPointsList = [];
var iterationCount = 0;
var benchmarkData = {
pointCount: testPoints.length,
iterationCount: 0,
duration: 0
}
var clusterButton = document.getElementById('cluster');
var kernelBandwidth = document.getElementById('kernelBandwidth').value;
var modeSelect = document.getElementById('mode');
var mode = modeSelect.options[modeSelect.selectedIndex].value;
function iterationCallback(shiftedPoints, iterationNumber) {
switch (mode) {
case 'demo':
shiftPointsList.push(shiftedPoints.slice());
break;
case 'benchmark':
benchmarkData.iterationCount = iterationNumber;
break;
default:
break;
}
}
clusterButton.disabled = true;
var startTime, stopTime;
switch (mode) {
case 'demo':
break;
case 'benchmark':
startTime = (new Date).getTime();
break;
default:
break;
}
var result = ms.cluster(testPoints, kernelBandwidth, iterationCallback);
stopTime = (new Date).getTime()
switch (mode) {
case 'demo': {
var shiftPointsIndex = 0;
for (i=0; i<shiftPointsList.length; ++i) {
Sleep(ITERATION_SLEEP_TIME * i).then((i) => {
ClearCanvas(canvas, context);
FillText(context, 'Points: ' + testPoints.length, 20, 25);
FillText(context, 'Iteration: ' + (shiftPointsIndex + 1) + '/' + shiftPointsList.length, 20, 50);
DrawPoints(context, shiftPointsList[shiftPointsIndex++]);
if (shiftPointsIndex == shiftPointsList.length) {
clusterButton.disabled = false;
}
});
}
}
break;
case 'benchmark': {
var maxClusterIndex = 0;
benchmarkData.duration = stopTime - startTime;
var i;
for (i=0; i<result.clusterIndices.length; ++i) {
var clusterIndex = result.clusterIndices[i];
if (clusterIndex > maxClusterIndex) {
maxClusterIndex = clusterIndex;
}
}
benchmarkData.clusterCount = 1 + maxClusterIndex;
clusterButton.disabled = false;
ClearCanvas(canvas, context);
DrawPoints(context, result.shiftedPoints);
FillText(context, 'Points: ' + benchmarkData.pointCount, 20, 25);
FillText(context, 'Clusters: ' + benchmarkData.clusterCount, 20, 50)
FillText(context, 'Iterations: ' + benchmarkData.iterationCount, 20, 75);
FillText(context, 'Duration: ' + benchmarkData.duration + 'ms', 20, 100);
}
break;
default:
break;
}
}
</script>
</head>
<body>
<div>
<h1>MeanShift_js</h1>
<p>based on <a href="https://github.com/mattnedrich/MeanShift_cpp">MeanShift_cpp</a>, <a href="https://github.com/mattnedrich/MeanShift_py">MeanShift_py</a> by <a href="https://github.com/mattnedrich">Matt Nedrich</a></p>
</div>
<div>
<canvas id="test_canvas" width="640" height="640"></canvas>
<div>
<label>Mode:</label>
<select id="mode">
<option selected="selected" value="demo">demo</option>
<option value="benchmark">benchmark</option>
</select>
 
<label>Kernel Bandwidth:</label>
<input id="kernelBandwidth" type="number" value="1"></input>
 
<button id="cluster" type="button" onclick="doCluster()">Cluster!</button>
</div>
</div>
<script src="./build/MeanShift.min.js"></script>
</body>
</html>