ARKit 和 ARCore原理介紹(轉)

weixin_34120274發表於2017-09-27

轉自:http://blog.csdn.net/qq_21158525/article/details/78052644?locationNum=8&fps=1

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 calledfeature pointsand 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 theARHitTestResultclass) to find real-world surfaces corresponding to a point in the camera image….”

“You can use hit-test results ordetected planesto 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…”

沒有解決遮擋: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來搭建溝通橋樑的。

6271687-d5b0e534ef0d7aaa.png

要想執行一個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,它覺得了我們看物體的視野。

6271687-b5423c42f13419df.png

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物體模型相對於相機的真實的矩陣位置

6271687-5b2cea6f7131d32e.png

三、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)

1

2

3

具體的實現還是被封裝了。。。

-ARCamera

ARCamera類裡有很多相關的Topics:

Tablescolscols

Handling Tracking StatustrackingStateThe general quality of position tracking available when the camera captured a frame.

ARTrackingStatePossible values for position tracking quality.

trackingStateReasonA possible diagnosis for limited position tracking quality as of when the camera captured a frame.

ARTrackingStateReasonPossible causes for limited position tracking quality.

Examining Camera GeometrytransformThe position and orientation of the camera in world coordinate space.

eulerAnglesThe orientation of the camera, expressed as roll, pitch, and yaw values.

Examining Imaging ParametersimageResolutionThe width and height, in pixels, of the captured camera image.

intrinsicsA matrix that converts between the 2D camera plane and 3D world coordinate space.

Applying Camera GeometryprojectionMatrixA 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.

projectPoint:orientation:viewportSize:Returns the projection of a point from the 3D world space detected by ARKit into the 2D space of a view rendering the scene.

-ARFrame

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

Tablescolscols

Accessing Captured Video FramescapturedImageA pixel buffer containing the image captured by the camera.

timestampThe time at which the frame was captured.

capturedDepthDataThe depth map, if any, captured along with the video frame.

capturedDepthDataTimestampThe time at which depth data for the frame (if any) was captured.

Examining Scene ParameterscameraInformation about the camera position, orientation, and imaging parameters used to capture the frame.

lightEstimateAn 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 ObjectsanchorsThe 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 DetectionrawFeaturePointsThe current intermediate results of the scene analysis ARKit uses to perform world tracking.

ARPointCloudA 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,我們只看Unity。

API 官網:https://developers.google.com/ar/reference/unity/

SDK on Github :https://github.com/google-ar/arcore-unity-sdk;

相關文章