目标检测:解决实时预览中Bounding Box偏移问题
实时目标检测Bounding Box坐标偏移问题修复
需求说明
- 实现实时预览中为多个检测目标绘制Bounding Box
- 代码简洁优雅、易于理解
- 明确背后的实现逻辑
当前问题
预览图与输入图尺寸不同,坐标转换后绘制的Bounding Box严重偏移,与实际目标位置不符。
现有代码
override fun draw(canvas: Canvas) { super.draw(canvas) // Get the dimensions of the preview image val previewWidth = previewImageSize.width val previewHeight = previewImageSize.height // Get the dimensions of the input image val inputWidth = inputImageSize.width val inputHeight = inputImageSize.height // Calculate the scaling factor for each dimension val scaleX: Float = previewWidth.toFloat() / inputWidth.toFloat() val scaleY: Float = previewHeight.toFloat() / inputHeight.toFloat() for (result in results) { val boundingBox = result.boundingBox val left = boundingBox.left * scaleX val top = boundingBox.top * scaleY val right = boundingBox.right * scaleX val bottom = boundingBox.bottom * scaleY // Draw bounding box around detected objects val drawableRect = RectF(left, top, right, bottom) canvas.drawRect(drawableRect, boxPaint) // Create text to display alongside detected objects val drawableText = result.categories[0].label + " " + String.format("%.2f", result.categories[0].score) // Draw rect behind display text textBackgroundPaint.getTextBounds(drawableText, 0, drawableText.length, bounds) val textWidth = bounds.width() val textHeight = bounds.height() canvas.drawRect( left, top, left + textWidth + BOUNDING_RECT_TEXT_PADDING, top + textHeight + BOUNDING_RECT_TEXT_PADDING, textBackgroundPaint ) // Draw text for detected object canvas.drawText(drawableText, left, top + bounds.height(), textPaint) } }
补充信息
- 输入图像尺寸:320x320(由预览图缩放后传入模型)
- 预览尺寸:1857x1080(1857为高度)
- CameraX获取的ImageAnalysis图像尺寸:640x480
- 计算得到的缩放因子:scaleX=3.375,scaleY=5.803125
内容的提问来源于stack exchange,提问作者MaaAn13
相关产品推荐
相关产品推荐

