|
| 1 | +package org.appdevforall.codeonthego.computervision.utils |
| 2 | + |
| 3 | +import android.graphics.Bitmap |
| 4 | +import org.appdevforall.codeonthego.computervision.utils.BitmapUtils.calculateVerticalProjection |
| 5 | + |
| 6 | +object SmartBoundaryDetector { |
| 7 | + |
| 8 | + private const val DEFAULT_EDGE_IGNORE_PERCENT = 0.05f |
| 9 | + private const val LEFT_ZONE_END_PERCENT = 0.4f |
| 10 | + private const val RIGHT_ZONE_START_PERCENT = 0.6f |
| 11 | + private const val MIN_GAP_WIDTH_PERCENT = 0.02 |
| 12 | + private const val PRIMARY_ACTIVITY_THRESHOLD = 0.05f |
| 13 | + private const val FALLBACK_ACTIVITY_THRESHOLD = 0.01f |
| 14 | + private const val LEFT_FALLBACK_BOUND_PERCENT = 0.15f |
| 15 | + private const val RIGHT_FALLBACK_BOUND_PERCENT = 0.85f |
| 16 | + |
| 17 | + fun detectSmartBoundaries( |
| 18 | + bitmap: Bitmap, |
| 19 | + edgeIgnorePercent: Float = DEFAULT_EDGE_IGNORE_PERCENT |
| 20 | + ): Pair<Int, Int> { |
| 21 | + val width = bitmap.width |
| 22 | + val projection = calculateVerticalProjection(bitmap) |
| 23 | + val minimumGapWidth = (width * MIN_GAP_WIDTH_PERCENT).toInt() |
| 24 | + |
| 25 | + val ignoredEdgePixels = (width * edgeIgnorePercent).toInt() |
| 26 | + val leftZoneEnd = (width * LEFT_ZONE_END_PERCENT).toInt() |
| 27 | + val rightZoneStart = (width * RIGHT_ZONE_START_PERCENT).toInt() |
| 28 | + val rightZoneEnd = width - ignoredEdgePixels |
| 29 | + |
| 30 | + val leftSignal = projection.copyOfRange(ignoredEdgePixels, leftZoneEnd) |
| 31 | + var (leftBound, leftGapLength) = findBestGapMidpoint(leftSignal, offset = ignoredEdgePixels) |
| 32 | + if (leftBound == null || leftGapLength < minimumGapWidth) { |
| 33 | + leftBound = findBestGapMidpoint(leftSignal, offset = ignoredEdgePixels, normalizeSignal = true).first |
| 34 | + } |
| 35 | + |
| 36 | + val rightSignal = projection.copyOfRange(rightZoneStart, rightZoneEnd) |
| 37 | + var (rightBound, rightGapLength) = findBestGapMidpoint(rightSignal, offset = rightZoneStart) |
| 38 | + if (rightBound == null || rightGapLength < minimumGapWidth) { |
| 39 | + rightBound = findBestGapMidpoint(rightSignal, offset = rightZoneStart, normalizeSignal = true).first |
| 40 | + } |
| 41 | + |
| 42 | + val finalLeftBound = leftBound ?: (width * LEFT_FALLBACK_BOUND_PERCENT).toInt() |
| 43 | + val finalRightBound = rightBound ?: (width * RIGHT_FALLBACK_BOUND_PERCENT).toInt() |
| 44 | + return Pair(finalLeftBound, finalRightBound) |
| 45 | + } |
| 46 | + |
| 47 | + private fun findBestGapMidpoint( |
| 48 | + signalSegment: FloatArray, |
| 49 | + offset: Int = 0, |
| 50 | + normalizeSignal: Boolean = false |
| 51 | + ): Pair<Int?, Int> { |
| 52 | + if (signalSegment.isEmpty()) { |
| 53 | + return Pair(null, 0) |
| 54 | + } |
| 55 | + |
| 56 | + val signal = if (normalizeSignal) { |
| 57 | + val minValue = signalSegment.minOrNull() ?: 0f |
| 58 | + FloatArray(signalSegment.size) { index -> signalSegment[index] - minValue } |
| 59 | + } else { |
| 60 | + signalSegment |
| 61 | + } |
| 62 | + |
| 63 | + val activityThresholdMultiplier = if (normalizeSignal) { |
| 64 | + FALLBACK_ACTIVITY_THRESHOLD |
| 65 | + } else { |
| 66 | + PRIMARY_ACTIVITY_THRESHOLD |
| 67 | + } |
| 68 | + val threshold = (signal.maxOrNull() ?: 0f) * activityThresholdMultiplier |
| 69 | + |
| 70 | + var maxGapLength = 0 |
| 71 | + var maxGapMidpoint: Int? = null |
| 72 | + var currentGapStart = -1 |
| 73 | + var previousIsActive = false |
| 74 | + |
| 75 | + signal.forEachIndexed { index, value -> |
| 76 | + val isActive = value > threshold |
| 77 | + if (previousIsActive && !isActive) { |
| 78 | + currentGapStart = index |
| 79 | + } |
| 80 | + |
| 81 | + val isGapClosing = currentGapStart != -1 && (index + 1 == signal.size || (!isActive && signal[index + 1] > threshold)) |
| 82 | + if (isGapClosing) { |
| 83 | + val gapLength = index - currentGapStart + 1 |
| 84 | + if (gapLength > maxGapLength) { |
| 85 | + maxGapLength = gapLength |
| 86 | + maxGapMidpoint = currentGapStart + (gapLength / 2) |
| 87 | + } |
| 88 | + currentGapStart = -1 |
| 89 | + } |
| 90 | + |
| 91 | + previousIsActive = isActive |
| 92 | + } |
| 93 | + |
| 94 | + return Pair(maxGapMidpoint?.plus(offset), maxGapLength) |
| 95 | + } |
| 96 | +} |
0 commit comments