ใช้ความลึกของข้อมูลดิบในแอป Android
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Raw Depth API มีข้อมูลความลึกสำหรับรูปภาพจากกล้องที่มีความแม่นยำสูงกว่าข้อมูล Depth API แบบเต็ม แต่ไม่ได้ครอบคลุมทุกพิกเซลเสมอไป รูปภาพความลึกแบบดิบ รวมถึงรูปภาพความเชื่อมั่นที่ตรงกันยังสามารถประมวลผลเพิ่มเติมได้ ซึ่งช่วยให้แอปใช้ได้เฉพาะข้อมูลความลึกที่มีความแม่นยำเพียงพอสำหรับกรณีการใช้งานแต่ละรายการ
ความเข้ากันได้ของอุปกรณ์
ความลึกของไฟล์ข้อมูล RAW พร้อมใช้งานในอุปกรณ์ทั้งหมดที่รองรับ Depth API ส่วน Raw Depth API จะเหมือนกับ Depth API แบบเต็ม ไม่จำเป็นต้องใช้เซ็นเซอร์ตรวจจับความลึกของฮาร์ดแวร์ที่รองรับ เช่น เซ็นเซอร์ตรวจจับช่วงเวลาแสดงโฆษณา (ToF) อย่างไรก็ตาม ทั้ง Raw Depth API และ Depth API เต็มรูปแบบจะใช้เซ็นเซอร์ของฮาร์ดแวร์ที่รองรับซึ่งอุปกรณ์อาจมี
Raw Depth API เทียบกับ Depth API แบบเต็ม
Raw Depth API ให้ค่าประมาณความลึกที่มีความแม่นยำสูงกว่า แต่รูปภาพความลึกแบบข้อมูลดิบอาจไม่รวมค่าประมาณความลึกของพิกเซลทั้งหมดในรูปภาพจากกล้อง ในทางตรงกันข้าม API ความลึกแบบเต็มจะให้ความลึกโดยประมาณสำหรับทุกพิกเซล แต่ข้อมูลความลึกต่อพิกเซลอาจมีความแม่นยำน้อยลงเนื่องจากการปรับให้ความลึกและการประมาณค่าความลึกสอดคล้องกัน รูปแบบและขนาดของรูปภาพความลึกเหมือนกันใน API ทั้ง 2 รายการ เฉพาะเนื้อหาที่ต่างออกไป
ตารางต่อไปนี้แสดงความแตกต่างระหว่าง Raw Depth API กับ Depth API แบบเต็มโดยใช้รูปภาพเก้าอี้และโต๊ะในห้องครัว
API |
การคืนสินค้า |
รูปภาพจากกล้อง |
รูปภาพความลึก |
รูปภาพความเชื่อมั่น |
API ความลึกของไฟล์ข้อมูล RAW
|
- รูปภาพแบบ RAW ที่มีค่าประมาณความลึกที่แม่นยำมากสำหรับพิกเซลบางส่วน (ไม่ใช่ทั้งหมด) ในรูปภาพจากกล้อง
- รูปภาพแสดงความมั่นใจที่ให้ความมั่นใจกับพิกเซลรูปภาพที่มีความลึกแบบ RAW ทุกพิกเซล จำนวนพิกเซลรูปภาพจากกล้องที่ไม่มีค่าประมาณความลึกมีค่าความเชื่อมั่นเป็น 0
|
|
|
|
API ความลึกเต็ม
|
- "เรียบ" รายการเดียว รูปภาพที่มีความลึกโดยประมาณของทุกพิกเซล
- ไม่มีรูปภาพความเชื่อมั่นที่มาพร้อมกับ API นี้
|
|
|
ไม่มี
|
รูปภาพแสดงความมั่นใจ
ในรูปภาพความเชื่อมั่นที่แสดงผลโดย Raw Depth API ค่าความเชื่อมั่นที่มีสีอ่อนกว่าจะมีพิกเซลสีขาวแสดงถึงความเชื่อมั่นแบบเต็ม และพิกเซลสีดำแสดงถึงไม่มีความเชื่อมั่น โดยทั่วไป พื้นที่ในรูปภาพจากกล้องที่มีพื้นผิวมากกว่า เช่น ต้นไม้ จะมีความมั่นใจในความลึกของข้อมูลดิบสูงกว่าบริเวณที่ไม่มีพื้นผิว เช่น ผนังว่างเปล่า พื้นผิวที่ไม่มีพื้นผิวมักจะให้ค่าความเชื่อมั่นเป็น 0
หากอุปกรณ์เป้าหมายมีเซ็นเซอร์ความลึกของฮาร์ดแวร์ที่รองรับ ความมั่นใจว่าบริเวณต่างๆ ของภาพอยู่ใกล้กับกล้องมากพอก็จะมีแนวโน้มสูงขึ้น แม้จะวางบนพื้นผิวที่ไม่มีพื้นผิวก็ตาม
ต้นทุนการประมวลผล
ต้นทุนการประมวลผลของ Raw Depth API คิดเป็นประมาณครึ่งหนึ่งของต้นทุนการประมวลผลสำหรับ Depth API ทั้งหมด
กรณีการใช้งาน
เมื่อใช้ Raw Depth API คุณจะรับภาพที่มีความลึกซึ่งนำเสนอรายละเอียดเพิ่มเติมของรูปทรงเรขาคณิตของวัตถุในฉากได้ ข้อมูลความลึกแบบดิบจะเป็นประโยชน์ในการสร้างประสบการณ์ AR ที่จำเป็นต้องมีความแม่นยำและรายละเอียดของความลึกเพิ่มขึ้นสำหรับงานทำความเข้าใจเรขาคณิต ตัวอย่างกรณีการใช้งานมีดังนี้
- การสร้าง 3 มิติใหม่
- การวัดผล
- การตรวจจับรูปร่าง
ข้อกำหนดเบื้องต้น
ตรวจสอบว่าคุณเข้าใจแนวคิด AR พื้นฐาน
และวิธีกําหนดค่าเซสชัน ARCore ก่อนดำเนินการต่อ
เปิดใช้ความลึก
ในเซสชัน ARCore ใหม่ ให้ตรวจสอบว่าอุปกรณ์ของผู้ใช้รองรับความลึกหรือไม่ อุปกรณ์ที่เข้ากันได้กับ ARCore บางรุ่นอาจไม่รองรับ Depth API เนื่องจากข้อจำกัดด้านพลังงานในการประมวลผล เพื่อประหยัดทรัพยากร ระบบจะปิดใช้ความลึกใน ARCore ตามค่าเริ่มต้น เปิดใช้โหมดความลึกเพื่อให้แอปใช้ Depth API
Java
Config config = session.getConfig();
// Check whether the user's device supports Depth.
if (session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)) {
// Enable depth mode.
config.setDepthMode(Config.DepthMode.AUTOMATIC);
}
session.configure(config);
Kotlin
if (session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)) {
session.configure(session.config.apply { depthMode = Config.DepthMode.AUTOMATIC })
}
รับรูปภาพความลึกและความเชื่อมั่นแบบข้อมูลดิบล่าสุด
เรียกใช้ frame.acquireRawDepthImage16Bits()
เพื่อรับรูปภาพความลึกซึ่งเป็นข้อมูลดิบล่าสุด พิกเซลรูปภาพบางส่วนที่แสดงผลผ่าน Raw Depth API จะมีข้อมูลความลึก และเฟรม ARCore บางเฟรมจะไม่มีรูปภาพความลึกแบบ RAW ใหม่ หากต้องการระบุว่ารูปภาพความลึกของข้อมูลดิบสำหรับเฟรมปัจจุบันเป็นรูปภาพใหม่ ให้เปรียบเทียบการประทับเวลากับการประทับเวลาของรูปภาพความลึกแบบ RAW ก่อนหน้า หากการประทับเวลาแตกต่างกัน รูปภาพความลึกแบบข้อมูลดิบจะอิงตามข้อมูลความลึกใหม่ มิเช่นนั้น รูปภาพความลึกจะเป็นเส้นโครงของข้อมูลความลึกก่อนหน้าซ้ำอีกครั้ง
โทรหา frame.acquireRawDepthConfidenceImage()
เพื่อรับรูปภาพความเชื่อมั่น คุณใช้รูปภาพความเชื่อมั่นเพื่อตรวจสอบความแม่นยำของพิกเซลความลึกแบบ RAW แต่ละรายการได้ ระบบจะส่งรูปภาพแสดงความมั่นใจในรูปแบบ Y8 แต่ละพิกเซลเป็นจำนวนเต็ม 8 บิตที่ไม่มีเครื่องหมาย 0
หมายถึงความเชื่อมั่นน้อยที่สุด ขณะที่ 255
หมายถึงค่าสูงสุด
Java
// Use try-with-resources, so that images are released automatically.
try (
// Depth image is in uint16, at GPU aspect ratio, in native orientation.
Image rawDepth = frame.acquireRawDepthImage16Bits();
// Confidence image is in uint8, matching the depth image size.
Image rawDepthConfidence = frame.acquireRawDepthConfidenceImage(); ) {
// Compare timestamps to determine whether depth is is based on new
// depth data, or is a reprojection based on device movement.
boolean thisFrameHasNewDepthData = frame.getTimestamp() == rawDepth.getTimestamp();
if (thisFrameHasNewDepthData) {
ByteBuffer depthData = rawDepth.getPlanes()[0].getBuffer();
ByteBuffer confidenceData = rawDepthConfidence.getPlanes()[0].getBuffer();
int width = rawDepth.getWidth();
int height = rawDepth.getHeight();
someReconstructionPipeline.integrateNewImage(depthData, confidenceData, width, height);
}
} catch (NotYetAvailableException e) {
// Depth image is not (yet) available.
}
Kotlin
try {
// Depth image is in uint16, at GPU aspect ratio, in native orientation.
frame.acquireRawDepthImage16Bits().use { rawDepth ->
// Confidence image is in uint8, matching the depth image size.
frame.acquireRawDepthConfidenceImage().use { rawDepthConfidence ->
// Compare timestamps to determine whether depth is is based on new
// depth data, or is a reprojection based on device movement.
val thisFrameHasNewDepthData = frame.timestamp == rawDepth.timestamp
if (thisFrameHasNewDepthData) {
val depthData = rawDepth.planes[0].buffer
val confidenceData = rawDepthConfidence.planes[0].buffer
val width = rawDepth.width
val height = rawDepth.height
someReconstructionPipeline.integrateNewImage(
depthData,
confidenceData,
width = width,
height = height
)
}
}
}
} catch (e: NotYetAvailableException) {
// Depth image is not (yet) available.
}
สิ่งที่จะเกิดขึ้นหลังจากนี้
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe Raw Depth API offers higher accuracy depth data compared to the full Depth API, but might not cover every pixel in the camera image.\u003c/p\u003e\n"],["\u003cp\u003eA confidence image, provided alongside the raw depth image, indicates the accuracy of each depth pixel, enabling developers to filter data based on their needs.\u003c/p\u003e\n"],["\u003cp\u003eRaw Depth is supported on all ARCore depth-compatible devices and leverages available hardware depth sensors if present.\u003c/p\u003e\n"],["\u003cp\u003eCommon use cases for Raw Depth include 3D reconstruction, precise measurements, and shape detection in AR applications.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can enable Raw Depth and access raw depth and confidence images through the ARCore session and frame objects, using provided code snippets for Java and Kotlin.\u003c/p\u003e\n"]]],["The Raw Depth API provides highly accurate depth data for some, but not all, pixels in a camera image, and includes a confidence image indicating the accuracy of each depth estimate. Unlike the full Depth API, which estimates depth for all pixels, Raw Depth may omit some pixels but offers higher precision. The APIs do not require hardware depth sensors. Raw depth images and confidence images are acquired using `frame.acquireRawDepthImage16Bits()` and `frame.acquireRawDepthConfidenceImage()` and can be used for 3D reconstruction, measurement, and shape detection.\n"],null,["# Use Raw Depth in your Android app\n\nThe Raw Depth API provides depth data for a camera image that has higher accuracy than full Depth API data, but does not always cover every pixel. Raw depth images, along with their matching confidence images, can also be further processed, allowing apps to use only the depth data that has sufficient accuracy for their individual use case.\n\nDevice compatibility\n--------------------\n\nRaw Depth is available on all [devices that support the Depth API](/ar/devices). **The Raw Depth API, like the full Depth API, does not require a supported hardware depth sensor, such as a time-of-flight (ToF) sensor.** However, both the Raw Depth API and the full Depth API make use of any supported hardware sensors that a device may have.\n\nRaw Depth API vs full Depth API\n-------------------------------\n\nThe Raw Depth API provides depth estimates with higher accuracy, but raw depth images may not include depth estimates for all pixels in the camera image. In contrast, the full Depth API provides estimated depth for every pixel, but per-pixel depth data may be less accurate due to smoothing and interpolation of depth estimates. **The format and size of depth images are the same across both APIs.** Only the content differs.\n\nThe following table illustrates the differences between the Raw Depth API and the full Depth API using an image of a chair and a table in a kitchen.\n\n| API | Returns | Camera image | Depth image | Confidence image |\n|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|-------------|------------------|\n| Raw Depth API | - A raw depth image that contains a very accurate depth estimate for some, but not all, pixels in the camera image. - A confidence image that gives the confidence for every raw depth image pixel. Camera image pixels that do not have a depth estimate have a confidence of zero. | | | |\n| Full Depth API | - A single \"smoothed\" depth image that contains a depth estimate for every pixel. - No confidence image is provided with this API. | | | N/A |\n\n### Confidence images\n\nIn confidence images returned by the Raw Depth API, lighter pixels have higher confidence values, with white pixels representing full confidence and black pixels representing no confidence. In general, regions in the camera image that have more texture, such as a tree, will have higher raw depth confidence than regions that don't, such as a blank wall. Surfaces with no texture usually yield a confidence of zero.\n\nIf the target device has a supported hardware depth sensor, confidence in areas of the image close enough to the camera will likely be higher, even on textureless surfaces.\n\n### Compute cost\n\nThe compute cost of the Raw Depth API is about half of the compute cost for the full Depth API.\n\nUse cases\n---------\n\nWith the Raw Depth API, you can obtain depth images that provide a more detailed representation of the geometry of the objects in the scene. Raw depth data can be useful when creating AR experiences where increased depth accuracy and detail are needed for geometry-understanding tasks. Some use cases include:\n\n- 3D reconstruction\n- Measurement\n- Shape detection\n\nPrerequisites\n-------------\n\nMake sure that you understand [fundamental AR concepts](/ar/develop/fundamentals)\nand how to [configure an ARCore session](/ar/develop/java/session-config) before proceeding.\n\nEnable Depth\n------------\n\nIn [a new ARCore session](/ar/develop/java/session-config), check whether a user's device supports Depth. Not all ARCore-compatible devices support the Depth API due to processing power constraints. To save resources, depth is disabled by default on ARCore. Enable depth mode to have your app use the Depth API. \n\n### Java\n\n```java\nConfig config = session.getConfig();\n\n// Check whether the user's device supports Depth.\nif (session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)) {\n // Enable depth mode.\n config.setDepthMode(Config.DepthMode.AUTOMATIC);\n}\nsession.configure(config);\n```\n\n### Kotlin\n\n```kotlin\nif (session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)) {\n session.configure(session.config.apply { depthMode = Config.DepthMode.AUTOMATIC })\n}\n```\n\nAcquire the latest raw depth and confidence images\n--------------------------------------------------\n\nCall [`frame.acquireRawDepthImage16Bits()`](/ar/reference/java/com/google/ar/core/Frame#acquireRawDepthImage16Bits-) to acquire the latest raw depth image. Not all image pixels returned via the Raw Depth API will contain depth data, and not every ARCore frame will contain a new raw depth image. To determine whether the raw depth image for the current frame is new, compare its timestamp with the timestamp of the previous raw depth image. If the timestamps are different, the raw depth image is based on new depth data. Otherwise, the depth image is a reprojection of previous depth data.\n\nCall [`frame.acquireRawDepthConfidenceImage()`](/ar/reference/java/com/google/ar/core/Frame#acquireRawDepthConfidenceImage-) to acquire the confidence image. You can use the confidence image to check the accuracy of each raw depth pixel. Confidence images are returned in Y8 format. Each pixel is an 8-bit unsigned integer. `0` indicates the least confidence, while `255` indicates the most. \n\n### Java\n\n```java\n// Use try-with-resources, so that images are released automatically.\ntry (\n// Depth image is in uint16, at GPU aspect ratio, in native orientation.\nImage rawDepth = frame.acquireRawDepthImage16Bits();\n // Confidence image is in uint8, matching the depth image size.\n Image rawDepthConfidence = frame.acquireRawDepthConfidenceImage(); ) {\n // Compare timestamps to determine whether depth is is based on new\n // depth data, or is a reprojection based on device movement.\n boolean thisFrameHasNewDepthData = frame.getTimestamp() == rawDepth.getTimestamp();\n if (thisFrameHasNewDepthData) {\n ByteBuffer depthData = rawDepth.getPlanes()[0].getBuffer();\n ByteBuffer confidenceData = rawDepthConfidence.getPlanes()[0].getBuffer();\n int width = rawDepth.getWidth();\n int height = rawDepth.getHeight();\n someReconstructionPipeline.integrateNewImage(depthData, confidenceData, width, height);\n }\n} catch (NotYetAvailableException e) {\n // Depth image is not (yet) available.\n}\n```\n\n### Kotlin\n\n```kotlin\ntry {\n // Depth image is in uint16, at GPU aspect ratio, in native orientation.\n frame.acquireRawDepthImage16Bits().use { rawDepth -\u003e\n // Confidence image is in uint8, matching the depth image size.\n frame.acquireRawDepthConfidenceImage().use { rawDepthConfidence -\u003e\n // Compare timestamps to determine whether depth is is based on new\n // depth data, or is a reprojection based on device movement.\n val thisFrameHasNewDepthData = frame.timestamp == rawDepth.timestamp\n if (thisFrameHasNewDepthData) {\n val depthData = rawDepth.planes[0].buffer\n val confidenceData = rawDepthConfidence.planes[0].buffer\n val width = rawDepth.width\n val height = rawDepth.height\n someReconstructionPipeline.integrateNewImage(\n depthData,\n confidenceData,\n width = width,\n height = height\n )\n }\n }\n }\n} catch (e: NotYetAvailableException) {\n // Depth image is not (yet) available.\n}\n```\n\nWhat's next\n-----------\n\n- Learn how to build your own app with Raw Depth by going through the [Raw Depth codelab](https://codelabs.developers.google.com/codelabs/arcore-rawdepthapi)."]]