如何将 16 位深度图像保存到 Arcore (java) 中的文件

问题描述

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

  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;
}

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

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();

我试图用

解码它们
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 
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)

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...