Mat Class Reference

The Mat class can handle multiple matrix formats from 1 to 4 channels, with different value types (float or uchar), and can be stored CPU and/or GPU side. More...

Functions

def __cinit__ (self, width=0, height=0, mat_type=MAT_TYPE.F32_C1, memory_type=MEM.CPU)
 Constructor. More...
 
def init_mat_type (self, width, height, mat_type, memory_type=MEM.CPU)
 Inits a new Mat . More...
 
def init_mat_cpu (self, int width, int height, MAT_TYPE mat_type, ptr, step, memory_type=MEM.CPU)
 Inits a new Mat from an existing data pointer. More...
 
def init_mat_resolution (self, Resolution resolution, MAT_TYPE mat_type, memory_type=MEM.CPU)
 Inits a new Mat . More...
 
def init_mat_resolution_cpu (self, Resolution resolution, mat_type, ptr, step, memory_type=MEM.CPU)
 Inits a new Mat from an existing data pointer. More...
 
def init_mat (self, Mat matrix)
 Inits a new Mat by copy (shallow copy). More...
 
def alloc_size (self, width, height, mat_type, memory_type=MEM.CPU)
 Allocates the Mat memory. More...
 
def alloc_resolution (self, Resolution resolution, MAT_TYPE mat_type, memory_type=MEM.CPU)
 Allocates the Mat memory. More...
 
def free (self, memory_type=MEM.CPU)
 Free the owned memory. More...
 
def copy_to (self, Mat dst, cpy_type=COPY_TYPE.CPU_CPU)
 Copies data to another Mat (deep copy). More...
 
def set_from (self, Mat src, cpy_type=COPY_TYPE.CPU_CPU)
 Copies data from an other Mat (deep copy). More...
 
def read (self, str filepath)
 Reads an image from a file (only if MEM.CPU is available on the current Mat ). More...
 
def write (self, str filepath, memory_type=MEM.CPU, compression_level=-1)
  Writes the Mat (only if MEM.CPU is available on the current Mat ) into a file as an image. More...
 
def set_to (self, value, memory_type=MEM.CPU)
 Fills the Mat with the given value. More...
 
def set_value (self, int x, int y, value, memory_type=MEM.CPU)
 Sets a value to a specific point in the matrix. More...
 
def get_value (self, int x, int y, memory_type=MEM.CPU)
 Returns the value of a specific point in the matrix. More...
 
def get_width (self)
 Returns the width of the matrix. More...
 
def get_height (self)
 Returns the height of the matrix. More...
 
def get_resolution (self)
 Returns the resolution of the matrix. More...
 
def get_channels (self)
 Returns the number of values stored in one pixel. More...
 
def get_data_type (self)
 Returns the format of the matrix. More...
 
def get_memory_type (self)
 Returns the format of the matrix. More...
 
def get_data (self, memory_type=MEM.CPU)
  Copies the data of the Mat in a Numpy array. More...
 
def get_step_bytes (self, memory_type=MEM.CPU)
 Returns the memory step in Bytes (the Bytes size of one pixel row). More...
 
def get_step (self, memory_type=MEM.CPU)
 Returns the memory step in number of elements (the number of values in one pixel row). More...
 
def get_pixel_bytes (self)
 Returns the size in bytes of one pixel. More...
 
def get_width_bytes (self)
  Returns the size in bytes of a row. More...
 
def get_infos (self)
  Returns the information about the Mat into a string. More...
 
def is_init (self)
 Defines whether the Mat is initialized or not. More...
 
def is_memory_owner (self)
 Returns whether the Mat is the owner of the memory it accesses. More...
 
def clone (self, Mat py_mat)
 Duplicates Mat by copy (deep copy). More...
 
def move (self, Mat py_mat)
 Moves Mat data to another Mat. More...
 
def get_pointer (self, memory_type=MEM.CPU)
 Gets the pointer of the content of the Mat. More...
 
def name (self)
 The name of the Mat (optional). More...
 
def verbose (self)
 Whether the Mat can display information or not.
 

Static Functions

def swap (Mat mat1, Mat mat2)
 Swaps the content of the provided Mat (only swaps the pointers, no data copy). More...
 

Detailed Description

The Mat class can handle multiple matrix formats from 1 to 4 channels, with different value types (float or uchar), and can be stored CPU and/or GPU side.

Mat is defined in a row-major order, it means that, for an image buffer, the entire first row is stored first, followed by the entire second row, and so on.

The CPU and GPU buffer aren't automatically synchronized for performance reasons, you can use update_gpu_from_cpu / update_gpu_from_cpu to do it. If you are using the GPU side of the Mat object, you need to make sure to call free before destroying the Camera object. The destruction of the Camera object deletes the CUDA context needed to free the Mat memory.

Functions

◆ __cinit__()

def __cinit__ (   self,
  width = 0,
  height = 0,
  mat_type = MAT_TYPE.F32_C1,
  memory_type = MEM.CPU 
)

Constructor.

Parameters
width: width of the matrix in pixels. Default: 0
height: height of the matrix in pixels. Default: 0
mat_type: the type of the matrix ( MAT_TYPE.F32_C1 , MAT_TYPE.U8_C4 ...). Default: MAT_TYPE.F32_C1
memory_type: defines where the buffer will be stored. Default: MEM.CPU (you cannot change this default value)

 

mat = sl.Mat(width=0, height=0, mat_type=MAT_TYPE.F32_C1, memory_type=MEM.CPU)
 

◆ init_mat_type()

def init_mat_type (   self,
  width,
  height,
  mat_type,
  memory_type = MEM.CPU 
)

Inits a new Mat .

This function directly allocates the requested memory. It calls alloc_size .  

Parameters
width: width of the matrix in pixels.  
height: height of the matrix in pixels.  
mat_type: the type of the matrix (MAT_TYPE.F32_C1 , MAT_TYPE.U8_C4 ...)
memory_type: defines where the buffer will be stored. Default: MEM.CPU (you cannot change this default value)

◆ init_mat_cpu()

def init_mat_cpu (   self,
int  width,
int  height,
MAT_TYPE  mat_type,
  ptr,
  step,
  memory_type = MEM.CPU 
)

Inits a new Mat from an existing data pointer.

This function doesn't allocate the memory.  

Parameters
width: width of the matrix in pixels.
height: height of the matrix in pixels.  
mat_type: the type of the matrix (MAT_TYPE.F32_C1 , MAT_TYPE.U8_C4 ...)  
ptr: pointer to the data array. (CPU or GPU).  
step: step of the data array. (the Bytes size of one pixel row).  
memory_type: defines where the buffer will be stored. Default: MEM.CPU (you cannot change this default value)

◆ init_mat_resolution()

def init_mat_resolution (   self,
Resolution  resolution,
MAT_TYPE  mat_type,
  memory_type = MEM.CPU 
)

Inits a new Mat .

This function directly allocates the requested memory. It calls alloc_resolution .

Parameters
resolution: the size of the matrix in pixels.  
mat_type: the type of the matrix (MAT_TYPE.F32_C1 , MAT_TYPE.U8_C4 ... )
memory_type: defines where the buffer will be stored. Default: MEM.CPU (you cannot change this default value)

◆ init_mat_resolution_cpu()

def init_mat_resolution_cpu (   self,
Resolution  resolution,
  mat_type,
  ptr,
  step,
  memory_type = MEM.CPU 
)

Inits a new Mat from an existing data pointer.

This function doesn't allocate the memory.

Parameters
resolution: the size of the matrix in pixels.  
mat_type: the type of the matrix (MAT_TYPE.F32_C1 , MAT_TYPE.U8_C4 ...)  
ptr: pointer to the data array. (CPU or GPU).  
step: step of the data array. (the Bytes size of one pixel row).  
memory_type: defines where the buffer will be stored. Default: MEM.CPU (you cannot change this default value)

◆ init_mat()

def init_mat (   self,
Mat  matrix 
)

Inits a new Mat by copy (shallow copy).

This function doesn't allocate the memory.  

Parameters
mat: a Mat to copy.

◆ alloc_size()

def alloc_size (   self,
  width,
  height,
  mat_type,
  memory_type = MEM.CPU 
)

Allocates the Mat memory.

 

Parameters
width: width of the matrix in pixels.  
height: height of the matrix in pixels.  
mat_type: the type of the matrix (MAT_TYPE.F32_C1 , MAT_TYPE.U8_C4 ...)  
memory_type: defines where the buffer will be stored. Default: MEM.CPU (you cannot change this default value)

 

Warning
It erases previously allocated memory.

◆ alloc_resolution()

def alloc_resolution (   self,
Resolution  resolution,
MAT_TYPE  mat_type,
  memory_type = MEM.CPU 
)

Allocates the Mat memory.

Parameters
resolution: the size of the matrix in pixels.  
mat_type: the type of the matrix (MAT_TYPE.F32_C1 , MAT_TYPE.U8_C4 ...)  
memory_type: defines where the buffer will be stored. Default: MEM.CPU (you cannot change this default value)

 

