ARKit 和 ARCore剖析、結構、原理介紹

來源:互聯網
上載者:User

ARKit 和 ARCore 都是三部分:相機姿態估計, 環境感知(平面估計)及光源感知。
ARCore 的部分源碼:https://github.com/google-ar/arcore-unity-sdk/tree/master/Assets/GoogleARCore/SDK;
ARKit API: https://developer.apple.com/documentation/arkit
ARCore API: https://developers.google.com/ar/reference/ -相機姿態估計

Motion Tracking方面都是VIO.
ARKit 是特徵點法,稀疏的點云:https://www.youtube.com/watch?v=rCknUayCsjk

ARKit recognizes notable features in the scene image, tracks differences in the positions of those features across video frames, and compares that information with motion sensing data. —-https://developer.apple.com/documentation/arkit/about_augmented_reality_and_arkit

ARCore 有猜測說是直接法估計的半稠密點雲。但是google自己說是特徵點,應該也是稀疏的了:

ARCore detects visually distinct features in the captured camera image called feature points and uses these points to compute its change in location.
—–https://developers.google.com/ar/discover/concepts -環境感知(平面檢測)

不太瞭解原理,摘了一些原文。
ARKit:

“…can track and place objects on smaller feature points as well…”
“…Use hit-testing methods (see the ARHitTestResult class) to find real-world surfaces corresponding to a point in the camera image….”
“You can use hit-test results or detected planes to place or interact with virtual content in your scene.”
檢測不了垂直面

ARCore:

“ARCore looks for clusters of feature points that appear to lie on common horizontal surfaces…”
“…can also determine each plane’s boundary…”
” …flat surfaces without texture, such as a white desk, may not be detected properly…”
demo視頻:https://www.youtube.com/watch?v=aSKgJEt9l-0 -光源感知

  暫時不瞭解。 ARKit

參考博文:http://blog.csdn.net/u013263917/article/details/72903174
IPhone X添加了TrueDepth camera,也支援 ARKit使用。 一、簡介

ARKit 架構

基於3D情境(SceneKit)實現的增強現實(主流)
基於2D情境(SpriktKit)實現的增強現實

ARKit與SceneKit的關係

ARKit並不是一個獨立就能夠啟動並執行架構,而是必須要SceneKit一起用才可以。 ARKit 實現相機捕捉現實世界映像並恢複三維世界 SceneKit 實現在映像中現實虛擬3D模型

我們focus ARKit 二、ARKit

   ARKit架構中中顯示3D虛擬增強現實的視圖ARSCNView繼承於SceneKit架構中的SCNView,而SCNView又繼承於UIKit架構中的UIView。
   在一個完整的虛擬增強現實體驗中,ARKit架構只負責將真實世界畫面轉變為一個3D情境,這一個轉變的過程主要分為兩個環節:由 ARCamera負責捕捉網路攝影機畫面,由ARSession負責搭建3D情境。
   ARSCNView與ARCamera兩者之間並沒有直接的關係,它們之間是通過AR會話,也就是ARKit架構中非常重量級的一個類ARSession來搭建溝通橋樑的。

   要想運行一個ARSession會話,你必須要指定一個稱之為會話追蹤配置的對象:ARSessionConfiguration, ARSessionConfiguration的主要目的就是負責追蹤相機在3D世界中的位置以及一些特徵情境的捕捉(例如平面捕捉),這個類本身比較簡單卻作用巨大。

ARSessionConfiguration是一個父類,為了更好的看到增強現實的效果,蘋果官方建議我們使用它的子類ARWorldTrackingSessionConfiguration,該類只支援A9晶片之後的機型,也就是iPhone6s之後的機型 2.1. ARWorldTrackingSessionConfiguration 與 ARFrame ARSession搭建溝通橋樑的參與者主要有兩個ARWorldTrackingSessionConfiguration與ARFrame。

ARWorldTrackingSessionConfiguration(會話追蹤配置)的作用是跟蹤裝置的方向和位置,以及檢測裝置網路攝影機看到的現實世界的表面。它的內部實現了一系列非常龐大的演算法計算以及調用了你的iPhone必要的感應器來檢測手機的移動及旋轉甚至是翻滾。

ARWorldTrackingSessionConfiguration 裡面就是VIO系統
這裡文中提到的ARWorldTrackingSessionConfiguration在最新的iOS 11 beta8中已被廢棄,因此以下更改為ARWorldTrackingConfiguration

