-
Notifications
You must be signed in to change notification settings - Fork 1
/
GrabCut.ts
417 lines (349 loc) · 16.1 KB
/
GrabCut.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import * as GMM from "./GMM";
import * as BK from "./BKGraph";
import * as FlowBase from "./FlowNetworkSolver";
import * as Mat from "./Matrix";
import * as Util from "./Utility";
import * as Conv from "./ConvergenceChecker";
import * as V3 from "./V3";
export enum Trimap {
Background = 0,
Foreground = 1,
Unknown = 2
}
export interface Options {
tolerance: number, //in %
maxIterations: number,
cohesionFactor: number,
nFGClusters: number,
nBGClusters: number
}
export class GrabCut {
private height: number;
private width: number;
private flattenedImg: Mat.Matrix[]; //Matrices -> [R,G,B] Each element ranges from 0-255
private trimap: Uint8Array;
private matte: Uint8Array;
private fgGMM = new GMM.GMM();
private bgGMM = new GMM.GMM();
constructor(image: Mat.Matrix[], width: number, height: number) {
this.height = height;
this.width = width;
let nPixels = width * height
this.matte = new Uint8Array(nPixels);
this.trimap = new Uint8Array(nPixels);
this.flattenedImg = image;
}
SetTrimap(trimap: Trimap[][], width: number, height: number): void {
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
let ind = GetArrayIndex(r, c, width);
this.trimap[ind] = trimap[r][c];
}
}
}
//Returns the alpha mask
BeginCrop(opt: Options) {
console.log(opt);
for (let i = 0; i < this.trimap.length; i++) {
this.matte[i] = (this.trimap[i] == Trimap.Background) ? Trimap.Background : Trimap.Foreground;
}
let [fgPixels, bgPixels] = GrabCut.SegregatePixels(this.flattenedImg, this.matte, 0, 0, this.height, this.width);
//Initial color GMMs
const GMM_N_ITER = 5;
const MIN_PERCENT_CHANGE = 1;
console.time("Grabcut-GM");
this.fgGMM.Fit(fgPixels, opt.nFGClusters, GMM.Initializer.KMeansPlusPlus, GMM_N_ITER, MIN_PERCENT_CHANGE);
this.bgGMM.Fit(bgPixels, opt.nBGClusters, GMM.Initializer.KMeansPlusPlus, GMM_N_ITER, MIN_PERCENT_CHANGE);
console.timeEnd("Grabcut-GM");
this.RunIterations(opt.maxIterations, opt.tolerance, opt.cohesionFactor);
}
RunIterations(nIter: number, tolerancePercent: number, cohesionFactor: number) {
//Create network graph (with edges between neighbouring pixels set)
//Clone this network & populate with source and sink for use in the graphcut.
let flowNetwork: FlowBase.IFlowNetwork = new BK.BKNetwork();//new Dinic.DinicNetwork();;
let maxFlowSolver: FlowBase.IMaxFlowSolver = BK.BKMaxflow;//Dinic.DinicSolver;//;
console.time("Grabcut-Pixel Graph");
let [network, maxCapacity] = GrabCut.GeneratePixel2PixelGraph(this.flattenedImg, this.width, this.height, flowNetwork, cohesionFactor);
let [srcNode, sinkNode] = GrabCut.InitSourceAndSink(network, this.width, this.height);
console.timeEnd("Grabcut-Pixel Graph");
let conv = new Conv.ConvergenceChecker(tolerancePercent, nIter);
let energy: number;
let labels = Util.Fill<number>(this.width * this.height, 0);
console.time("Grabcut-Graph Cut");
do {
console.log(`iter:${conv.getCurrentIter()}`);
console.time("Grabcut-Graph init");
let filterEmptyGroups = (indices: number[], groupSize: number[]) => {
let validIndices = [];
let nonEmptyGroups = [];
for (let i = 0; i < indices.length; i++) {
if (groupSize[i] > 0) {
validIndices.push(indices[i]);
nonEmptyGroups.push(groupSize[i]);
}
}
return [validIndices, nonEmptyGroups];
};
console.time("Graphcut-Graph GMM-recomputation");
let [fgInd, fgGroupSizes, bgInd, bgGroupSizes] = GrabCut.LabelPixels(this.matte, this.height, this.width, this.fgGMM, this.bgGMM, this.flattenedImg, labels);
let [validFgInd, validFgGroupSizes] = filterEmptyGroups(fgInd, fgGroupSizes);
let [validBgInd, validBgGroupSizes] = filterEmptyGroups(bgInd, bgGroupSizes);
[this.fgGMM, this.bgGMM] = GMM.GMM.labelledDataToGMMs(validFgInd, validFgGroupSizes, validBgInd, validBgGroupSizes, labels, this.flattenedImg);
console.timeEnd("Graphcut-Graph GMM-recomputation");
console.log(`fg clusters:${this.fgGMM.clusters.length}, bg clusters:${this.bgGMM.clusters.length}`);
console.time("Grabcut-Graph source sink update");
GrabCut.UpdateSourceAndSink(network, maxCapacity, this.fgGMM, this.bgGMM, this.flattenedImg, this.width, this.height, this.trimap, srcNode, sinkNode);
console.timeEnd("Grabcut-Graph source sink update");
console.time("Grabcut-Graph flow reset");
network.ResetFlow();
console.timeEnd("Grabcut-Graph flow reset");
console.timeEnd("Grabcut-Graph init");
console.time("Grabcut-Graph maxflow");
console.log('max flow');
let flowResult = maxFlowSolver(srcNode, sinkNode, network);
console.timeEnd("Grabcut-Graph maxflow");
console.time("Grabcut-Graph cut");
console.log('cut');
let fgPixelIndices = flowResult.GetSourcePartition();
GrabCut.UpdateMatte(this.matte, this.trimap, fgPixelIndices);
energy = flowResult.GetMaxFlow();
console.timeEnd("Grabcut-Graph cut");
console.log(`Energy: ${energy}`);
} while (!conv.hasConverged(energy))
console.timeEnd("Grabcut-Graph Cut");
//Done
//Alpha mask is now stored in the matte array.
}
//Mask values will be between 0-1
GetAlphaMask(): number[][] {
let alpha = Mat.CreateMatrix(this.height, this.width);
for (let i = 0; i < this.matte.length; i++) {
let [r, c] = get2DArrayIndex(i, this.width);
alpha[r][c] = (this.matte[i] == Trimap.Foreground) ? 1.0 : 0.0;
}
return alpha;
}
private static UpdateMatte(matte: Uint8Array, trimap: Uint8Array, fgPixelIndices: number[]) {
let indexTable = Util.HashItems<number>(fgPixelIndices, n => n);
for (let i = 0; i < matte.length; i++) {
//Only update pixels that are marked as unknown
//All other pixels are treated as ground truth (i.e. BG & FG data from the user)
if (trimap[i] == Trimap.Unknown) {
let isFG = indexTable.ContainsKey(i);
matte[i] = (isFG) ? Trimap.Foreground : Trimap.Background;
}
}
}
//Returns the FG and BG pixel groups
private static SegregatePixels(img: Mat.Matrix[], matte: Uint8Array, top: number, left: number, height: number, width: number): [Mat.Matrix[], Mat.Matrix[]] {
let fgPixels: Mat.Matrix[] = [];
let bgPixels: Mat.Matrix[] = [];
for (let idx = 0; idx < img.length; idx++) {
let currentPixel = img[idx];
if (matte[idx] == Trimap.Foreground) fgPixels.push(currentPixel);
else bgPixels.push(currentPixel);
}
return [fgPixels, bgPixels];
}
//Returns the FG and BG group sizes
private static LabelPixels(
matte: Uint8Array, height: number, width: number,
fgGMM: GMM.GMM, bgGMM: GMM.GMM,
img: Mat.Matrix[], labels: number[]):
[number[], number[], number[], number[]] {
let nFGClusters = fgGMM.clusters.length;
let nBGClusters = bgGMM.clusters.length
let fgGroupSizes = Util.Fill<number>(nFGClusters, 0);
let bgGroupSizes = Util.Fill<number>(nBGClusters, 0);
let maxIndex = function (arr: number[]): number {
let max = -Number.MAX_SAFE_INTEGER;
let maxInd = 0;
for (let i = 0; i < arr.length; i++) {
let current = arr[i];
if (current > max) {
maxInd = i;
max = current;
}
}
return maxInd;
}
//Assign labels to each pixel
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
let linearIndex = GetArrayIndex(r, c, width);
let pixelIsFG = matte[linearIndex] == Trimap.Foreground;
let currentPixel: Mat.Matrix = img[linearIndex];
if (pixelIsFG) {
let likelihoods = fgGMM.Predict(currentPixel).likelihoods;
let fgGroup = maxIndex(likelihoods);
fgGroupSizes[fgGroup]++;
labels[linearIndex] = 0 + fgGroup;
} else { //Bg
let likelihoods = bgGMM.Predict(currentPixel).likelihoods;
let bgGroup = maxIndex(likelihoods);
bgGroupSizes[bgGroup]++;
labels[linearIndex] = nFGClusters + bgGroup;
}
}
}
let fgIndices = Util.Range(0, nFGClusters);
let bgIndices = Util.Range(nFGClusters, nFGClusters + nBGClusters);
return [fgIndices, fgGroupSizes, bgIndices, bgGroupSizes];
}
//TODO: Clone this network so it can be reused between iterations
//Pixel to pixel edge capacities do not change
//Returns the resultant network and the highest edge capacity
private static GeneratePixel2PixelGraph(img: Mat.Matrix[], width: number, height: number, network: FlowBase.IFlowNetwork, cohesionFactor: number): [FlowBase.IFlowNetwork, number] {
let isV3 = V3.isV3(img[0]);
{
let nPixels = height * width;
for (let i = 0; i < nPixels; i++) {
network.CreateNode();
}
}
//Row, Column offsets for all 8 adjacent neighbours
//let neighbours = [[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1]];
//neighbours within the 4 cardinal directions gives a better result than 8 surrounding pixels.
let neighbours = [[0, -1], [-1, 0], [0, 1], [1, 0]];
let coeff = neighbours.map(t => cohesionFactor / Math.sqrt(t[0] ** 2 + t[1] ** 2));
let GetNeighbour = (r: number, c: number, neighbourInd: number): [boolean, number, number] => {
let offset = neighbours[neighbourInd];
let nR = r + offset[0];
let nC = c + offset[1];
let validNeighbour = WithinBounds(nR, nC, width, height);
return [validNeighbour, nR, nC];
};
//Find beta (the mean difference between a pixel and its neighbours)
let nCount = 0;
let diffAcc = 0;
let fnDiffSquare = (isV3) ?
V3.DiffNormSquare :
function (v1: Mat.Matrix, v2: Mat.Matrix) { return Mat.NormSquare(Mat.Sub(v1, v2)) };
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
let linearInd = GetArrayIndex(r, c, width);
let currentPixel: Mat.Matrix = img[linearInd];
for (let i = 0; i < neighbours.length; i++) {
let [validNeighbour, nR, nC] = GetNeighbour(r, c, i);
if (!validNeighbour) continue;
let neighbourInd = GetArrayIndex(nR, nC, width);
let neighbouringPixel = img[neighbourInd];
let diffSquare = fnDiffSquare(currentPixel, neighbouringPixel);
diffAcc += diffSquare;
nCount++;
}
}
}
let beta = 0.5 / (diffAcc / nCount);
let maxCap = -Number.MAX_SAFE_INTEGER;
//Set pixel to pixel edge capacities
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
let nodeIndex = GetArrayIndex(r, c, width);
for (let i = 0; i < neighbours.length; i++) {
let [validNeighbour, nR, nC] = GetNeighbour(r, c, i);
if (!validNeighbour) continue;
let neighbourIndex = GetArrayIndex(nR, nC, width);
let diffSquare = fnDiffSquare(img[nodeIndex], img[neighbourIndex]);
let exponent = -beta * diffSquare;
let capacity = coeff[i] * Math.exp(exponent);
//Debugging purposes only
if (isNaN(capacity)) {
console.log({
coeff: coeff,
beta: beta,
exponent: exponent,
capacity: capacity
});
}
network.CreateEdge(nodeIndex, neighbourIndex, capacity);
maxCap = (capacity > maxCap) ? capacity : maxCap;
}
}
}
console.log(`Pixel to pixel maximum capacity:${maxCap}`);
return [network, maxCap];
}
//Creates edges from the source nodes to the pixels &
//edges from the pixel node to the sink node
//Returns the [sourceNodeIndex, sinkNodeIndex]
private static InitSourceAndSink(network: FlowBase.IFlowNetwork, width: number, height: number): [number, number] {
let srcInd = network.CreateNode();
let sinkInd = network.CreateNode();
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
//Src to pixel
let pixelNodeInd = GetArrayIndex(r, c, width);
network.CreateEdge(srcInd, pixelNodeInd, 0);
}
}
for (let r = 0; r < height; r++) {
for (let c = 0; c < width; c++) {
//Pixel to src
let pixelNodeInd = GetArrayIndex(r, c, width);
network.CreateEdge(pixelNodeInd, sinkInd, 0);
}
}
return [srcInd, sinkInd];
}
//Source node represents the foreground
//Returns the new network, with the edges connecting the source (FG) and sink (BG) to the pixels added
//[network, sourceNodeIndex, sinkNodeIndex]
private static UpdateSourceAndSink(
network: FlowBase.IFlowNetwork, maxCap: number,
gmmFG: GMM.GMM, gmmBG: GMM.GMM,
image: Mat.Matrix[], width: number, height: number,
trimap: Uint8Array,
srcNode: number, sinkNode: number): void {
let nPixels = width * height;
for (let idx = 0; idx < nPixels; idx++) {
switch (trimap[idx]) {
case Trimap.Foreground: {
network.UpdateEdge(srcNode, idx, maxCap);
network.UpdateEdge(idx, sinkNode, 0);
break;
}
case Trimap.Background: {
network.UpdateEdge(srcNode, idx, 0);
network.UpdateEdge(idx, sinkNode, maxCap);
break;
}
case Trimap.Unknown: {
let currentPixel: Mat.Matrix = image[idx];
let pFore = GrabCut.GetTLinkWeight(gmmBG, currentPixel);
let pBack = GrabCut.GetTLinkWeight(gmmFG, currentPixel);
network.UpdateEdge(srcNode, idx, pFore);
network.UpdateEdge(idx, sinkNode, pBack);
break;
}
}
}
}
private static GetTLinkWeight(gmm: GMM.GMM, pixel: Mat.Matrix): number {
let gmmResult = gmm.Predict(pixel).TotalLikelihood();
let res = -Math.log(gmmResult);
if (isNaN(res)) {
console.log({
gmm: gmm,
res: res,
pixel: pixel,
gmmResult: gmmResult
});
//Temporary bandaid
return 0;
}
return res;
}
}
function WithinBounds(row: number, col: number, width: number, height: number): boolean {
return (row >= 0 && row < height) && (col >= 0 && col < width);
}
function GetArrayIndex(row: number, col: number, width: number): number {
return row * width + col;
}
//Indices are returned in the form of [row, col]
function get2DArrayIndex(index1D: number, width: number): [number, number] {
let row = Math.floor(index1D / width);
let col = index1D % width;
return [row, col];
}