iOS还原相机投影

Gui*_*uig 87 camera augmented-reality ios scenekit arkit

我正在尝试估算与太空中QR码相关的设备位置.我正在使用ARKit和Vision框架,两者都在iOS11中引入,但这个问题的答案可能并不依赖于它们.

使用Vision框架,我能够获得在相机框架中限制QR码的矩形.我想将此矩形与从标准位置转换QR码所需的设备平移和旋转相匹配.

例如,如果我观察到框架:

*            *

    B
          C
  A
       D


*            *
Run Code Online (Sandbox Code Playgroud)

如果我距离QR码1米远,以它为中心,假设QR码的边长为10cm,我会看到:

*            *


    A0  B0

    D0  C0


*            *
Run Code Online (Sandbox Code Playgroud)

那两个帧之间的设备转换是什么?我知道确切的结果可能是不可能的,因为观察到的QR码可能是略微非平面的,我们试图估计一个不完美的东西上的仿射变换.

我想这sceneView.pointOfView?.camera?.projectionTransformsceneView.pointOfView?.camera?.projectionTransform?.camera.projectionMatrix后者已经考虑了从ARKit推断的变换更有帮助我对此问题不感兴趣.

我该怎么做

func get transform(
  qrCodeRectangle: VNBarcodeObservation,
  cameraTransform: SCNMatrix4) {
  // qrCodeRectangle.topLeft etc is the position in [0, 1] * [0, 1] of A0

  // expected real world position of the QR code in a referential coordinate system
  let a0 = SCNVector3(x: -0.05, y: 0.05, z: 1)
  let b0 = SCNVector3(x: 0.05, y: 0.05, z: 1)
  let c0 = SCNVector3(x: 0.05, y: -0.05, z: 1)
  let d0 = SCNVector3(x: -0.05, y: -0.05, z: 1)

  let A0, B0, C0, D0 = ?? // CGPoints representing position in
                          // camera frame for camera in 0, 0, 0 facing Z+

  // then get transform from 0, 0, 0 to current position/rotation that sees
  // a0, b0, c0, d0 through the camera as qrCodeRectangle 
}
Run Code Online (Sandbox Code Playgroud)

==== ====编辑

在尝试了很多东西之后,我最终使用openCV投影和透视解算器进行相机姿态估计.solvePnP这给了我一个旋转和平移,它应该代表QR码参考中的相机姿态.然而,当使用这些值并放置对应于逆变换的对象时,QR码应该在相机空间中,我得到不准确的移位值,并且我无法使旋转起作用:

// some flavor of pseudo code below
func renderer(_ sender: SCNSceneRenderer, updateAtTime time: TimeInterval) {
  guard let currentFrame = sceneView.session.currentFrame, let pov = sceneView.pointOfView else { return }
  let intrisics = currentFrame.camera.intrinsics
  let QRCornerCoordinatesInQRRef = [(-0.05, -0.05, 0), (0.05, -0.05, 0), (-0.05, 0.05, 0), (0.05, 0.05, 0)]

  // uses VNDetectBarcodesRequest to find a QR code and returns a bounding rectangle
  guard let qr = findQRCode(in: currentFrame) else { return }

  let imageSize = CGSize(
    width: CVPixelBufferGetWidth(currentFrame.capturedImage),
    height: CVPixelBufferGetHeight(currentFrame.capturedImage)
  )

  let observations = [
    qr.bottomLeft,
    qr.bottomRight,
    qr.topLeft,
    qr.topRight,
  ].map({ (imageSize.height * (1 - $0.y), imageSize.width * $0.x) })
  // image and SceneKit coordinated are not the same
  // replacing this by:
  // (imageSize.height * (1.35 - $0.y), imageSize.width * ($0.x - 0.2))
  // weirdly fixes an issue, see below

  let rotation, translation = openCV.solvePnP(QRCornerCoordinatesInQRRef, observations, intrisics)
  // calls openCV solvePnP and get the results

  let positionInCameraRef = -rotation.inverted * translation
  let node = SCNNode(geometry: someGeometry)
  pov.addChildNode(node)
  node.position = translation
  node.orientation = rotation.asQuaternion
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

在此输入图像描述

其中A,B,C,D是它们传递给程序的顺序中的QR码角.

当手机旋转时,预测的原点保持不变,但它会从应有的位置移动.令人惊讶的是,如果我移动观察值,我可以纠正这个:

  // (imageSize.height * (1 - $0.y), imageSize.width * $0.x)
  // replaced by:
  (imageSize.height * (1.35 - $0.y), imageSize.width * ($0.x - 0.2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

现在预测的起源保持稳健.但是我不明白班次值的来源.

最后,我试图获得相对于QR码参考的方向:

    var n = SCNNode(geometry: redGeometry)
    node.addChildNode(n)
    n.position = SCNVector3(0.1, 0, 0)
    n = SCNNode(geometry: blueGeometry)
    node.addChildNode(n)
    n.position = SCNVector3(0, 0.1, 0)
    n = SCNNode(geometry: greenGeometry)
    node.addChildNode(n)
    n.position = SCNVector3(0, 0, 0.1)
Run Code Online (Sandbox Code Playgroud)

当我直接查看二维码时,方向很好,但随后它会转移一些似乎与手机旋转有关的东西:在此输入图像描述

我遇到的突出问题是:

  • 如何解决旋转问题?
  • 位置偏移值来自何处?
  • 什么简单的关系做旋转,翻译,QRCornerCoordinatesInQRRef,观察,内在验证?是O~K ^ -1*(R_3x2 | T)Q?因为如果是这样的话会减少几个数量级.

如果这有用,这里有一些数值:

Intrisics matrix
Mat 3x3
1090.318, 0.000, 618.661
0.000, 1090.318, 359.616
0.000, 0.000, 1.000

imageSize
1280.0, 720.0
screenSize
414.0, 736.0
Run Code Online (Sandbox Code Playgroud)

==== Edit2 ====

我注意到当手机水平平行于QR码时旋转工作正常(即旋转矩阵是[[a,0,b],[0,1,0],[c,0,d]] ),无论实际的QR码方向是什么:

在此输入图像描述

其他旋转不起作用.

小智 1

数学(三角):

方程

注:下角为l(二维码长度),左角为k,上角为i(摄像头)

图片