coomdev
10 months ago
3 changed files with 107 additions and 68 deletions
@ -0,0 +1,78 @@ |
|||||
|
"use strict"; |
||||
|
Object.defineProperty(exports, "__esModule", { value: true }); |
||||
|
exports.computeBounds = exports.connectedComponentLabeling = void 0; |
||||
|
function connectedComponentLabeling(binaryImage, width, height) { |
||||
|
var labels = Array(binaryImage.length).fill(0); |
||||
|
var linked = []; |
||||
|
var nextLabel = 1; |
||||
|
function getNeighbors(row, col) { |
||||
|
var neighbors = []; |
||||
|
if (row > 0 && labels[(row - 1) * width + col] > 0) { |
||||
|
neighbors.push(labels[(row - 1) * width + col]); |
||||
|
} |
||||
|
if (col > 0 && labels[row * width + col - 1] > 0) { |
||||
|
neighbors.push(labels[row * width + col - 1]); |
||||
|
} |
||||
|
return neighbors; |
||||
|
} |
||||
|
// First pass
|
||||
|
for (var row = 0; row < height; row++) { |
||||
|
for (var col = 0; col < width; col++) { |
||||
|
var idx = row * width + col; |
||||
|
if (binaryImage[idx] !== 0) { |
||||
|
var neighbors = getNeighbors(row, col); |
||||
|
if (neighbors.length === 0) { |
||||
|
linked[nextLabel] = [nextLabel]; |
||||
|
labels[idx] = nextLabel; |
||||
|
nextLabel++; |
||||
|
} |
||||
|
else { |
||||
|
neighbors.sort(); |
||||
|
var smallestLabel = neighbors[0]; |
||||
|
labels[idx] = smallestLabel; |
||||
|
for (var i = 1; i < neighbors.length; i++) { |
||||
|
linked[neighbors[i]] = linked[neighbors[i]].concat(linked[smallestLabel]); |
||||
|
linked[smallestLabel] = linked[smallestLabel].concat(linked[neighbors[i]]); |
||||
|
linked[neighbors[i]] = Array.from(new Set(linked[neighbors[i]])); |
||||
|
linked[smallestLabel] = Array.from(new Set(linked[smallestLabel])); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
// Second pass
|
||||
|
for (var idx = 0; idx < binaryImage.length; idx++) { |
||||
|
if (binaryImage[idx] !== 0) { |
||||
|
labels[idx] = Math.min.apply(Math, linked[labels[idx]]); |
||||
|
} |
||||
|
} |
||||
|
return labels; |
||||
|
} |
||||
|
exports.connectedComponentLabeling = connectedComponentLabeling; |
||||
|
function computeBounds(labels, width, height) { |
||||
|
var bounds = {}; |
||||
|
for (var row = 0; row < height; row++) { |
||||
|
for (var col = 0; col < width; col++) { |
||||
|
var idx = row * width + col; |
||||
|
var label = labels[idx]; |
||||
|
if (label > 0) { |
||||
|
if (!bounds[label]) { |
||||
|
bounds[label] = { minRow: row, minCol: col, maxRow: row, maxCol: col, area: 1 }; |
||||
|
} |
||||
|
else { |
||||
|
if (row < bounds[label].minRow) |
||||
|
bounds[label].minRow = row; |
||||
|
if (col < bounds[label].minCol) |
||||
|
bounds[label].minCol = col; |
||||
|
if (row > bounds[label].maxRow) |
||||
|
bounds[label].maxRow = row; |
||||
|
if (col > bounds[label].maxCol) |
||||
|
bounds[label].maxCol = col; |
||||
|
++bounds[label].area; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return bounds; |
||||
|
} |
||||
|
exports.computeBounds = computeBounds; |
@ -1,26 +1,3 @@ |
|||||
{ |
{ |
||||
"charset": [ |
|
||||
"", |
|
||||
"0", |
|
||||
"2", |
|
||||
"4", |
|
||||
"8", |
|
||||
"A", |
|
||||
"D", |
|
||||
"G", |
|
||||
"H", |
|
||||
"J", |
|
||||
"K", |
|
||||
"M", |
|
||||
"N", |
|
||||
"P", |
|
||||
"Q", |
|
||||
"R", |
|
||||
"S", |
|
||||
"T", |
|
||||
"V", |
|
||||
"W", |
|
||||
"X", |
|
||||
"Y" |
|
||||
] |
|
||||
} |
} |
||||
|
Loading…
Reference in new issue