Commit 28d22814 authored by Matija Obreza's avatar Matija Obreza
Browse files

Rectangle

parent db599467
......@@ -7,6 +7,7 @@
*/
import Vector from './vector';
import Rectangle from './rectangle';
export const logElapsed = (prevTime, message?: string): number => {
const now = Date.now();
......@@ -1531,15 +1532,91 @@ export function connectedComponentLabels(image: ImageData, primaries: Array<Vect
ufColors[ufRoots[i]] = i * 256 / ufRoots.length;
}
const dataR = new Uint8ClampedArray(w * h * 4);
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const p = (y * w + x) * 4;
// dataR[p + 0] = dataR[p + 1] = dataR[p + 2] = dataI[y * w + x] * 256 / primaries.length;
const ufRoot = UF.find(dataL[P1(x, y)]);
dataR[p + 0] = dataR[p + 1] = dataR[p + 2] = ufColors[ufRoot];
dataR[p + 3] = ufRoot === 0 ? 0 : 255;
dataR.fill(0);
const findRect = (ufColor: number) => {
let minX;
let minY;
let maxX;
let maxY;
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
if (UF.find(dataL[P1(x, y)]) === ufColor) {
if (minX === undefined) {
minX = maxX = x;
minY = maxY = y;
} else {
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
}
}
}
return minX === undefined ? undefined : new Rectangle(minX, minY, maxX, maxY);
}
const rectangles: Array<Rectangle> = new Array<Rectangle>();
for (const ufRoot of ufRoots) {
if (ufRoot === 0) {
continue;
}
const rect = findRect(ufRoot);
if (rect === undefined) {
continue;
}
if (rect.area < 50 || rect.minX === 0 || rect.minY === 0 || rect.maxX === w - 1 || rect.maxY === h - 1) {
continue;
}
// console.log(`UfRect#${ufRoot}`, rect);
rectangles.push(rect);
}
const rectWidths = rectangles.map((r) => r.width);
const rectHeights = rectangles.map((r) => r.height);
const rectAreas = rectangles.map((r) => r.area);
const rectMedianWidth = rectWidths.sort((a, b) => a - b)[Math.floor(rectWidths.length / 2)];
const rectMedianHeight = rectHeights.sort((a, b) => a - b)[Math.floor(rectHeights.length / 2)];
const rectMedianArea = rectAreas.sort((a, b) => a - b)[Math.floor(rectAreas.length / 2)];
console.log(`Rectange median ${rectMedianWidth}x${rectMedianHeight} a=${rectMedianArea}`);
let objectCounter = 0;
for (const rect of rectangles) {
if (Math.abs(rect.width - rectMedianWidth) / rectMedianWidth > 0.5
&& Math.abs(rect.height - rectMedianHeight) / rectMedianHeight > 0.5
&& Math.abs(rect.area - rectMedianArea) / rectMedianArea > 0.5) {
continue;
}
objectCounter++;
for (let x = rect.minX; x <= rect.maxX; x++) {
let p = (rect.minY * w + x) * 4;
dataR[p + 0] = 255;
dataR[p + 3] = 255;
p = (rect.maxY * w + x) * 4;
dataR[p + 0] = 255;
dataR[p + 3] = 255;
}
for (let y = rect.minY; y <= rect.maxY; y++) {
let p = (y * w + rect.minX) * 4;
dataR[p + 0] = 255;
dataR[p + 3] = 255;
p = (y * w + rect.maxX) * 4;
dataR[p + 0] = 255;
dataR[p + 3] = 255;
}
}
// for (let y = 0; y < h; y++) {
// for (let x = 0; x < w; x++) {
// const p = (y * w + x) * 4;
// // dataR[p + 0] = dataR[p + 1] = dataR[p + 2] = dataI[y * w + x] * 256 / primaries.length;
// const ufRoot = UF.find(dataL[P1(x, y)]);
// dataR[p + 0] = dataR[p + 1] = dataR[p + 2] = ufColors[ufRoot];
// dataR[p + 3] = ufRoot === 0 ? 0 : 255;
// }
// }
console.log(`\n\nDetected ${objectCounter} objects!!\n\n`);
return new ImageData(dataR, w, h);
}
class Rectangle {
// A is the top-left point
private Ax;
private Ay;
// B is the bottom-right point
private Bx;
private By;
public constructor(x1, y1, x2, y2) {
this.Ax = Math.min(x1, x2);
this.Ay = Math.min(y1, y2);
this.Bx = Math.max(x1, x2);
this.By = Math.max(y1, y2);
}
public get minX(): number {
return this.Ax;
}
public get maxX(): number {
return this.Bx;
}
public get minY(): number {
return this.Ay;
}
public get maxY(): number {
return this.By;
}
public get width(): number {
return this.Bx - this.Ax;
}
public get height(): number {
return this.By - this.Ay;
}
public get area(): number {
return this.width * this.height;
}
public get diagonal(): number {
return Math.sqrt(this.width * this.width + this.height * this.height);
}
};
export { Rectangle 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