【问题标题】:Draw rectangle using boundingBox使用boundingBox绘制矩形
【发布时间】:2020-11-25 14:19:40
【问题描述】:

通过 VNRecognizeTextRequest,我从特定的边界框获得以下坐标:(0.21611927830895714, 0.4163079471243136, 0.017705895179925962, 0.1368724813140948)。

我的问题是如何使用这些 boundingBox 坐标在 UIImageView 上绘制一个矩形?

【问题讨论】:

    标签: ios swift vision


    【解决方案1】:

    要从标准化转换为图像坐标,您可以使用以下方法

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-20
      • 2011-02-17
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-09
      • 1970-01-01
      相关资源
      最近更新 更多