无法将shottbuffer深度图像转换为图片格式

gzszwxb4  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(379)

使用hms arengine,sdk版本是2.13.0.4,在一个带有tof摄像机的设备上。
在尝试获取优化的深度图像时,我使用了getscenedepth()方法,它返回一个shortbuffer,我将其转换为bytearray并将其存储为jpg文件。
但是jpg文件无法打开,而getscenedepthwidth()和getscenedepthheight()方法返回正确的值,下面是代码:

int a = arFrame.acquireSceneMesh().getSceneDepthWidth();
int b = arFrame.acquireSceneMesh().getSceneDepthHeight();
Log.i(TAG, "WorldRenderManager: arFrame.acquireSceneMesh().getSceneDepthWidth()" + a + "");
Log.i(TAG, "WorldRenderManager: arFrame.acquireSceneMesh().getSceneDepthHeight()" + b + "");
ShortBuffer shortBuffer = arFrame.acquireSceneMesh().getSceneDepth();
ByteBuffer byteBuffer = ByteBuffer.allocate(shortBuffer.capacity() * 2);
byteBuffer.asShortBuffer().put(shortBuffer);
OutputStream fos = new FileOutputStream(depthimage.jpg);
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes, 0, bytes.length);
BufferedOutputStream bos = new BufferedOutputStream(fos);
try {
    bos.write(bytes);
    bos.close();
    } catch (FileNotFoundException e) {
    Log.e(TAG, "FileNotFound");
    } catch (IOException e) {
    Log.e(TAG, "IOException");
    }

我是否正确地转换了shortbuffer或者遗漏了什么?欢迎任何建议。

xnifntxz

xnifntxz1#

getscenedepth()方法将深度图像作为shortbuffer返回,不能直接以图片格式存储,可以先将图像存储为bin文件,然后使用python将其转换为jpg文件。更改fos的文件存储格式:

OutputStream fos = new FileOutputStream(depthimage.bin);

然后使用python进行转换并将其存储为jpg图片:

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import rotate

BIN_FILE = " depthimage.bin"

# Set the format of data, 180 and 240 are the height and width of depth map, which is acquired using getSceneDepthHeight and getSceneDepthWidth

BIN_SIZE = [180, 240]
data_raw = np.fromfile(BIN_FILE, dtype = 'uint16')
data_swap = data_raw.byteswap(True)
data = np.reshape(data_swap, BIN_SIZE, order = 'C')
data = np.bitwise_and(data, 0x1fff)

# Rotate the data if needed

data_rotate = rotate(data, -90)
plt.imshow(data, cmap = 'gray')
plt.axis('off')
cbar = plt.colorbar()
cbar.set_label('Distance in mm', rotation = -90, va = 'bottom')
plt.savefig('./ depthimage.jpg')

大田,现在你有了jpg格式的深度图像。

相关问题