将图像格式从 32FC1 转换为 16UC1

Convert an image format from 32FC1 to 16UC1

我需要以 16UC1 格式对图像进行编码,但收到错误消息: cv_bridge.core.CvBridgeError:编码指定为 16UC1,但图像具有不兼容的类型 32FC1

我尝试使用 skimage 函数 img_as_uint 但由于我的图像值不在 -1 和 1 之间,所以它不起作用。我还尝试通过将所有值除以从 np.amax 获得的值来 "normalize" 我的值,但仅使用 skimage 函数 returns 空白图像。

有没有办法实现这种转换?

这是原文32FC1 image

使用 numpy 你应该能够:

import numpy as np
img = np.random.normal(0, 1, (300, 300, 3)).astype(np.float32) # simulated image
uimg = img.astype(np.uint16)

如果它不在无符号范围内,您可能首先要进行某种规范化。可能是这样的:

img_normalized = (img-img.min())/(img.max()-img.min())*256**2

但是您的规范化策略将取决于您想要实现的目标。


感谢您分享图片。我可以想象如下:

import numpy as np
import matplotlib.pyplot as plt

arr = np.load('32FC1_image.npz')
img = arr['arr_0']
img = np.squeeze(img) # this gets rid of the extra dimensions that are causing matplotlib to not recognize it as an image, the extra dimensions also may be causing your problems
img_normalized = (img-img.min())/(img.max()-img.min())*256**2
img_normalized = img_normalized.astype(np.uint16)
plt.imshow(img_normalized)

尝试使用标准化的 16 位图像。