-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
508 lines (408 loc) · 22 KB
/
index.html
File metadata and controls
508 lines (408 loc) · 22 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<!DOCTYPE html>
<html lang="en">
<head>
<title>Spectral and Color Reconstruction</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
}
</style>
</head>
<script id="vs" type="x-shader/x-vertex">
uniform vec2 size;
out vec2 vUv;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
// Convert position.xy to 1.0-0.0
vUv.xy = position.xy / size + 0.5;
vUv.y = 1.0 - vUv.y; // original data is upside down
}
</script>
<script id="fs" type="x-shader/x-fragment">
precision highp float;
precision highp int;
precision highp sampler2DArray;
uniform sampler2DArray Components_texture;
in vec2 vUv;
uniform vec2 texture_size;
uniform int operation;
uniform int no_components;
uniform float eigen_vector[MAX_NO_COMPONENTS];
uniform float average;
// Min and Max pixel value for displaying components
uniform float MIN;
uniform float MAX;
// Offset correction
uniform float PCA_offset;
// Gamma Correction
uniform bool with_Gamma_Corr;
uniform float scale;
uniform float gamma;
uniform float offset;
// Color Reconstruction
uniform mat3 rgb_Matrix;
uniform vec3 light;
uniform float Luminance;
uniform bool add_gamut;
uniform bool with_linearizer;
// Wavelength Reconstruction
uniform float R_eigen_vector[MAX_NO_COMPONENTS];
uniform float G_eigen_vector[MAX_NO_COMPONENTS];
uniform float B_eigen_vector[MAX_NO_COMPONENTS];
uniform float R_average, G_average, B_average;
out vec4 outColor;
// Color Transformation Functions
float linearize_inverse(float c){
if(c <= 0.0031308) return 12.92*c;
return (1.055 * pow(c, 1.0/2.4)) - 0.055;
}
// XYZ to rgb
vec3 XYZ2rgb(vec3 XYZ){
return rgb_Matrix * XYZ;
}
void main() {
if(operation == 0){ // Displaying Components
float color = texelFetch( Components_texture, ivec3(ivec2(vUv.x * texture_size.x, vUv.y * texture_size.y), no_components), 0 ).r;
color = (color - MIN) / (MAX - MIN);
outColor = vec4(vec3(color), 1.0 );
}
else if(operation == 1){ // Spectral Reconstruction
vec3 res = vec3(0.0);
for(int i = 0; i <= no_components; i++){
vec4 color = texelFetch( Components_texture, ivec3(ivec2(vUv.x * texture_size.x, vUv.y * texture_size.y), i), 0); // Get feature map pixel
res += (color.rrr * vec3(eigen_vector[i]));
}
res -= vec3(average) + vec3(PCA_offset); // offset correction
outColor = vec4(res, 1.0);
}
else if(operation == 2){ // Color Reconstruction
vec3 res = vec3(0.0);
for(int i = 0; i <= no_components; i++){
vec4 color = texelFetch( Components_texture, ivec3(ivec2(vUv.x * texture_size.x, vUv.y * texture_size.y), i), 0 ); // Get feature map pixel
res += (color.rrr * vec3(R_eigen_vector[i], G_eigen_vector[i], B_eigen_vector[i]) * light * vec3(Luminance));
}
res -= vec3(R_average, G_average, B_average) + vec3(PCA_offset); // offset correction
if( add_gamut && res.y > 1.0) res = vec3((res.y - 1.0) * (res.y - 1.0), 0.35 * sqrt(res.y - 1.0), 0.0);
else{
res = XYZ2rgb(res); // Linear RGB value
if(with_linearizer) res = vec3(linearize_inverse(res.r), linearize_inverse(res.g), linearize_inverse(res.b)); // sRGB value
}
outColor = vec4(res, 1.0);
}
else{ // Wavelength Reconstruction
vec3 res = vec3(0.0);
for(int i = 0; i <= no_components; i++){
vec4 color = texelFetch( Components_texture, ivec3(ivec2(vUv.x * texture_size.x, vUv.y * texture_size.y), i), 0 ); // Get feature map pixel
res += (color.rrr * vec3(R_eigen_vector[i], G_eigen_vector[i], B_eigen_vector[i]));
}
res -= vec3(R_average, G_average, B_average) + vec3(PCA_offset); // offset correction
if(with_Gamma_Corr){ // Add Gamma Corection
res = vec3(scale)*pow(res, vec3(1.0 / gamma)) + vec3(offset);
}
outColor = vec4(res, 1.0);
}
}
</script>
<body>
<div id="graph"> </div>
<script type="module">
import * as THREE from 'https://cdn.skypack.dev/three@0.136.0/build/three.module.js';
import {TrackballControls} from 'https://cdn.skypack.dev/three@0.136.0/examples/jsm/controls/TrackballControls.js';
import Stats from 'https://cdn.skypack.dev/three@0.136.0/examples/jsm/libs/stats.module.js';
import { WEBGL } from 'https://cdn.skypack.dev/three@0.136.0/examples/jsm/WebGL.js';
import { GUI } from 'https://cdn.skypack.dev/three@0.136.0/examples/jsm/libs/lil-gui.module.min.js';
if ( WEBGL.isWebGL2Available() === false ) {
document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
}
let container, camera, scene, mesh, renderer, stats, controls, cross_cursor = (0, 0);
var components, eigen_vectors, average_vector, width, height;
const planeWidth = 60;
const planeHeight = 60;
const Wavelength_START = 400;
init();
animate();
// Get minimum value in array
function arrayMin(arr) {
return arr.reduce(function (p, v) {
return ( p < v ? p : v );
});
}
// Get maximum value in array
function arrayMax(arr) {
return arr.reduce(function (p, v) {
return ( p > v ? p : v );
});
}
// Get min and max values for each components
function min_max_array(components){
var min_array = [], max_array = [];
for(let b = 0; b < components.length; b++){
min_array[b] = arrayMin(components[b]);
max_array[b] = arrayMax(components[b]);
}
return [min_array, max_array];
}
// Read File
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
// Load 3D hyperspectral image
function Load_3D(components){
var data = new Float32Array( width * height * components.length);
let c = 0;
for(let b = 0; b < components.length; b++){
for ( let i = 0; i < height; i++ ) {
for ( let j = 0; j < width; j++) {
data[c++] = components[b][i * width + j];
}
}
}
return data;
}
// Read PCA Data from json files
function get_PCA_data(filename){
var data;
readTextFile(filename, function(text){
data = JSON.parse(text);
});
width = data.width;
height = data.height;
var PCA_bands = data.components.length; // Number of PCA components
var hyperspectral_data = Load_3D(data.components); // Loading Hyperspectral Images into 2D Array
var eigen_vectors = data.eigen_vector; // Loading Eigen Vector
var average_vector = data.average; // Loading Average Vector
var HSI_bands = average_vector.length; // 204 or 600 channels
var [min_array, max_array] = min_max_array(data.components); // Get min and max value of each components
// 2D Texture array is available on WebGL 2.0
const textures = new THREE.DataTexture2DArray(hyperspectral_data, width, height, PCA_bands);
textures.format = THREE.RedFormat;
textures.type = THREE.FloatType;
textures.minFilter = THREE.NearestFilter;
textures.magFilter = THREE.NearestFilter;
textures.needsUpdate = true;
return [data.components, textures, eigen_vectors, average_vector, PCA_bands, HSI_bands, min_array, max_array, data.lights];
}
// Initialize
function init() {
container = document.createElement( 'div' );
container.setAttribute('style', 'cursor:crosshair;');
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
camera.position.z = 80;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xBBBBBB );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
controls = new TrackballControls(camera, renderer.domElement);
controls.noZoom = false;
controls.noPan = false;
controls.noRotate = true;
controls.minDistance = 0.1;
controls.maxDistance = 40.0;
controls.enabled = true;
controls.addEventListener('change', render);
stats = new Stats();
container.appendChild( stats.dom );
// Model Info
const ModelInfo = {
model: "petal_016_600",
}
var textures, PCA_bands, HSI_bands, min_array, max_array, Lights;
[components, textures, eigen_vectors, average_vector, PCA_bands,
HSI_bands, min_array, max_array, Lights] = get_PCA_data(ModelInfo.model + ".json");
// Parameters
const parameters = {
// Components and Spectral Reconstruction
operation: 1,
no_components: PCA_bands, // no of components to use
band: 500, // default selected band
PCA_offset: -0.35, // PCA Offset Correction
// Color Reconstruction
light_type: 6,
luminance: 75,
add_gamut: false,
with_linearizer: false,
// Wavelength Reconstruction
band_R: 640,
band_G: 540,
band_B: 500,
// Gamma Correction
scale: 1.0,
gamma: 1.0,
offset: -0.08,
with_Gamma_Corr: true,
};
// Illuminants Dictionary
const LightType = { "A": 0, "B": 1, "C": 2, "E": 3,
"D50": 4, "D55": 5, "D65": 6, "D75": 7, "D95": 8,
"F2": 9, "F6": 10, "F7": 11, "F8": 12, "F10": 13, "F11": 14, "F12": 15
};
// Create Material
var rgb_Matrix = new THREE.Matrix3(); // XYZ to RGB matrix
rgb_Matrix.set(3.2404542, -1.5371385, -0.4985314,
-0.9692660, 1.8760108, 0.0415560,
0.0556434, -0.2040259, 1.0572252);
const material = new THREE.ShaderMaterial({
uniforms: {
operation:{value: parameters.operation},
Components_texture: {value: textures},
texture_size: {value: new THREE.Vector2(width, height)},
size: {value: new THREE.Vector2(planeWidth, planeHeight)},
// Viewing Components and Spectral Reconstruction
no_components: {value: parameters.no_components - 1},
eigen_vector: {value: eigen_vectors[parameters.band - Wavelength_START]},
average: {value: average_vector[parameters.band - Wavelength_START]},
PCA_offset : {type: "f" , value: parameters.PCA_offset },
MAX: {type: 'f', value: max_array[parameters.no_components - 1]},
MIN: {type: 'f', value: min_array[parameters.no_components - 1]},
// Color Reconstruction
rgb_Matrix: {value: rgb_Matrix},
light: {value: new THREE.Vector3(Lights[parameters.light_type][0], Lights[parameters.light_type][1], Lights[parameters.light_type][2])},
Luminance: {type: "f" , value: parameters.luminance / 100.0},
add_gamut: {type: "b" , value: parameters.add_gamut },
with_linearizer: {type: "b" , value: parameters.with_linearizer },
// Wavelength Reconstruction
R_eigen_vector: {value: eigen_vectors[parameters.band_R - Wavelength_START]},
G_eigen_vector: {value: eigen_vectors[parameters.band_G - Wavelength_START]},
B_eigen_vector: {value: eigen_vectors[parameters.band_B - Wavelength_START]},
R_average: {type: 'f', value: average_vector[parameters.band_R - Wavelength_START]},
G_average: {type: 'f', value: average_vector[parameters.band_G - Wavelength_START]},
B_average: {type: 'f', value: average_vector[parameters.band_B - Wavelength_START]},
// Gamma Correction
with_Gamma_Corr: {type: "b" , value: parameters.with_Gamma_Corr },
scale : {type: "f" , value: parameters.scale },
gamma : {type: "f" , value: parameters.gamma },
offset : {type: "f" , value: parameters.offset },
},
defines: {
MAX_NO_COMPONENTS: PCA_bands,
},
vertexShader: document.getElementById( 'vs' ).textContent.trim(),
fragmentShader: document.getElementById( 'fs' ).textContent.trim(),
glslVersion: THREE.GLSL3
});
// Creating mesh
const geometry = new THREE.PlaneGeometry(planeWidth, planeHeight);
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
// Update function for GUI
function update() {
material.uniforms.operation.value = parameters.operation;
material.uniforms.no_components.value = parameters.no_components - 1;
material.uniforms.eigen_vector.value = eigen_vectors[parameters.band - Wavelength_START];
material.uniforms.average.value = average_vector[parameters.band - Wavelength_START];
material.uniforms.MAX.value = max_array[parameters.no_components - 1];
material.uniforms.MIN.value = min_array[parameters.no_components - 1];
material.uniforms.PCA_offset.value = parameters.PCA_offset;
material.uniforms.light.value = new THREE.Vector3(Lights[parameters.light_type][0], Lights[parameters.light_type][1], Lights[parameters.light_type][2]);
material.uniforms.Luminance.value = parameters.luminance / 100.0;
material.uniforms.add_gamut.value = parameters.add_gamut;
material.uniforms.with_linearizer.value = parameters.with_linearizer;
material.uniforms.R_eigen_vector.value = eigen_vectors[parameters.band_R - Wavelength_START];
material.uniforms.G_eigen_vector.value = eigen_vectors[parameters.band_G - Wavelength_START];
material.uniforms.B_eigen_vector.value = eigen_vectors[parameters.band_B - Wavelength_START];
material.uniforms.R_average.value = average_vector[parameters.band_R - Wavelength_START];
material.uniforms.G_average.value = average_vector[parameters.band_G - Wavelength_START];
material.uniforms.B_average.value = average_vector[parameters.band_B - Wavelength_START];
material.uniforms.with_Gamma_Corr.value = parameters.with_Gamma_Corr;
material.uniforms.scale.value = parameters.scale;
material.uniforms.gamma.value = parameters.gamma;
material.uniforms.offset.value = parameters.offset;
}
// Creating GUI
const gui = new GUI();
var modelSelected = gui.add(ModelInfo, 'model', ['petal_016_600', 'petal_014_600'] );
modelSelected.onChange(function(value) {
[components, textures, eigen_vectors, average_vector,
PCA_bands, HSI_bands, min_array, max_array] = get_PCA_data(value + ".json");
material.uniforms.Components_texture.value = textures;
material.uniforms.texture_size.value = new THREE.Vector2(width, height);
update();
// Changing slider max level
PCA.children[0].max(PCA_bands);
PCA.children[0].setValue(PCA_bands);
} );
gui.add(parameters, 'operation', { "Components": 0, "Spectral Reconstruction": 1, "Color Reconstruction": 2, "Wavelength Reconstruction": 3}).onChange( update );
var PCA = gui.addFolder('PCA').onChange( update );
PCA.add( parameters, 'no_components', 1, PCA_bands, 1 ).name("no components").onChange( update );
var Spectral = gui.addFolder('Spectral Reconstruction').onChange( update );
Spectral.add( parameters, 'band', Wavelength_START, HSI_bands + Wavelength_START - 1, 1 ).onChange( update );
Spectral.add(parameters, 'PCA_offset', -1.0, 1.0).step(0.001).name("PCA Offset").onChange( update );
var color = gui.addFolder('Color Reconstruction').onChange( update );
color.add(parameters, 'light_type', LightType).name("Light Type").onChange( update );
color.add(parameters, 'luminance', 0, 300).step(1).name("Luminance").onChange( update );
color.add(parameters, 'add_gamut').name("Show Gamut");
color.add(parameters, 'with_linearizer').name("sRGB");
var wavelength = gui.addFolder('Wavelength Reconstruction').onChange( update );
wavelength.add( parameters, 'band_R', Wavelength_START, HSI_bands + Wavelength_START - 1, 1 ).name("R").onChange( update );
wavelength.add( parameters, 'band_G', Wavelength_START, HSI_bands + Wavelength_START - 1, 1 ).name("G").onChange( update );
wavelength.add( parameters, 'band_B', Wavelength_START, HSI_bands + Wavelength_START - 1, 1 ).name("B").onChange( update );
var gamma = wavelength.addFolder('Gamma Correction').onChange( update );
gamma.add(parameters, 'with_Gamma_Corr').name("Gamma");
gamma.add(parameters, 'scale', 1.0, 20.0).step(0.1).onChange( update );
gamma.add(parameters, 'gamma', 0.0, 10.0).step(0.01).onChange( update );
gamma.add(parameters, 'offset', -10.0, 10.0).step(0.01).onChange( update );
controls.addEventListener('change', render); // use if there is no animation loop
controls.addEventListener('start', startChange); // use if there is no animation loop
controls.addEventListener('end', endChange); // use if there is no animation loop
window.addEventListener( 'resize', onWindowResize );
container.addEventListener('mousemove', onMouseMove, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
/*** Selecting pixel with cross cursor ***/
// Author: Philippe Colantoni
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var onClickPosition = new THREE.Vector2();
function startChange() {
container.setAttribute('style', 'cursor:move;');
}
function endChange() {
container.setAttribute('style', 'cursor:crosshair;');
}
function onMouseMove(evt) {
evt.preventDefault();
var array = getMousePosition(container, evt.clientX, evt.clientY);
onClickPosition.fromArray(array);
var intersects = getIntersects(onClickPosition, scene.children);
if (intersects.length > 0 && intersects[0].uv) {
cross_cursor = intersects[0].uv;
}
}
function getMousePosition(dom, x, y) {
var rect = dom.getBoundingClientRect();
return [(x - rect.left) / rect.width, (y - rect.top) / rect.height];
}
function getIntersects(point, objects) {
mouse.set((point.x * 2) - 1, -(point.y * 2) + 1);
raycaster.setFromCamera(mouse, camera);
return raycaster.intersectObjects(objects);
}
/*** Add D3 js code here for graph for displaying spectrum of selected pixel ***/
</script>
</body>
</html>