Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions components/annotorious-annotator/detect-lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ export function removeOutlierLines(lines, toleranceRatio = 1.5) {
return lines.filter((_, i) => validIndices.has(i))
}

/**
* Validates that a column element is a properly formatted array with numeric start and end values.
* @param {*} col - The column element to validate
* @returns {boolean} True if the column is an array with at least 2 numeric elements
*/
function isValidColumn(col) {
return Array.isArray(col) && col.length >= 2 &&
typeof col[0] === 'number' && typeof col[1] === 'number'
}

export function detectColumns(binary, minColumnWidth = 50, spaceThreshold = 0.1) {
const verticalProj = new cv.Mat()
cv.reduce(binary, verticalProj, 0, cv.REDUCE_SUM, cv.CV_32S)
Expand Down Expand Up @@ -50,19 +60,40 @@ export function detectColumns(binary, minColumnWidth = 50, spaceThreshold = 0.1)

if (columns.length > 1) {
const merged = []
let [curStart, curEnd] = columns[0]
for (const [s, e] of columns.slice(1)) {
if (s - curEnd < 3) curEnd = e
else {
merged.push([curStart, curEnd])
[curStart, curEnd] = [s, e]
// Ensure columns[0] is a valid array before destructuring
if (!isValidColumn(columns[0])) {
console.warn("Invalid column format detected, using default column")
columns = [[0, binary.cols]]
} else {
let curStart = columns[0][0]
let curEnd = columns[0][1]
for (const col of columns.slice(1)) {
// Validate each column element
if (!isValidColumn(col)) {
console.warn("Skipping invalid column element:", col)
continue
}
const [s, e] = col
if (s - curEnd < 3) curEnd = e
else {
merged.push([curStart, curEnd])
curStart = s
curEnd = e
}
}
merged.push([curStart, curEnd])
columns = merged
}
merged.push([curStart, curEnd])
columns = merged
}

const totalWidth = columns.reduce((sum, [s, e]) => sum + (e - s), 0)
// Safely calculate total width with validation
const totalWidth = columns.reduce((sum, col) => {
if (isValidColumn(col)) {
return sum + (col[1] - col[0])
}
return sum
}, 0)

if (columns.length === 0 || totalWidth > 0.85 * binary.cols) {
columns = [[0, binary.cols]]
}
Expand Down