【发布时间】:2020-11-25 14:19:40
【问题描述】:
通过 VNRecognizeTextRequest,我从特定的边界框获得以下坐标:(0.21611927830895714, 0.4163079471243136, 0.017705895179925962, 0.1368724813140948)。
我的问题是如何使用这些 boundingBox 坐标在 UIImageView 上绘制一个矩形?
【问题讨论】:
通过 VNRecognizeTextRequest,我从特定的边界框获得以下坐标:(0.21611927830895714, 0.4163079471243136, 0.017705895179925962, 0.1368724813140948)。
我的问题是如何使用这些 boundingBox 坐标在 UIImageView 上绘制一个矩形?
【问题讨论】:
要从标准化转换为图像坐标,您可以使用以下方法
let rectInImage = VNImageRectForNormalizedRect(boundingBox, image.size.width, image.size.height)
从那里您可以使用 UIBezierPath 来绘制路径:
func drawRect(_ rect: CGRect, layer: CALayer) {
let center = CGPoint(x: rect.midX, y: rect.midY)
let path: UIBezierPath = UIBezierPath(rect: rect)
let rectShape: CAShapeLayer = CAShapeLayer()
rectShape.path = path.cgPath
rectShape.position = center
rectShape.bounds = rect
rectShape.strokeColor = UIColor.green.cgColor
rectShape.fillColor = UIColor.clear.cgColor
rectShape.lineWidth = 1.0
layer.addSublayer(rectShape)
}
【讨论】: