[Python] How V4l2 10bit storage works?

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    This is the v4l2 documentation for 10 bit sensor: https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/pixfmt-srggb10.html

    According to this documentation I belive my 1 10 bit pixel should be stored like this

    xxxxxxxx 000000xx


    where first 8 bits are the lower byte and next 8 bits are the higher byte and first 6 of MSB are zeroes.

    I get the sensor data through Jetson Xavier nx using the following command:

    v4l2-ctl -d /dev/video0 --set-fmt-video=width=1280,height=720,pixelformat=RG10 --set-ctrl=bypass_mode=0 --stream-mmap --stream-count=1 --stream-to=file.raw


    Here's my python code where I'm reading my raw file and printing it along with it's binary:

    import numpy as np
    raw_file_path = 'file.raw'
    width = 3280
    height = 2464
    dtype = np.uint16
    with open(raw_file_path, 'rb') as f:
    data = np.fromfile(f, dtype=dtype)

    if data.size != width * height:
    raise ValueError(f"File size doesn't match expected shape ({height}, {width}). Got {data.size} elements.")

    image = data.reshape((height, width)) # shape (3280, 2482)

    print(image)
    print(image.shape)
    print(image.dtype)


    image_bin = np.vectorize(lambda x: format(x, '016b'))(image)

    print(image_bin)
    print(image_bin.shape)
    print(image_bin.dtype)


    ANd here's the output:

    [[4548 4996 4484 ... 4676 4932 4612]
    [5125 4420 4996 ... 0 0 0]
    [4676 5125 4484 ... 4612 5060 4548]
    ...
    [4676 4356 4676 ... 0 0 0]
    [4164 4420 4292 ... 4292 4548 4292]
    [4420 4292 4420 ... 0 0 0]]
    (2464, 3280)
    uint16
    [['0001000111000100' '0001001110000100' '0001000110000100' ...
    '0001001001000100' '0001001101000100' '0001001000000100']
    ['0001010000000101' '0001000101000100' '0001001110000100' ...
    '0000000000000000' '0000000000000000' '0000000000000000']
    ['0001001001000100' '0001010000000101' '0001000110000100' ...
    '0001001000000100' '0001001111000100' '0001000111000100']
    ...
    ['0001001001000100' '0001000100000100' '0001001001000100' ...
    '0000000000000000' '0000000000000000' '0000000000000000']
    ['0001000001000100' '0001000101000100' '0001000011000100' ...
    '0001000011000100' '0001000111000100' '0001000011000100']
    ['0001000101000100' '0001000011000100' '0001000101000100' ...
    '0000000000000000' '0000000000000000' '0000000000000000']]
    (2464, 3280)
    <U16


    As seen there's no consecutive 6 zeros in my binary. How the 10 bit image is stored? Help me understand.

    Continue reading...

Compartilhe esta Página