Last active
March 17, 2019 12:59
-
-
Save aribornstein/5c0509ed2153c546c3d2a945eded21de to your computer and use it in GitHub Desktop.
tensorflow.js RGB2LAB code example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://www.easyrgb.com/en/math.php | |
function tfRGB2LAB(rgb) { | |
let a_con = tf.pow(rgb.add(0.055).div(1.055), 2.4); | |
let b_con = rgb.div(12.92); | |
let rgb_2 = tf.where(rgb.greater(0.04045), a_con, b_con); | |
const [r, g, b] = rgb_2.split(3, 2); | |
let x = r | |
.mul(0.4124) | |
.add(g.mul(0.3576)) | |
.add(b.mul(0.1805)) | |
.div(0.95047), | |
y = r | |
.mul(0.2126) | |
.add(g.mul(0.7152)) | |
.add(b.mul(0.0722)) | |
.div(1.0), | |
z = r | |
.mul(0.0193) | |
.add(g.mul(0.1192)) | |
.add(b.mul(0.9505)) | |
.div(1.08883), | |
xyz = tf.concat([x, y, z], 2); | |
xyz = tf.where( | |
xyz.greater(0.008856), | |
tf.pow(xyz, 1 / 3), | |
xyz.mul(7.787).add(16 / 116) | |
); | |
[x, y, z] = xyz.split(3, 2); | |
return tf.concat( | |
[y.mul(116).sub(16), x.sub(y).mul(500), y.sub(z).mul(200)], | |
2 | |
); | |
} | |
function tfLAB2RGB(lab) { | |
let [lab_l, lab_a, lab_b] = lab.split(3, 2), | |
y = lab_l.add(16).div(116), | |
x = lab_a.div(500).add(y), | |
z = y.sub(lab_b.div(200)), | |
xyz = tf.concat([x, y, z], 2); | |
const xyz_pow3 = xyz.pow(3); | |
xyz = tf.where( | |
xyz_pow3.greater(0.008856), | |
xyz_pow3, | |
xyz.sub(16 / 116).div(7.787) | |
); | |
[x, y, z] = xyz.split(3, 2); // this can be replaced with matmul | |
x = x.mul(0.95047); | |
y = y.mul(1.0); | |
z = z.mul(1.08883); | |
let r = x | |
.mul(3.2406) | |
.add(y.mul(-1.5372)) | |
.add(z.mul(-0.4986)), | |
g = x | |
.mul(-0.9689) | |
.add(y.mul(1.8758)) | |
.add(z.mul(0.0415)), | |
b = x | |
.mul(0.0557) | |
.add(y.mul(-0.204)) | |
.add(z.mul(1.057)), | |
rgb = tf.concat([r, g, b], 2); | |
rgb = tf.where( | |
rgb.greater(0.0031308), | |
rgb | |
.pow(1 / 2.4) | |
.mul(1.055) | |
.sub(0.055), | |
rgb.mul(12.92) | |
); | |
return rgb | |
.minimum(1) | |
.mul(255) | |
.maximum(0); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment