ARKit 与 ARCore比对(三)
ARKit 和 ARCore剖析、結構、原理介紹
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 (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(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.”“...可以在較小的特征點上跟蹤和放置對象...”
“...使用命中測試方法(請參閱ARHitTestResult類)來查找與相機圖像中的點相對應的真實世界曲面...”。
“您可以使用命中測試結果或檢測到的飛機來放置或與場景中的虛擬內容交互。”
檢測不了垂直面檢測不了垂直面
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…”“ARCore尋找似乎位于普通水平表面上的特征點集群...”
“...還可以確定每架平面的邊界...”
“......沒有紋理的平坦表面,如白色書桌,可能無法正確檢測到......”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 = .horizontal
sceneView.session.run(configuration) - 1
- 2
- 3
具體的實現被封裝了。
然后與重要的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類里:
?
?
?
總結
以上是生活随笔為你收集整理的ARKit 与 ARCore比对(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做四维彩超多少钱啊?
- 下一篇: ubuntu16.04 下安装Openc