发布于 2022-05-18 11:41:24
numpy不支持每个元素数组1位,我怀疑memmap有这样的特性。然而,有一个简单的解决办法使用包位。
由于您的情况不是按位随机访问,所以可以将其读取为每个元素数组1字节。
# A binary mask represented as an 1 byte per element array.
full_size_mask = np.random.randint(0, 2, size=[1920, 1080], dtype=np.uint8)
# Pack mask vertically.
packed_mask = np.packbits(full_size_mask, axis=0)
# Save as a memmap compatible file.
buffer = np.memmap("./temp.bin", mode='w+',
dtype=packed_mask.dtype, shape=packed_mask.shape)
buffer[:] = packed_mask
buffer.flush()
del buffer
# Open as a memmap file.
packed_mask = np.memmap("./temp.bin", mode='r',
dtype=packed_mask.dtype, shape=packed_mask.shape)
# Rect where you want to crop.
top = 555
left = 777
width = 256
height = 256
# Read the area containing the rect.
packed_top = top // 8
packed_bottom = (top + height) // 8 + 1
packed_patch = packed_mask[packed_top:packed_bottom, left:left + width]
# Unpack and crop the actual area.
patch_top = top - packed_top * 8
patch_mask = np.unpackbits(packed_patch, axis=0)[patch_top:patch_top + height]
# Check that the mask is cropped from the correct area.
print(np.all(patch_mask == full_size_mask[top:top + height, left:left + width]))请注意,此解决方案可以(而且很可能会)读取额外的位。具体而言,两端最大7位。在您的例子中,它将是7x2x256位,但这仅仅是补丁的5%,所以我相信它是可以忽略不计的。
顺便说一句,这不是你的问题的答案,但是当你处理二进制掩码,如图像分割标签,压缩压缩可能会大大减少文件大小。这是可能的,它可以减少到少于8KB的每幅图像(而不是每个补丁)。您可能也需要考虑这个选项。
https://stackoverflow.com/questions/72283998
复制相似问题