Warning
It erases previously allocated memory.

◆ free()

def free (   self,
  memory_type = MEM.CPU 
)

Free the owned memory.

Parameters
memory_type: specifies which memory you wish to free. Default: MEM.CPU (you cannot change this default value)

◆ copy_to()

def copy_to (   self,
Mat  dst,
  cpy_type = COPY_TYPE.CPU_CPU 
)

Copies data to another Mat (deep copy).

 

Parameters
dst: the Mat where the data will be copied.  
cpy_type: specifies the memories that will be used for the copy. Default: COPY_TYPE.CPU_CPU (you cannot change the default value)  
Returns
ERROR_CODE.SUCCESS if everything went well, ERROR_CODE.FAILURE otherwise.

 

Note
If the destination is not allocated or doesn't have a compatible MAT_TYPE or Resolution , current memory is freed and new memory is directly allocated.

◆ set_from()

def set_from (   self,
Mat  src,
  cpy_type = COPY_TYPE.CPU_CPU 
)

Copies data from an other Mat (deep copy).

Parameters
src: the Mat where the data will be copied from.
cpy_type: specifies the memories that will be used for the update. Default: COPY_TYPE.CPU_CPU (you cannot change the default value)  
Returns
ERROR_CODE.SUCCESS if everything went well, ERROR_CODE.FAILURE otherwise.

 

Note
If the current Mat doesn't have a compatible MAT_TYPE or Resolution with the source, current memory is freed and new memory is directly allocated.

◆ read()

def read (   self,
str  filepath 
)

Reads an image from a file (only if MEM.CPU is available on the current Mat ).

Supported input files format are PNG and JPEG.  

Parameters
filepath: file path including the name and extension
Returns
ERROR_CODE.SUCCESS if everything went well, ERROR_CODE.FAILURE otherwise.

 

Note
Supported MAT_TYPE are :
- MAT_TYPE.F32_C1 for PNG/PFM/PGM
- MAT_TYPE.F32_C3 for PCD/PLY/VTK/XYZ
- MAT_TYPE.F32_C4 for PCD/PLY/VTK/WYZ
- MAT_TYPE.U8_C1 for PNG/JPG
- MAT_TYPE.U8_C3 for PNG/JPG
- MAT_TYPE.U8_C4 for PNG/JPG

◆ write()

def write (   self,
str  filepath,
  memory_type = MEM.CPU,
  compression_level = -1 
)

 Writes the Mat (only if MEM.CPU is available on the current Mat ) into a file as an image.

Supported output files format are PNG and JPEG.  

Parameters
filepath: file path including the name and extension.  
memory_type: memory type of the Mat. Default: MEM.CPU (you cannot change the default value)  
compression_level: level of compression between 0 (lowest compression == highest size == highest quality(jpg)) and 100 (highest compression == lowest size == lowest quality(jpg)).
Note
Specific/default value for compression_level = -1 : This will set the default quality for PNG(30) or JPEG(5).
compression_level is only supported for U8_Cx.  
Returns
ERROR_CODE.SUCCESS if everything went well, ERROR_CODE.FAILURE otherwise.
Note
Supported MAT_TYPE are :
- MAT_TYPE.F32_C1 for PNG/PFM/PGM
- MAT_TYPE.F32_C3 for PCD/PLY/VTK/XYZ
- MAT_TYPE.F32_C4 for PCD/PLY/VTK/WYZ
- MAT_TYPE.U8_C1 for PNG/JPG
- MAT_TYPE.U8_C3 for PNG/JPG
- MAT_TYPE.U8_C4 for PNG/JPG

◆ set_to()

def set_to (   self,
  value,
  memory_type = MEM.CPU 
)

Fills the Mat with the given value.

This function overwrites all the matrix.  

Parameters
value: the value to be copied all over the matrix.  
memory_type: defines which buffer to fill. Default: MEM.CPU (you cannot change the default value)

◆ set_value()

def set_value (   self,
int  x,
int  y,
  value,
  memory_type = MEM.CPU 
)

Sets a value to a specific point in the matrix.

 

Parameters
x: specifies the column.  
y: specifies the row.  
value: the value to be set.  
memory_type: defines which memory will be updated. Default: MEM.CPU (you cannot change the default value)  
Returns
ERROR_CODE.SUCCESS if everything went well, ERROR_CODE.FAILURE otherwise.

 

Warning
not efficient for GPU, use it on sparse data.

◆ get_value()

