Commit fc569627 authored by Matija Obreza's avatar Matija Obreza
Browse files

filterTakeHighest

parent b1787d42
......@@ -82,6 +82,33 @@ export function filterSum(image) {
return result;
}
/**
* Make grayscale image using highest color component
*
* @param image The Image
*/
export function filterTakeHighest(image) {
const w = image.width;
const h = image.height;
const data = image.data;
const result = new ImageData(w, h);
const dataR = result.data;
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const p = (y * w + x) * 4;
const R = data[p + 0];
const G = data[p + 1];
const B = data[p + 2];
const A = data[p + 3];
// dataR[p + 0] = dataR[p + 1] = dataR[p + 2] = (R * (1 - .2126) + G * (1 - .7152) + B * (1 - .0722));
dataR[p + 0] = dataR[p + 1] = dataR[p + 2] = Math.max(R, G, B);
dataR[p + 3] = A;
}
}
return result;
}
/**
* Set RGB to 255 if value is above or equal the threshold, 0 otherwise.
* @param {*} image the image
......@@ -968,22 +995,12 @@ export function colorClassifier(image, buckets: number = 16): ImageData {
}
}
console.log(`Top color ${color.toString()} cleared ${clearedPixels} pixels.`);
})
// const v1 = new Vector(32, 128, 0);
// const v2 = new Vector(0, 96, 32);
// const v3 = new Vector(30, 128, 192);
// const d = Vector.distance(v1, v2);
// console.log('v1', v1.toString(), v1.length(), v1);
// console.log('v2', v2.toString(), v2.length(), v2);
// console.log('Distance', d);
// console.log('v1-v2', v1.diff(v2).toString(), v1.diff(v2), v1.diff(v2).length());
// console.log('v2-v1', v2.diff(v1).toString(), v2.diff(v1), v2.diff(v1).length());
// console.log('v3-v1', v3.diff(v1).toString(), v3.diff(v1), v3.diff(v1).length());
});
return new ImageData(dataR, w, h);
}
/**
* Implementation of filtering by kernel
*
......
class Vector {
private v: number[];
public constructor(x: number = 0, y: number = 0, z: number = 0) {
this.v = [x, y, z];
}
public static fromArray(v: any) {
return new Vector(v[0], v[1], v[2]);
}
public length(): number {
return Math.sqrt(this.v[0] * this.v[0] + this.v[1] * this.v[1] + this.v[2] * this.v[2]);
}
......@@ -19,11 +22,27 @@ class Vector {
return '(' + this.v[0] + ', ' + this.v[1] + ', ' + this.v[2] + ')';
}
public hashCode() {
return this.v[0] * 256 * 256 + this.v[1] * 256 + this.v[0];
}
public eq(other: Vector): boolean {
return this.v[0] === other.v[0] && this.v[1] === other.v[1] && this.v[2] === other.v[2];
}
public static distance(a: Vector, b: Vector): number {
const d = [a.v[0] - b.v[0], a.v[1] - b.v[1], a.v[2] - b.v[2]];
return Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
}
public static compareByCoordinates(a: Vector, b: Vector) {
return a.hashCode() - b.hashCode();
}
public distance(x: number, y: number, z: number): number {
const d = [this.v[0] - x, this.v[1] - y, this.v[2] - z];
return Math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
}
};
export { Vector as default };
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment