Width/height for the document: use avg rather than max

This commit is contained in:
Pierre-Yves Nicolas
2025-06-21 14:45:30 +02:00
parent cf1dc97b7e
commit 5b777e5edc

View File

@@ -69,11 +69,11 @@ fun detectDocumentQuad(mask: Bitmap): Quad? {
fun extractDocument(originalBitmap: Bitmap, quad: Quad, rotationDegrees: Int): Bitmap { fun extractDocument(originalBitmap: Bitmap, quad: Quad, rotationDegrees: Int): Bitmap {
val widthTop = norm(quad.topLeft, quad.topRight) val widthTop = norm(quad.topLeft, quad.topRight)
val widthBottom = norm(quad.bottomLeft, quad.bottomRight) val widthBottom = norm(quad.bottomLeft, quad.bottomRight)
val maxWidth = max(widthTop, widthBottom).toInt() val targetWidth = (widthTop + widthBottom) / 2
val heightLeft = norm(quad.topLeft, quad.bottomLeft) val heightLeft = norm(quad.topLeft, quad.bottomLeft)
val heightRight = norm(quad.topRight, quad.bottomRight) val heightRight = norm(quad.topRight, quad.bottomRight)
val maxHeight = max(heightLeft, heightRight).toInt() val targetHeight = (heightLeft + heightRight) / 2
val srcPoints = MatOfPoint2f( val srcPoints = MatOfPoint2f(
quad.topLeft.toCv(), quad.topLeft.toCv(),
@@ -83,16 +83,16 @@ fun extractDocument(originalBitmap: Bitmap, quad: Quad, rotationDegrees: Int): B
) )
val dstPoints = MatOfPoint2f( val dstPoints = MatOfPoint2f(
org.opencv.core.Point(0.0, 0.0), org.opencv.core.Point(0.0, 0.0),
org.opencv.core.Point(maxWidth.toDouble(), 0.0), org.opencv.core.Point(targetWidth.toDouble(), 0.0),
org.opencv.core.Point(maxWidth.toDouble(), maxHeight.toDouble()), org.opencv.core.Point(targetWidth.toDouble(), targetHeight.toDouble()),
org.opencv.core.Point(0.0, maxHeight.toDouble()) org.opencv.core.Point(0.0, targetHeight.toDouble())
) )
val transform = Imgproc.getPerspectiveTransform(srcPoints, dstPoints) val transform = Imgproc.getPerspectiveTransform(srcPoints, dstPoints)
val inputMat = Mat() val inputMat = Mat()
Utils.bitmapToMat(originalBitmap, inputMat) Utils.bitmapToMat(originalBitmap, inputMat)
val outputMat = Mat() val outputMat = Mat()
val outputSize = Size(maxWidth.toDouble(), maxHeight.toDouble()) val outputSize = Size(targetWidth.toDouble(), targetHeight.toDouble())
Imgproc.warpPerspective(inputMat, outputMat, transform, outputSize) Imgproc.warpPerspective(inputMat, outputMat, transform, outputSize)
val enhanced = enhanceCapturedImage(outputMat) val enhanced = enhanceCapturedImage(outputMat)