{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "def load_binary_masks(bin_file_path):\n", " with open(bin_file_path, 'rb') as f:\n", " shape = np.fromfile(f, dtype=np.int32, count=3)\n", " # 读取掩码数据,使用uint16类型\n", " binary_masks = np.fromfile(f, dtype=np.uint16)\n", " binary_masks = binary_masks.reshape(shape)\n", " return binary_masks" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "orig= load_binary_masks('/nfs/dataset-ofs-voyager-research/shichen/project/video_diffusion/ConsisID/workdirs/step4_track_masks_data/515d576284baf2cb5ecc534f3105f3fb_0_107/tracking_mask_results/1/masks.bin')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "orig = orig.astype(np.uint8)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import blosc\n", "import os\n", "\n", "def compress_3d_direct(binary_volume, output_file='3d_direct.bin'):\n", " \"\"\"直接压缩整个3D体积\"\"\"\n", " # 先用np.packbits进行基础压缩\n", " packed = np.packbits(binary_volume)\n", " \n", " # 使用专门的压缩算法\n", " compressed = blosc.compress(packed.tobytes(), \n", " typesize=1, \n", " cname='zstd', \n", " clevel=9,\n", " shuffle=blosc.BITSHUFFLE) # 位级混排,对二值数据更有效\n", " \n", " # 保存压缩数据和元数据\n", " with open(output_file, 'wb') as f:\n", " # 保存元数据(形状)\n", " shape_info = np.array(binary_volume.shape, dtype=np.int32)\n", " f.write(shape_info.tobytes())\n", " # 保存压缩数据\n", " f.write(compressed)\n", " \n", " # 计算压缩比\n", " original_size = binary_volume.nbytes\n", " compressed_size = os.path.getsize(output_file)\n", " return original_size, compressed_size, original_size/compressed_size" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(99688320, 121135, 822.9522433648409)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "compress_3d_direct(orig)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import blosc\n", "import os\n", "\n", "def decompress_3d_direct(input_file='3d_direct.bin'):\n", " \"\"\"解压缩由compress_3d_direct压缩的3D二值数组\"\"\"\n", " with open(input_file, 'rb') as f:\n", " # 读取元数据(形状信息)\n", " # 假设形状是3维的,每个维度是32位整数(4字节)\n", " shape_bytes = f.read(3 * 4) # 3个int32\n", " shape_info = np.frombuffer(shape_bytes, dtype=np.int32)\n", " \n", " # 读取压缩数据\n", " compressed_data = f.read()\n", " \n", " # 解压缩blosc数据\n", " decompressed_bytes = blosc.decompress(compressed_data)\n", " \n", " # 将字节转换回numpy数组(仍是打包的位)\n", " packed_array = np.frombuffer(decompressed_bytes, dtype=np.uint8)\n", " \n", " # 计算原始数组中的元素总数\n", " total_elements = shape_info[0] * shape_info[1] * shape_info[2]\n", " \n", " # 解开位打包,还原为布尔数组\n", " unpacked = np.unpackbits(packed_array)\n", " \n", " # 可能需要截断多余的位(unpackbits总是产生8的倍数长度)\n", " if len(unpacked) > total_elements:\n", " unpacked = unpacked[:total_elements]\n", " \n", " # 重塑为原始形状\n", " result = unpacked.reshape(tuple(shape_info)).astype(np.bool_)\n", " \n", " return result\n", "\n", "# 验证压缩和解压是否正确\n", "def verify_compression(original_array, input_file='3d_direct.bin'):\n", " \"\"\"验证压缩和解压是否无损\"\"\"\n", " # 解压缩\n", " decompressed = decompress_3d_direct(input_file)\n", " \n", " # 检查形状是否相同\n", " shape_match = original_array.shape == decompressed.shape\n", " \n", " # 检查内容是否相同\n", " content_match = np.array_equal(original_array, decompressed)\n", " \n", " print(f\"形状匹配: {shape_match}\")\n", " print(f\"内容匹配: {content_match}\")\n", " \n", " if not content_match:\n", " # 找出不匹配的元素数量\n", " diff_count = np.sum(original_array != decompressed)\n", " total_elements = np.prod(original_array.shape)\n", " print(f\"不匹配元素: {diff_count}/{total_elements} ({diff_count/total_elements*100:.6f}%)\")\n", " \n", " return shape_match and content_match" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "tmp = decompress_3d_direct()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(tmp.astype(np.uint8) == orig).all()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "dino", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.19" } }, "nbformat": 4, "nbformat_minor": 2 }