def get_value (   self,
int  x,
int  y,
  memory_type = MEM.CPU 
)

Returns the value of a specific point in the matrix.

 

Parameters
x: specifies the column  
y: specifies the row  
memory_type: defines which memory should be read. Default: MEM.CPU (you cannot change this default value)  
Returns
ERROR_CODE.SUCCESS if everything went well, ERROR_CODE.FAILURE otherwise.

◆ get_width()

def get_width (   self)

Returns the width of the matrix.

 

Returns
The width of the matrix in pixels.

◆ get_height()

def get_height (   self)

Returns the height of the matrix.

 

Returns
The height of the matrix in pixels.

◆ get_resolution()

def get_resolution (   self)

Returns the resolution of the matrix.

 

Returns
The resolution of the matrix in pixels.

◆ get_channels()

def get_channels (   self)

Returns the number of values stored in one pixel.

 

Returns
The number of values in a pixel.

◆ get_data_type()

def get_data_type (   self)

Returns the format of the matrix.

 

Returns
The format of the current Mat .

Referenced by Mat.get_value(), Mat.set_to(), and Mat.set_value().

◆ get_memory_type()

def get_memory_type (   self)

Returns the format of the matrix.

 

Returns
The format of the current Mat

◆ get_data()

def get_data (   self,
  memory_type = MEM.CPU 
)

 Copies the data of the Mat in a Numpy array.

 

Parameters
memory_type: defines which memory should be read. Default: MEM.CPU (you cannot change the default value)  
Returns
A Numpy array containing the Mat data.  
Warning
not efficient for GPU, use it on sparse data.

◆ get_step_bytes()

def get_step_bytes (   self,
  memory_type = MEM.CPU 
)

Returns the memory step in Bytes (the Bytes size of one pixel row).

 

Parameters
memory_type: defines which memory should be read. Default: MEM.CPU (you cannot change the default value)  
Returns
The step in bytes of the specified memory.

◆ get_step()

def get_step (   self,
  memory_type = MEM.CPU 
)

Returns the memory step in number of elements (the number of values in one pixel row).

 

Parameters
memory_type: defines which memory should be read. Default: MEM.CPU (you cannot change the default value)  
Returns
The step in number of elements.

◆ get_pixel_bytes()

def get_pixel_bytes (   self)

Returns the size in bytes of one pixel.

 

Returns
The size in bytes of a pixel.

◆ get_width_bytes()

def get_width_bytes (   self)

 Returns the size in bytes of a row.

Returns
The size in bytes of a row.

◆ get_infos()

def get_infos (   self)

 Returns the information about the Mat into a string.

Returns
A string containing the Mat information.

Referenced by Matrix4f.m(), and Mat.verbose().

◆ is_init()

def is_init (   self)

Defines whether the Mat is initialized or not.

Returns
True if current Mat has been allocated (by the constructor or therefore).

◆ is_memory_owner()

def is_memory_owner (   self)

Returns whether the Mat is the owner of the memory it accesses.

If not, the memory won't be freed if the Mat is destroyed.  

Returns
True if the Mat is owning its memory, else false.

◆ clone()

def clone (   self,
Mat  py_mat 
)

Duplicates Mat by copy (deep copy).

 

Parameters
py_mat: the reference to the Mat to copy. This function copies the data array(s), it marks the new Mat as the memory owner.

◆ move()

def move (   self,
Mat  py_mat 
)

Moves Mat data to another Mat.

This function gives the attribute of the current Mat to the specified one. (No copy).  

Parameters
py_mat: the Mat to move.

 

Note
the current Mat is then no more usable since it loses its attributes.

Referenced by Mat.__cinit__(), Mat.init_mat(), Mat.init_mat_cpu(), Mat.init_mat_resolution(), Mat.init_mat_resolution_cpu(), and Mat.init_mat_type().

◆ swap()

def swap ( Mat  mat1,
Mat  mat2 
)
static

Swaps the content of the provided Mat (only swaps the pointers, no data copy).

Static Method.

This function swaps the pointers of the given Mat.  

Parameters
mat1: the first mat.  
mat2: the second mat.

◆ get_pointer()

def get_pointer (   self,
  memory_type = MEM.CPU 
)

Gets the pointer of the content of the Mat.

Parameters
memory_type: Defines which memory you want to get. Default: MEM.CPU (you cannot change the default value)  
Returns
the pointer of the content of the Mat.

◆ name()

def name (   self)

The name of the Mat (optional).

In verbose mode, it's used to indicate which Mat is printing information. Default set to "n/a" to avoid empty string if not filled.