【问题标题】:Create depth map image as 24-bit (Carla)将深度图图像创建为 24 位 (Carla)
【发布时间】:2023-03-10 20:00:01
【问题描述】:

我有一个以 24 位编码的深度图(标记为“原始”)。 使用以下代码:

carla_img = cv.imread('carla_deep.png', flags=cv.IMREAD_COLOR)
carla_img = carla_img[:, :, :3]
carla_img = carla_img[:,:,::-1]
gray_depth = ((carla_img[:,:,0] + carla_img[:,:,1] * 256.0 + carla_img[:,:,2] * 256.0 * 256.0)/((256.0 * 256.0 * 256.0) - 1))
gray_depth = gray_depth * 1000

我可以将它转换为“转换后的”图像。 如图所示:https://carla.readthedocs.io/en/latest/ref_sensors/

我怎样才能扭转这个过程(不使用任何更大的外部库并且最多使用openCV)?在 Python 中,我在 OpenCV 的帮助下创建了一个深度图。我想以 Carla(24 位)的形式保存获得的深度图。

这就是我创建深度图的方式:

imgL = cv.imread('leftImg.png',0)
imgR = cv.imread('rightImg.png',0)
stereo = cv.StereoBM_create(numDisparities=128, blockSize=17)
disparity = stereo.compute(imgL,imgR)

CameraFOV = 120
Focus_length = width /(2 * math.tan(CameraFOV * math.pi / 360))
camerasBaseline = 0.3
depthMap = (camerasBaseline * Focus_length) / disparity

如何将获取的深度图保存为与标有“原始”的图片相同的格式?

【问题讨论】:

  • 视差图不是深度图。我想将深度图保存为 24 位。所以在R(8位)、G(8位)、B(8位)通道上写入距离数据。为了使它出来这样的图像,标记为“原始”。法线深度图为 8 位(仅限灰度)。在帖子中,我询问了我从 24 位到 8 位的逆向过程。
  • 好吧,对不起,这更容易,但我对 python 的了解还不够,无法用于那些频道重新排序的东西。看起来像 depthmap/1000 和 channel1=depthmap*(256*256*256-1);频道2= ...
  • 深度图是灰度图。它仅在您的原始文件中是彩色的,因为有人添加了一个颜色图来用不同的颜色对每个灰度进行编码。您需要知道用于创建原件的颜色图。
  • @fmw42 不,这不是颜色图,实际上是 24 位深度,重新解释为 RGB。我看了看频道。
  • 所以问题是如何向后计算......这并不难。您只缺少“模”运算符。人们也可以做一些重新解释技巧并省去数学,但这会假设一定的字节顺序。

标签: python numpy opencv depth carla


【解决方案1】:

Docs say:

normalized = (R + G * 256 + B * 256 * 256) / (256 * 256 * 256 - 1)
in_meters = 1000 * normalized

因此,如果您有一个深度图 in_meters,您可以通过重新排列方程式来做相反的事情。

需要确保您的深度图(来自块匹配)以米为单位。假设您的相机的基线为 0.3 米,您的计算看起来很合理。

第一个变体

使用除法和模运算将计算分开。

需要各种.astype 将浮点数转换为整数,将较宽的整数转换为窄整数(图片假设)。

normalized = in_meters / 1000
BGR = (normalized * (2**24-1)).astype(np.uint32)
BG,R = np.divmod(BGR, 2**8)
B,G = np.divmod(BG, 2**8)
carla_img = np.dstack([B,G,R]).astype(np.uint8) # BGR order

第二种变体

也可以使用view 来执行此操作,将 uint32 数据重新解释为四个 uint8 值。这假设了一个 little endian 系统,这是一个公平的假设,但需要注意这一点。

...
reinterpreted = BGR.view(np.uint8) # lowest byte first, i.e. order is RGBx
reinterpreted.shape = BGR.shape + (4,) # np.view refuses to add a dimension

carla_img = reinterpreted[:,:,(2,1,0)] # select BGR
# this may require a .copy() to get data without holes (OpenCV may want this)

免责声明

我无法测试此答案中的代码,因为您没有提供可用数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多