ios - ARKit 2 - 裁剪识别图像

标签 ios swift arkit

所以,我的目标是:

  1. 查找已知图像

    guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return }
    
    let configuration = ARWorldTrackingConfiguration()
    configuration.detectionImages = referenceImages
    

.

  • 从 sceneView 中拍摄快照

     func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    
           let image = sceneView.snapshot()  //  or else self.sceneView.session.currentFrame?.capturedImage
    
  • 我需要从 SceneView 中提取图像。

  • enter image description here

    这是我的代码, 我正在使用 renderer.projectPoint 方法。

      guard let imageAnchor = anchor as? ARImageAnchor else {
                return
            }
            let posx = anchorTr.x// fnode.convertPosition(node.position, to: node).x //self.positionFromTransform(node.simdTransform).x// node.simdTransform.columns.3.x
            let posy = anchorTr.y//fnode.convertPosition(node.position, to: node).y //self.positionFromTransform(node.simdTransform).y//node.simdTransform.columns.3.y
            let posz = anchorTr.z //node.simdTransform.columns.3.z
    
    
                      let width = referenceImage.physicalSize.width
            let height = referenceImage.physicalSize.height
    
    
            let topLeftPosition = SCNVector3(posx - Float(width / 2), posy - Float(height / 2), posz )
            let topRightPosition = SCNVector3(posx + Float(width / 2), posy - Float(height / 2), posz )
            let bottomLeftPosition = SCNVector3(posx - Float(width / 2), posy + Float(height / 2), posz )
            let bottomRightPosition = SCNVector3(posx + Float(width / 2), posy  + Float(height / 2), posz)
    
    
            let topLeftProjection = renderer.projectPoint(topLeftPosition)
            let topRightProjection = '''
            let bottomLeftProjection = '''
            let bottomRightProjection = '''
    
            let topLeft = CGPoint(x: CGFloat(topLeftProjection.x), y: CGFloat( topLeftProjection.y))
            let topRight = ''
            let bottomLeft = ''''''
            let bottomRight = '''
    
    
      let points = [topLeft, topRight, bottomRight, bottomLeft]
    
       self.drawPolygon(points, color:.green)
    

    但是这个方法似乎取决于手机的位置。

    enter image description here

    enter image description here

    最佳答案

    就我而言,我通过对每个引用点执行此操作来解决该问题。

    let transform = node.simdConvertTransform(node.simdTransform, to: node.parent!)
    
    let x = transform.columns.3.x
    let y = transform.columns.3.y
    let z = transform.columns.3.z
    
    let position = SCNVector3(x, y, z)
    let projection = renderer.projectPoint(position)
    
    let screenPoint =  CGPoint(x: CGFloat(projection.x), y: CGFloat( projection.y))
    

    关于ios - ARKit 2 - 裁剪识别图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53453203/

    相关文章:

    rotation - 获取 ARKit pointOfView 的 Y 旋转

    ios - ARKit - 对象未放置

    iphone - 如何在 iPhone sdk 中将 HTML 页面附加到我的邮件编辑器?

    objective-c - CoreData 按创建顺序排序

    swift - 在 AFNetworking swift 的 header 响应中获取 token 的值

    ios - 布局约束不起作用

    iOS : how to shrink a UIView. layer.shadow 到所需的宽度?

    ios - PushNotification OneSignal 问题

    macos - 设置窗口级别 Swift 错误 OSX

    swift - ARKit – Transform Matrix 中的不同列代表什么?