by adec314 · 2w ago · 0 comments
import flatpoints as fp
import pyjpegxl
import os
#============ Raw point cloud =============================
xyz = fp.load("bunny") #Required format: numpy array, (N, 3)
#============ Fit the flattener ===========================
single_face = True #Or false -> two images (left + right)
flat = fp.Flat(xyz, single_face = single_face)
#============ Build one (single_face) resp 2 images: ======
images = flat.to_image(xyz) #(1, n, n, 3) or (2, n, n, 3)
left_image = images[0] #(n, n, 3), right_image = images[1]
#/!\, normalisation, needed for pyjpegxl:
b = xyz.min(axis = 0)
a = xyz.max(axis = 0) - b
left_image_normalised = (left_image - b[None, None])/a[None, None]
#============ Compression, Decompression ==================
pyjpegxl.imwrite(f"compressed.jxl", left_image_normalised, quality = 98)
_, image_decompressed = pyjpegxl.imread(f"compressed.jxl")
xyz_decompressed = a*image_decompressed.reshape(-1, 3) + b #basically, that's it.
#============ Visual inspection ============================
_ = fp.Flat(xyz_decompressed, single_face = single_face).plot()
#============ Memory saving ============================
memory_before = xyz.nbytes
memory_after = os.path.getsize('compressed.jxl')
print(f"Initial memory: {memory_before / 1024:.2f} KB")
print(f"New memory: {memory_after / 1024:.2f} KB")
No comments yet.