【发布时间】:2018-08-14 01:05:47
【问题描述】:
我编写了一个 javascript 程序,它使用遗传算法仅使用三角形重新创建图像。策略如下:
- 生成一个随机模型池,每个模型都有一个三角形数组(3 个点和一种颜色)
- 评估每个模型的适应度。为此,我将原始图像的像素阵列与我的模型进行比较。我使用Cosine Similarity 来比较数组
- 保留最佳模型,并将它们配对以创建新模型
- 随机变异一些模型
- 评估新池并继续
我遇到的问题是它非常慢,大部分时间都花在获取模型的像素上(将三角形列表(颜色+点)转换为像素数组)。 我现在是这样做的:
我的像素数组是一维数组,我需要能够将 x,y 坐标转换为索引:
static getIndex(x, y, width) {
return 4 * (width * y + x);
}
然后我可以画一个点:
static plot(x, y, color, img) {
let idx = this.getIndex(x, y, img.width);
let added = [color.r, color.g, color.b, map(color.a, 0, 255, 0, 1)];
let base = [img.pixels[idx], img.pixels[idx + 1], img.pixels[idx + 2], map(img.pixels[idx + 3], 0, 255, 0, 1)];
let a01 = 1 - (1 - added[3]) * (1 - base[3]);
img.pixels[idx + 0] = Math.round((added[0] * added[3] / a01) + (base[0] * base[3] * (1 - added[3]) / a01)); // red
img.pixels[idx + 1] = Math.round((added[1] * added[3] / a01) + (base[1] * base[3] * (1 - added[3]) / a01)); // green
img.pixels[idx + 2] = Math.round((added[2] * added[3] / a01) + (base[2] * base[3] * (1 - added[3]) / a01)); // blue
img.pixels[idx + 3] = Math.round(map(a01, 0, 1, 0, 255));
}
然后一行:
static line(x0, y0, x1, y1, img, color) {
x0 = Math.round(x0);
y0 = Math.round(y0);
x1 = Math.round(x1);
y1 = Math.round(y1);
let dx = Math.abs(x1 - x0);
let dy = Math.abs(y1 - y0);
let sx = x0 < x1 ? 1 : -1;
let sy = y0 < y1 ? 1 : -1;
let err = dx - dy;
do {
this.plot(x0, y0, color, img);
let e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
} while (x0 != x1 || y0 != y1);
}
最后是一个三角形:
static drawTriangle(triangle, img) {
for (let i = 0; i < triangle.points.length; i++) {
let point = triangle.points[i];
let p1 =
i === triangle.points.length - 1
? triangle.points[0]
: triangle.points[i + 1];
this.line(point.x, point.y, p1.x, p1.y, img, triangle.color);
}
this.fillTriangle(triangle, img);
}
static fillTriangle(triangle, img) {
let vertices = Array.from(triangle.points);
vertices.sort((a, b) => a.y > b.y);
if (vertices[1].y == vertices[2].y) {
this.fillBottomFlatTriangle(vertices[0], vertices[1], vertices[2], img, triangle.color);
} else if (vertices[0].y == vertices[1].y) {
this.fillTopFlatTriangle(vertices[0], vertices[1], vertices[2], img, triangle.color);
} else {
let v4 = {
x: vertices[0].x + float(vertices[1].y - vertices[0].y) / float(vertices[2].y - vertices[0].y) * (vertices[2].x - vertices[0].x),
y: vertices[1].y
};
this.fillBottomFlatTriangle(vertices[0], vertices[1], v4, img, triangle.color);
this.fillTopFlatTriangle(vertices[1], v4, vertices[2], img, triangle.color);
}
}
static fillBottomFlatTriangle(v1, v2, v3, img, color) {
let invslope1 = (v2.x - v1.x) / (v2.y - v1.y);
let invslope2 = (v3.x - v1.x) / (v3.y - v1.y);
let curx1 = v1.x;
let curx2 = v1.x;
for (let scanlineY = v1.y; scanlineY <= v2.y; scanlineY++) {
this.line(curx1, scanlineY, curx2, scanlineY, img, color);
curx1 += invslope1;
curx2 += invslope2;
}
}
static fillTopFlatTriangle(v1, v2, v3, img, color) {
let invslope1 = (v3.x - v1.x) / (v3.y - v1.y);
let invslope2 = (v3.x - v2.x) / (v3.y - v2.y);
let curx1 = v3.x;
let curx2 = v3.x;
for (let scanlineY = v3.y; scanlineY > v1.y; scanlineY--) {
this.line(curx1, scanlineY, curx2, scanlineY, img, color);
curx1 -= invslope1;
curx2 -= invslope2;
}
}
你可以看到完整的代码here
所以,我想知道:
- 是否可以优化这段代码?
- 如果是,最好的方法是什么?也许有一个图书馆比我做得更好?还是使用工人?
谢谢!
【问题讨论】:
-
如果它在浏览器中运行,您可以查看使用不可见的
<canvas>元素为您绘制。然后,您可以使用getImageData访问像素。 -
我也想知道余弦相似度是否是比较两个图像的合适指标。亮度减半的完美再现仍然具有 1 的余弦相似度。两个阵列之间差异的简单均方根可能更稳健,也同样简单。
-
为什么不在codereview发帖?
-
@Thomas 感谢您的建议!我会尝试他们两个,这似乎很有趣。你认为在不可见的画布上绘图会更快吗?
-
@Viney 我不得不承认我不知道 codereview,这似乎是一个更适合我的问题的地方,谢谢!
标签: javascript image algorithm optimization genetic