當ARWorldTrackingSessionConfiguration計算出相機在3D世界中的位置時,它本身並不持有這個位置資料,而是將其計算出的位置資料交給ARSession去管理(與前面說的session管理記憶體相呼應),而相機的位置資料對應的類就是ARFrame
ARSession類一個屬性叫做currentFrame,維護的就是ARFrame這個對象 ARCamera只負責捕捉映像,不參與資料的處理。它屬於3D情境中的一個環節,每一個3D Scene都會有一個Camera,它覺得了我們看物體的視野。
2.2. ARSession

ARSession擷取相機位置資料主要有兩種方式

第一種:push。 即時不斷的擷取相機位置,由ARSession主動告知使用者。通過實現ARSession的代理- (void)session:(ARSession )session didUpdateFrame:(ARFrame )frame來擷取
第二種:pull。 使用者想要時,主動去擷取。ARSession的屬性currentFrame來擷取 2.3. ARKit工作完整流程 ARSCNView載入情境SCNScene SCNScene啟動相機ARCamera開始捕捉情境 捕捉情境後ARSCNView開始將情境資料交給Session Session通過管理ARSessionConfiguration實現情境的追蹤並且返回一個ARFrame 給ARSCNView的scene添加一個子節點(3D物體模型)

ARSessionConfiguration捕捉相機3D位置的意義就在於能夠在添加3D物體模型的時候計算出3D物體模型相對於相機的真實的矩陣位置

三、API分析

