-
Notifications
You must be signed in to change notification settings - Fork 18
ADFA-3290 | Implement smart boundary detection for dynamic margins #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+159
−15
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
.../src/main/java/org/appdevforall/codeonthego/computervision/utils/SmartBoundaryDetector.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package org.appdevforall.codeonthego.computervision.utils | ||
|
|
||
| import android.graphics.Bitmap | ||
| import org.appdevforall.codeonthego.computervision.utils.BitmapUtils.calculateVerticalProjection | ||
|
|
||
| object SmartBoundaryDetector { | ||
|
|
||
| private const val DEFAULT_EDGE_IGNORE_PERCENT = 0.05f | ||
| private const val LEFT_ZONE_END_PERCENT = 0.4f | ||
| private const val RIGHT_ZONE_START_PERCENT = 0.6f | ||
| private const val MIN_GAP_WIDTH_PERCENT = 0.02 | ||
| private const val PRIMARY_ACTIVITY_THRESHOLD = 0.05f | ||
| private const val FALLBACK_ACTIVITY_THRESHOLD = 0.01f | ||
| private const val LEFT_FALLBACK_BOUND_PERCENT = 0.15f | ||
| private const val RIGHT_FALLBACK_BOUND_PERCENT = 0.85f | ||
|
|
||
| fun detectSmartBoundaries( | ||
| bitmap: Bitmap, | ||
| edgeIgnorePercent: Float = DEFAULT_EDGE_IGNORE_PERCENT | ||
| ): Pair<Int, Int> { | ||
| val width = bitmap.width | ||
| val projection = calculateVerticalProjection(bitmap) | ||
| val minimumGapWidth = (width * MIN_GAP_WIDTH_PERCENT).toInt() | ||
|
|
||
| val ignoredEdgePixels = (width * edgeIgnorePercent).toInt() | ||
| val leftZoneEnd = (width * LEFT_ZONE_END_PERCENT).toInt() | ||
| val rightZoneStart = (width * RIGHT_ZONE_START_PERCENT).toInt() | ||
| val rightZoneEnd = width - ignoredEdgePixels | ||
|
|
||
| val leftSignal = projection.copyOfRange(ignoredEdgePixels, leftZoneEnd) | ||
| var (leftBound, leftGapLength) = findBestGapMidpoint(leftSignal, offset = ignoredEdgePixels) | ||
| if (leftBound == null || leftGapLength < minimumGapWidth) { | ||
| leftBound = findBestGapMidpoint(leftSignal, offset = ignoredEdgePixels, normalizeSignal = true).first | ||
| } | ||
|
|
||
| val rightSignal = projection.copyOfRange(rightZoneStart, rightZoneEnd) | ||
| var (rightBound, rightGapLength) = findBestGapMidpoint(rightSignal, offset = rightZoneStart) | ||
| if (rightBound == null || rightGapLength < minimumGapWidth) { | ||
| rightBound = findBestGapMidpoint(rightSignal, offset = rightZoneStart, normalizeSignal = true).first | ||
| } | ||
|
|
||
| val finalLeftBound = leftBound ?: (width * LEFT_FALLBACK_BOUND_PERCENT).toInt() | ||
| val finalRightBound = rightBound ?: (width * RIGHT_FALLBACK_BOUND_PERCENT).toInt() | ||
| return Pair(finalLeftBound, finalRightBound) | ||
| } | ||
|
|
||
| private fun findBestGapMidpoint( | ||
| signalSegment: FloatArray, | ||
| offset: Int = 0, | ||
| normalizeSignal: Boolean = false | ||
| ): Pair<Int?, Int> { | ||
| if (signalSegment.isEmpty()) { | ||
| return Pair(null, 0) | ||
| } | ||
|
|
||
| val signal = if (normalizeSignal) { | ||
| val minValue = signalSegment.minOrNull() ?: 0f | ||
| FloatArray(signalSegment.size) { index -> signalSegment[index] - minValue } | ||
| } else { | ||
| signalSegment | ||
| } | ||
|
|
||
| val activityThresholdMultiplier = if (normalizeSignal) { | ||
| FALLBACK_ACTIVITY_THRESHOLD | ||
| } else { | ||
| PRIMARY_ACTIVITY_THRESHOLD | ||
| } | ||
| val threshold = (signal.maxOrNull() ?: 0f) * activityThresholdMultiplier | ||
|
|
||
| var maxGapLength = 0 | ||
| var maxGapMidpoint: Int? = null | ||
| var currentGapStart = -1 | ||
| var previousIsActive = false | ||
|
|
||
| signal.forEachIndexed { index, value -> | ||
| val isActive = value > threshold | ||
| if (previousIsActive && !isActive) { | ||
| currentGapStart = index | ||
| } | ||
|
|
||
| val isGapClosing = currentGapStart != -1 && (index + 1 == signal.size || (!isActive && signal[index + 1] > threshold)) | ||
| if (isGapClosing) { | ||
| val gapLength = index - currentGapStart + 1 | ||
| if (gapLength > maxGapLength) { | ||
| maxGapLength = gapLength | ||
| maxGapMidpoint = currentGapStart + (gapLength / 2) | ||
| } | ||
| currentGapStart = -1 | ||
| } | ||
|
|
||
| previousIsActive = isActive | ||
| } | ||
|
|
||
| return Pair(maxGapMidpoint?.plus(offset), maxGapLength) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate
edgeIgnorePercentand range bounds beforecopyOfRange.Current logic can throw
IllegalArgumentExceptionwhen callers pass invalidedgeIgnorePercent(e.g., negative or too large), because computed start/end indices may invert.🛡️ Proposed fix
fun detectSmartBoundaries( bitmap: Bitmap, edgeIgnorePercent: Float = DEFAULT_EDGE_IGNORE_PERCENT ): Pair<Int, Int> { val width = bitmap.width + if (width <= 0) { + return Pair(0, 0) + } + + val safeEdgeIgnorePercent = edgeIgnorePercent.coerceIn(0f, 0.49f) val projection = calculateVerticalProjection(bitmap) val minimumGapWidth = (width * MIN_GAP_WIDTH_PERCENT).toInt() - val ignoredEdgePixels = (width * edgeIgnorePercent).toInt() - val leftZoneEnd = (width * LEFT_ZONE_END_PERCENT).toInt() - val rightZoneStart = (width * RIGHT_ZONE_START_PERCENT).toInt() - val rightZoneEnd = width - ignoredEdgePixels + val ignoredEdgePixels = (width * safeEdgeIgnorePercent).toInt() + val leftZoneEnd = (width * LEFT_ZONE_END_PERCENT).toInt().coerceAtLeast(ignoredEdgePixels) + val rightZoneStart = (width * RIGHT_ZONE_START_PERCENT).toInt().coerceIn(0, width) + val rightZoneEnd = (width - ignoredEdgePixels).coerceAtLeast(rightZoneStart) val leftSignal = projection.copyOfRange(ignoredEdgePixels, leftZoneEnd) var (leftBound, leftGapLength) = findBestGapMidpoint(leftSignal, offset = ignoredEdgePixels) if (leftBound == null || leftGapLength < minimumGapWidth) { leftBound = findBestGapMidpoint(leftSignal, offset = ignoredEdgePixels, normalizeSignal = true).first } val rightSignal = projection.copyOfRange(rightZoneStart, rightZoneEnd) var (rightBound, rightGapLength) = findBestGapMidpoint(rightSignal, offset = rightZoneStart) if (rightBound == null || rightGapLength < minimumGapWidth) { rightBound = findBestGapMidpoint(rightSignal, offset = rightZoneStart, normalizeSignal = true).first } - val finalLeftBound = leftBound ?: (width * LEFT_FALLBACK_BOUND_PERCENT).toInt() - val finalRightBound = rightBound ?: (width * RIGHT_FALLBACK_BOUND_PERCENT).toInt() + val fallbackLeft = (width * LEFT_FALLBACK_BOUND_PERCENT).toInt() + val fallbackRight = (width * RIGHT_FALLBACK_BOUND_PERCENT).toInt() + val finalLeftBound = (leftBound ?: fallbackLeft).coerceIn(0, width - 1) + val finalRightBound = (rightBound ?: fallbackRight).coerceIn(finalLeftBound, width - 1) return Pair(finalLeftBound, finalRightBound) }📝 Committable suggestion
🤖 Prompt for AI Agents