-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_processing.js
122 lines (101 loc) · 3.18 KB
/
image_processing.js
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
// Coding Train / Daniel Shiffman
// Weighted Voronoi Stippling
// https://thecodingtrain.com/challenges/181-image-stippling
let points = [];
let delaunay, voronoi;
let gloria;
function preload() {
gloria = loadImage("gloria_pickle.jpg");
}
function setup() {
createCanvas(gloria.width, gloria.height);
for (let i = 0; i < 1000; i++) {
let x = random(width);
let y = random(height);
let col = gloria.get(x, y);
if (random(100) > brightness(col)) {
points.push(createVector(x, y));
} else {
i--;
}
}
delaunay = calculateDelaunay(points);
voronoi = delaunay.voronoi([0, 0, width, height]);
//noLoop();
}
function draw() {
background(255);
let polygons = voronoi.cellPolygons();
let cells = Array.from(polygons);
let centroids = new Array(cells.length);
let weights = new Array(cells.length).fill(0);
let counts = new Array(cells.length).fill(0);
let avgWeights = new Array(cells.length).fill(0);
for (let i = 0; i < centroids.length; i++) {
centroids[i] = createVector(0, 0);
}
gloria.loadPixels();
let delaunayIndex = 0;
for (let i = 0; i < width; i++) {
for (let j = 0; j < height; j++) {
let index = (i + j * width) * 4;
let r = gloria.pixels[index + 0];
let g = gloria.pixels[index + 1];
let b = gloria.pixels[index + 2];
let bright = (r + g + b) / 3;
let weight = 1 - bright / 255;
delaunayIndex = delaunay.find(i, j, delaunayIndex);
centroids[delaunayIndex].x += i * weight;
centroids[delaunayIndex].y += j * weight;
weights[delaunayIndex] += weight;
counts[delaunayIndex]++;
}
}
let maxWeight = 0;
for (let i = 0; i < centroids.length; i++) {
if (weights[i] > 0) {
centroids[i].div(weights[i]);
avgWeights[i] = weights[i] / (counts[i] || 1);
if (avgWeights[i] > maxWeight) {
maxWeight = avgWeights[i];
}
} else {
centroids[i] = points[i].copy();
}
}
for (let i = 0; i < points.length; i++) {
points[i].lerp(centroids[i], 0.5);
}
// for (let i = 0; i < cells.length; i++) {
// let poly = cells[i];
// let centroid = centroids[i];
// let col =gloria.get(centroid.x, centroid.y)
// stroke(0);
// strokeWeight(0.5);
// fill(col);
// beginShape();
// for (let i = 0; i < poly.length; i++) {
// vertex(poly[i][0], poly[i][1]);
// }
// endShape();
// }
for (let i = 0; i < points.length; i++) {
let v = points[i];
let col = gloria.get(v.x, v.y);
stroke(col);
//stroke(0);
let sw = map(avgWeights[i], 0, maxWeight, 1, 14, true);
//sw = 4;
strokeWeight(sw);
point(v.x, v.y);
}
delaunay = calculateDelaunay(points);
voronoi = delaunay.voronoi([0, 0, width, height]);
}
function calculateDelaunay(points) {
let pointsArray = [];
for (let v of points) {
pointsArray.push(v.x, v.y);
}
return new d3.Delaunay(pointsArray);
}