-AROrientationTrackingConfiguration
tracks the device’s movement with three degrees of freedom (3DOF): specifically, the three rotation axes;只跟蹤三個,不如下面的這個。
-[ARWorldTrackingConfiguration](https://developer.apple.com/documentation/arkit/arworldtrackingconfiguration)
 負責跟蹤相機,檢測平面。tracks the device’s movement with six degrees of freedom (6DOF)。
完成slam工作的主要內容應該就是在這個裡面。
但啟動一個最簡單的AR, 只需要:

let configuration = ARWorldTrackingConfiguration()configuration.planeDetection = .horizontalsceneView.session.run(configuration)

具體的實現被封裝了。
然後與重要的ARSession類和ARSCNView的介面:
由於介面很多,只列部分,詳細的轉官網or參考博文:http://blog.csdn.net/u013263917/article/category/6959089
蘋果有個自己的特色功能,Face-based AR experience。可以使用TrueDepth camera追蹤人臉的表情,pose, topology等。world based AR experience 對應其它廠商的sdk,比如ARCore。
比如一些常用的資訊介面:
虛擬物體的位置,ARFrame–>ARAnchor中有transform
相機的位置,ARFrame–>ARCamera 中displayTransform包含位姿
視圖矩陣,投影矩陣:ARFrame–>ARCamera 中有projectMatrix,viewMatrix
-ARSession

Tables cols cols
Configuring and Running a Session func run(ARConfiguration, options: ARSession.RunOptions =) Starts AR processing for the session with the specified configuration and options.
var configuration: ARConfiguration An object that defines motion and scene tracking behaviors for the session.
Responding to AR Updates var delegate: ARSessionDelegate An object you provide to receive captured video images and tracking information, or to respond to changes in session status.
protocol ARSessionDelegate Methods you can implement to receive captured video frame images and tracking state from an AR session.
Displaying and Interacting with AR Content var currentFrame: ARFrame
func add(anchor: ARAnchor) Adds the specified anchor to be tracked by the session.

-ARSCNView
ARSCNView即使用者見到的介面。

Tables cols cols
var session: ARSession The AR session that manages motion tracking and camera image processing for the view’s contents.
var scene: SCNScene The SceneKit scene to be displayed in the view.
Responding to AR Updates protocol ARSCNViewDelegate Methods you can implement to mediate the automatic synchronization of SceneKit content with an AR session.
func hitTest(CGPoint, types: ARHitTestResult.ResultType) Searches for real-world objects or AR anchors in the captured camera image corresponding to a point in the SceneKit view.

-ARCamera
ARCamera類裡有很多相關的Topics:

Tables cols cols
Handling Tracking Status trackingState The general quality of position tracking available when the camera captured a frame.
ARTrackingState Possible values for position tracking quality.
Examining Imaging Parameters imageResolution The width and height, in pixels, of the captured camera image.
Applying Camera Geometry projectionMatrix A transform matrix appropriate for rendering 3D content to match the image captured by the camera.
projectionMatrixForOrientation: Returns a transform matrix appropriate for rendering 3D content to match the image captured by the camera, using the specified parameters.
viewMatrixForOrientation: Returns a transform matrix for converting from world space to camera space.

-ARFrame
ARFrame 類裡的Topics 可以看到slam輸入輸出介面

Tables cols cols
Accessing Captured Video Frames capturedImage A pixel buffer containing the image captured by the camera.
capturedDepthData The depth map, if any, captured along with the video frame.
Examining Scene Parameters camera Information about the camera position, orientation, and imaging parameters used to capture the frame.
lightEstimate An estimate of lighting conditions based on the camera image.
displayTransformForOrientation: Returns an affine transform for converting between normalized image coordinates and a coordinate space appropriate for rendering the camera image onscreen.
Tracking and Finding Objects anchors The list of anchors representing positions tracked or objects detected in the scene.
hitTest:types: Searches for real-world objects or AR anchors in the captured camera image.
Debugging Scene Detection rawFeaturePoints The current intermediate results of the scene analysis ARKit uses to perform world tracking.
ARPointCloud A collection of points in the world coordinate space of the AR session.

關於特徵點通過ARPointCloud可以看到特徵點的個數和identitiers;
究竟是用的什麼特徵點。可能需要看下identitiers的維數等資訊。SIFT 特徵的descriptor是128維。

-ARLightEstimate
ARCore

google 發布了對應 Android studio、 Unity、Unreal以及Web的環境的ARCore。
API 官網:https://developers.google.com/ar/reference/。
SDK on Github :https://github.com/google-ar;
2017.12.15更新:google 添加了java ,C平台的支援。(原來的Android Studio部分放到了Java裡)
ARCore的結構比較靈活,
AR中,在一個真實情境中繪製一個虛擬物體,在繪製之前,開發人員所必需知道的資訊:

虛擬物體的位置姿態
視圖矩陣,投影矩陣
虛擬物體的光照資訊

這些資訊都可以從ARCore中得到。我們先看google提供的java平台裡的Sample。
先理一下Sample裡的繪製邏輯,關於繪製的類有:
BackgroundRenderer用於繪製網路攝影機採集到的資料。
VirtualObject用於繪製Android小機器人。
VirtualObjectShadow用於給Android機器人繪製陰影。
PlaneRenderer用於繪製SDK識別出來的平面。
PointCloud用於繪製SDK識別出來的點雲。
採用的是Opengl ES繪製:配置GLSurfaceView,實現GLSurfaceView.Renderer介面。關於繪製的部分都可以替換,可以用一些更現代化的3D圖形架構等。

提一下Renderer介面;
`public interface Renderer {
void onSurfaceCreated(GL10 gl, EGLConfig config);
void onSurfaceChanged(GL10 gl, int width, int height);
void onDrawFrame(GL10 gl);
}
onSurfaceCreated這個方法在可繪製表面建立或重新建立的時候被調用。在這個回調裡,可以做一些初始化的事情。注意,此方法運行在OpenGL線程中,具有OpenGL上下文,因此這裡可以進行執行OpenGL調用。
onSurfaceChanged 這個方法在可繪製表面發生變化的時候被調用。此時外部可能改變了控制項的大小,因此我們需要在這個調用裡更新我們的視口資訊,以便繪製的時候能準確繪製到螢幕中來。
void onDrawFrame 核心方法。在繪製的時候調用。每繪製一次,就會調用一次,即每一幀觸發一次。這裡是主要的繪製邏輯。
參考:https://juejin.im/post/59ac1f2bf265da249517ac72
具體的實現見Sample吧。

擷取虛擬物體的POSE,ViewMatrix 和ProjectMatrix以及光照這些必要資訊的具體介面:
我們先看java平台。https://developers.google.com/ar/reference/java/com/google/ar/core/package-summary 虛擬物體的Pose:

ARCore規定,當你想放置的虛擬對象,你需要定義一個 錨Anchor,以確保ARCORE跟蹤對象的位置隨著時間的推移。
Anchor類

getPose就可以得到Anchor的當前位置。Sample 裡調用 anchor.getPose().toMatrix(mAnchorMatrix, 0);得到了Anchor的位置矩陣,一個4x4 model-to-world transformation matrix, stored in column-major order。 視圖矩陣和投影矩陣:

Camera類儲存了這些資訊:

Camera的屬性會隨著Session.update() is called update. 所以我們再看下Session類如何更新Camera。

Session類,Session類管理著AR 系統的狀態, Session is the main entry point to ARCore API.

最後的update() 更新ARCore系統的狀態,包括得到一個新的camera frame,更新device的位置,更新Anchor的位置,更新檢測到的平面。新的camera 屬性通過frame的getCamera()得到。
Frame類

hitTest是google的點擊測試介面,檢測使用者是否點擊到平面,是否載入虛擬物體。

光照暫時不關心。

C平台與Java 大同小異。
Session類:

Frame類,包括了Java中的Frame,Camera,HitResult,光照等類:

Anchor包括在Trackable類裡:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.