ARKit图像检测方向

时间:2018-03-01 10:43:32

标签: 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)
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

   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

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

答案 1 :(得分:0)

在同时使用垂直和水平平面检测的ARKit应用中,我们可以使用属性ARPlaneAnchor.Alignment获得平面方向。