如何将 16 位深度图像保存到 Arcore (java) 中的文件 [英] How to save 16bit depth Image to file in Arcore (java)

查看:58
本文介绍了如何将 16 位深度图像保存到 Arcore (java) 中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将arcore的深度信息保存到存储中.这是开发者指南中的示例.

 public int getMillimetersDepth(Image depthImage, int x, int y) {//深度图像有一个平面,每个平面存储深度//像素为 16 位无符号整数.Image.Plane 平面 = depthImage.getPlanes()[0];int byteIndex = x * plane.getPixelStride() + y * plane.getRowStride();ByteBuffer 缓冲区 = plane.getBuffer().order(ByteOrder.nativeOrder());短 depthSample = buffer.getShort(byteIndex);返回深度样本;}

所以我想把这个bytebuffer保存到本地文件中,但是我输出的txt文件不可读.我怎么能解决这个问题?这是我所拥有的

Image depthImage = frame.acquireDepthImage();Image.Plane 平面 = depthImage.getPlanes()[0];int format = depthImage.getFormat();ByteBuffer 缓冲区 = plane.getBuffer().order(ByteOrder.nativeOrder());字节 [] 数据 = 新字节 [buffer.remaining()];缓冲区.获取(数据);File mypath=new File(super.getExternalFilesDir(depDir"),Long.toString(lastPointCloudTimestamp)+.txt");FileChannel fc = new FileOutputStream(mypath).getChannel();fc.write(缓冲区);fc.close();depthImage.close();

我试图用

解码它们

String s = new String(data, UTF-8");System.out.println(StandardCharsets.UTF-8.decode(buffer).toString());

但是输出还是这样奇怪

 .03579:<=>@ABCDFGHJKMNOPRQSUWY]_b

解决方案

为了获取 ARCore 会话提供的深度数据,您需要将字节写入本地文件.Buffer 对象是一个容器,它包含特定原始类型元素的有限序列(此处为 ByteBuffer 的字节).因此,您需要在文件中写入的是 data 变量,该变量对应于先前存储在缓冲区中的信息(根据 buffer.get(data)).

它对我来说很好用,我设法在 python 代码中绘制了提供的深度图(但以下代码背后的想法可以很容易地适应 java 代码):

depthData = np.fromfile('depthdata.txt', dtype = np.uint16)H = 120宽 = 160定义提取深度(x):depthConfidence = (x > > 13) &0x7如果(深度置信度 > 6):返回 0返回 x &0x1FFFdepthMap = np.array([extractDepth(x) for x in depthData]).reshape(H,W)depthMap = cv.rotate(depthMap, cv.ROTATE_90_CLOCKWISE)

有关详细信息,请在此处阅读有关深度图 (DEPTH16) 格式的信息:https://developer.android.com/reference/android/graphics/ImageFormat#DEPTH16您还必须注意,深度图分辨率设置为 160x120 像素,并根据横向格式定向.

还要确保用 try/catch 代码块包围您的代码,以防出现 IOException 错误.

I want to save the depth info from the arcore to the storage. Here is the example from the developer guide.

  public int getMillimetersDepth(Image depthImage, int x, int y) {
  // The depth image has a single plane, which stores depth for each
  // pixel as 16-bit unsigned integers.
  Image.Plane plane = depthImage.getPlanes()[0];
  int byteIndex = x * plane.getPixelStride() + y * plane.getRowStride();
  ByteBuffer buffer = plane.getBuffer().order(ByteOrder.nativeOrder());
  short depthSample = buffer.getShort(byteIndex);
  return depthSample;
}

So I want to save this bytebuffer into a local file, but my output txt file is not readable. How could I fix this? Here is what I have

Image depthImage = frame.acquireDepthImage();
Image.Plane plane = depthImage.getPlanes()[0];
int format = depthImage.getFormat();
ByteBuffer buffer = plane.getBuffer().order(ByteOrder.nativeOrder());
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
File mypath=new File(super.getExternalFilesDir("depDir"),Long.toString(lastPointCloudTimestamp)+".txt");
FileChannel fc = new FileOutputStream(mypath).getChannel();
fc.write(buffer);
fc.close();
depthImage.close();

I tried to decode them with

String s = new String(data, "UTF-8");
System.out.println(StandardCharsets.UTF-8.decode(buffer).toString());

but the output is still strange like this

    .03579:<=>@ABCDFGHJKMNOPRQSUWY]_b

解决方案

In order to obtain the depth data provided by the ARCore session, you need to write bytes into your local file. A Buffer object is a container, it countains a finite sequence of elements of a specific primitive type (here bytes for a ByteBuffer). So what you need to write inside your file is your data variable that corresponds to the information previously stored in the buffer (according to buffer.get(data)).

It works fine for me, I managed to draw the provided depth map within a python code (but the idea behind the following code can be easily adapted to a java code):

depthData = np.fromfile('depthdata.txt', dtype = np.uint16) 
H = 120 
W = 160
def extractDepth(x):
    depthConfidence = (x >> 13) & 0x7 
    if (depthConfidence > 6): return 0 
    return x & 0x1FFF 
depthMap = np.array([extractDepth(x) for x in depthData]).reshape(H,W)
depthMap = cv.rotate(depthMap, cv.ROTATE_90_CLOCKWISE)

For further details, read the information concerning the format of the depthmap (DEPTH16) here: https://developer.android.com/reference/android/graphics/ImageFormat#DEPTH16 You must also be aware that the depthmap resolution is set to 160x120 pixels and is oriented according to a landscape format.

Make also sure to surround your code by a try/catch code bloc in case of a IOException error.

这篇关于如何将 16 位深度图像保存到 Arcore (java) 中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