swift - ARKit 图像检测方向

标签 swift arkit

Apple 已经实现了带有一些附加功能的 ARKit beta 1.5。 (垂直平面检测,图像检测,...)

我是做图像检测的,想知道检测图像的时候如何获取方向信息? (垂直/水平图像检测)

获取此信息的唯一方法是使用 ARPlaneAnchor.Alignment

在 Apple 示例项目中,他们假设图像在其局部空间中是水平的。

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let imageAnchor = anchor as? ARImageAnchor else { return }
    let referenceImage = imageAnchor.referenceImage
    updateQueue.async {

        // Create a plane to visualize the initial position of the detected image.
        let plane = SCNPlane(width: referenceImage.physicalSize.width,
                             height: referenceImage.physicalSize.height)
        let planeNode = SCNNode(geometry: plane)
        planeNode.opacity = 0.25

        /*
         `SCNPlane` is vertically oriented in its local coordinate space, but
         `ARImageAnchor` assumes the image is horizontal in its local space, so
         rotate the plane to match.
         */
        planeNode.eulerAngles.x = -.pi / 2

        /*
         Image anchors are not tracked after initial detection, so create an
         animation that limits the duration for which the plane visualization appears.
         */
        planeNode.runAction(self.imageHighlightAction)

        // Add the plane visualization to the scene.
        node.addChildNode(planeNode)
    }
}

最佳答案

我不完全确定这是否正确,但希望这对您有所帮助,或者至少让您入门。

我认为您需要查看从相机中获取数据以帮助解决此问题。例如:

   func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {

    /*
    Get The Pitch (Rotation Around X Axis)
    Get The Yaw (Rotation Around Y Axis)
    Get The Roll (Rotation Around Z Axis)
    */
    guard let pitch = self.augmentedRealityView.session.currentFrame?.camera.eulerAngles.x,
          let yaw =   self.augmentedRealityView.session.currentFrame?.camera.eulerAngles.y,
          let roll = self.augmentedRealityView.session.currentFrame?.camera.eulerAngles.z else { return }

    print("""
     Pitch = \(degreesFrom(pitch))
     Yaw = \(degreesFrom(yaw))
     Roll = \(degreesFrom(roll))
    """)
}


/// Convert Radians To Degrees
///
/// - Parameter radian: Float
/// - Returns: Float
func degreesFrom( _ radian: Float) -> Float{

    return radian * Float(180.0 / Double.pi)

}

enter image description here

然后您可以(大致)确定设备是处于水平位置还是垂直位置。

关于swift - ARKit 图像检测方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49047912/

相关文章:

swift - 从贝塞尔路径计算点

ios - 如何从行索引 Swift 获取索引路径

swift - 将 Realm 与 A​​mazon DynamoDB 结合使用

ios - 如何将 SKScene 添加到 SCNNode 平面?

Swift区分几个同名的函数用途

ios - 如何从不同的函数检索 Realm 对象? swift

ios - GameplayKit 中的 GKS 文件是什么?

ios - ARKit - 相对于世界原点在世界中定位一个节点

ios - 如何在不改变世界坐标中的相对位置、大小和比例的情况下将 SCNNode 作为子节点添加到父节点

swift - 无法使用 RealityKit 场景预览 SwiftUI Canvas