Mat Class Reference

Class representing 1 to 4-channel matrix of float or uchar, stored on CPU and/or GPU side. More...

Functions

 Mat ()
 Default constructor. More...
 
 Mat (size_t width, size_t height, MAT_TYPE mat_type, MEM memory_type=MEM::CPU)
 Constructor. More...
 
 Mat (size_t width, size_t height, MAT_TYPE mat_type, sl::uchar1 *ptr, size_t step, MEM memory_type=MEM::CPU)
 Constructor from an existing data pointer. More...
 
 Mat (size_t width, size_t height, MAT_TYPE mat_type, sl::uchar1 *ptr_cpu, size_t step_cpu, sl::uchar1 *ptr_gpu, size_t step_gpu)
 Constructor from two existing data pointers, CPU and GPU. More...
 
 Mat (Resolution resolution, MAT_TYPE mat_type, MEM memory_type=MEM::CPU)
 Constructor from an sl::Resolution. More...
 
 Mat (Resolution resolution, MAT_TYPE mat_type, sl::uchar1 *ptr, size_t step, MEM memory_type=MEM::CPU)
 Constructor from an existing data pointer. More...
 
 Mat (Resolution resolution, MAT_TYPE mat_type, sl::uchar1 *ptr_cpu, size_t step_cpu, sl::uchar1 *ptr_gpu, size_t step_gpu)
 Constructor from two existing data pointers, CPU and GPU. More...
 
 Mat (const Mat &mat)
 Copy constructor (shallow copy). More...
 
void alloc (size_t width, size_t height, MAT_TYPE mat_type, MEM memory_type=MEM::CPU)
 Allocates the sl::Mat memory. More...
 
void alloc (Resolution resolution, MAT_TYPE mat_type, MEM memory_type=MEM::CPU)
 Allocates the sl::Mat memory. More...
 
 ~Mat ()
 Destructor. More...
 
void free (MEM memory_type=MEM::CPU|MEM::GPU)
 Free the owned memory. More...
 
Matoperator= (const Mat &that)
 Performs a shallow copy. More...
 
ERROR_CODE updateCPUfromGPU (cudaStream_t stream=0)
 Downloads data from DEVICE (GPU) to HOST (CPU), if possible. More...
 
ERROR_CODE updateGPUfromCPU (cudaStream_t stream=0, int GPU_id=0)
 Uploads data from HOST (CPU) to DEVICE (GPU), if possible. More...
 
ERROR_CODE copyTo (Mat &dst, COPY_TYPE cpyType=COPY_TYPE::CPU_CPU, cudaStream_t stream=0) const
 Copies data an other sl::Mat (deep copy). More...
 
ERROR_CODE setFrom (const Mat &src, COPY_TYPE cpyType=COPY_TYPE::CPU_CPU, cudaStream_t stream=0)
 Copies data from an other sl::Mat (deep copy). More...
 
ERROR_CODE read (const String &filePath)
 Reads an image from a file. More...
 
ERROR_CODE write (const String &filePath, sl::MEM memory_type=sl::MEM::CPU, int compression_level=-1)
 Writes the sl::Mat (only if sl::MEM::CPU is available) into a file defined by its extension. More...
 
template<typename T >
ERROR_CODE setTo (T value, sl::MEM memory_type=MEM::CPU)
 Fills the sl::Mat with the given value. More...
 
template<typename N >
ERROR_CODE setValue (size_t x, size_t y, N value, sl::MEM memory_type=MEM::CPU)
 Sets a value to a specific point in the matrix. More...
 
template<typename N >
ERROR_CODE getValue (size_t x, size_t y, N *value, MEM memory_type=MEM::CPU) const
 Returns the value of a specific point in the matrix. More...
 
size_t getWidth () const
 Returns the width of the matrix. More...
 
size_t getHeight () const
 Returns the height of the matrix. More...
 
Resolution getResolution () const
 Returns the resolution (width and height) of the matrix. More...
 
size_t getChannels () const
 Returns the number of values stored in one pixel. More...
 
MAT_TYPE getDataType () const
 Returns the format of the matrix. More...
 
MEM getMemoryType () const
 Returns the type of memory (CPU and/or GPU). More...
 
template<typename N >
N * getPtr (MEM memory_type=MEM::CPU) const
 Returns the CPU or GPU data pointer. More...
 
size_t getStepBytes (MEM memory_type=MEM::CPU) const
 Returns the memory step in bytes (size of one pixel row). More...
 
template<typename N >
size_t getStep (MEM memory_type=MEM::CPU) const
 Returns the memory step in number of elements (size in one pixel row). More...
 
size_t getStep (MEM memory_type=MEM::CPU) const
 Returns the memory step in number of elements (size in one pixel row). More...
 
size_t getPixelBytes () const
 Returns the size of one pixel in bytes. More...
 
size_t getWidthBytes () const
 Returns the size of a row in bytes. More...
 
String getInfos ()
 Returns the information about the sl::Mat into a sl::String. More...
 
bool isInit () const
 Returns whether the sl::Mat is initialized or not. More...
 
bool isMemoryOwner () const
 Returns whether the sl::Mat is the owner of the memory it accesses. More...
 
ERROR_CODE clone (const Mat &src)
 Duplicates a sl::Mat by copy (deep copy). More...
 
ERROR_CODE move (Mat &dst)
 Moves the data of the sl::Mat to another sl::Mat. More...
 

Static Functions

static void swap (Mat &mat1, Mat &mat2)
 Swaps the content of the provided sl::Mat (only swaps the pointers, no data copy). More...
 

Attributes

String name = "n/a"
 Variable used in verbose mode to indicate which sl::Mat is printing informations. More...
 
bool verbose = false
 Whether the sl::Mat can display informations. More...
 
Timestamp timestamp = 0
 Timestamp of the last manipulation of the data of the matrix by a method/function. More...
 

Detailed Description

Class representing 1 to 4-channel matrix of float or uchar, stored on CPU and/or GPU side.

This class is defined in a row-major order, meaning that for an image buffer, the rows are stored consecutively from top to bottom.

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

Constructor and Destructor

◆ Mat() [1/8]

Mat ( )

Default constructor.

The sl::Mat is empty (width = height = 0).

◆ Mat() [2/8]

Mat ( size_t  width,
size_t  height,
MAT_TYPE  mat_type,
MEM  memory_type = MEM::CPU 
)

Constructor.

Initializes the sl::Mat and allocates the requested memory by calling alloc().

Parameters
width: Width of the matrix in pixels.
height: Height of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
memory_type: Where the buffer will be stored (sl::MEM::CPU and/or sl::MEM::GPU).

◆ Mat() [3/8]

Mat ( size_t  width,
size_t  height,
MAT_TYPE  mat_type,
sl::uchar1 ptr,
size_t  step,
MEM  memory_type = MEM::CPU 
)

Constructor from an existing data pointer.

Initializes the sl::Mat but does not allocate the memory.

Parameters
width: Width of the matrix in pixels.
height: Height of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
ptr: Pointer to the data array (CPU or GPU).
step: Step of the data array (bytes size of one pixel row).
memory_type: Where the buffer will be stored (sl::MEM::CPU and/or sl::MEM::GPU).

◆ Mat() [4/8]

Mat ( size_t  width,
size_t  height,
MAT_TYPE  mat_type,
sl::uchar1 ptr_cpu,
size_t  step_cpu,
sl::uchar1 ptr_gpu,
size_t  step_gpu 
)

Constructor from two existing data pointers, CPU and GPU.

Initializes the sl::Mat but does not allocate the memory.

Parameters
width: Width of the matrix in pixels.
height: Height of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
ptr_cpu: CPU pointer to the data array.
step_cpu: Step of the CPU data array (bytes size of one pixel row).
ptr_gpu: GPU pointer to the data array.
step_gpu: Step of the GPU data array (bytes size of one pixel row).

◆ Mat() [5/8]

Mat ( Resolution  resolution,
MAT_TYPE  mat_type,
MEM  memory_type = MEM::CPU 
)

Constructor from an sl::Resolution.

Initializes the sl::Mat and allocates the requested memory by calling alloc().

Parameters
resolution: Size of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
memory_type: Where the buffer will be stored (sl::MEM::CPU and/or sl::MEM::GPU).

◆ Mat() [6/8]

Mat ( Resolution  resolution,
MAT_TYPE  mat_type,
sl::uchar1 ptr,
size_t  step,
MEM  memory_type = MEM::CPU 
)

Constructor from an existing data pointer.

Initializes the sl::Mat but does not allocate the memory.

Parameters
resolution: Size of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
ptr: Pointer to the data array (CPU or GPU).
step: Step of the data array (bytes size of one pixel row).
memory_type: Where the buffer will be stored (sl::MEM::CPU and/or sl::MEM::GPU).

◆ Mat() [7/8]

Mat ( Resolution  resolution,
MAT_TYPE  mat_type,
sl::uchar1 ptr_cpu,
size_t  step_cpu,
sl::uchar1 ptr_gpu,
size_t  step_gpu 
)

Constructor from two existing data pointers, CPU and GPU.

Initializes the sl::Mat but does not allocate the memory.

Parameters
resolution: Size of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
ptr_cpu: CPU pointer to the data array.
step_cpu: Step of the CPU data array (bytes size of one pixel row).
ptr_gpu: GPU pointer to the data array.
step_gpu: Step of the GPU data array (bytes size of one pixel row).

◆ Mat() [8/8]

Mat ( const Mat mat)

Copy constructor (shallow copy).

Initializes the sl::Mat but does not allocate the memory.

Parameters
mat: Reference to the sl::Mat to copy.

◆ ~Mat()

~Mat ( )

Destructor.

This method calls free to release owned memory.

Functions

◆ alloc() [1/2]

void alloc ( size_t  width,
size_t  height,
MAT_TYPE  mat_type,
MEM  memory_type = MEM::CPU 
)

Allocates the sl::Mat memory.

Parameters
width: Width of the matrix in pixels.
height: Height of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
memory_type: Where the buffer will be stored (sl::MEM::CPU and/or sl::MEM::GPU).
Warning
It erases previously allocated memory.

◆ alloc() [2/2]

void alloc ( Resolution  resolution,
MAT_TYPE  mat_type,
MEM  memory_type = MEM::CPU 
)

Allocates the sl::Mat memory.

Parameters
resolution: Size of the matrix in pixels.
mat_type: Type of the matrix (sl::MAT_TYPE::F32_C1, sl::MAT_TYPE::U8_C4, etc.).
memory_type: Where the buffer will be stored (sl::MEM::CPU and/or sl::MEM::GPU).
Warning
It erases previously allocated memory.

◆ free()

void free ( MEM  memory_type = MEM::CPU|MEM::GPU)

Free the owned memory.

Parameters
memory_type: Specifies whether you want to free the sl::MEM::CPU and/or sl::MEM::GPU memory.

◆ operator=()

Mat& operator= ( const Mat that)

Performs a shallow copy.

This method does not copy the data array, it only copies the pointer.

Parameters
that: sl::Mat to be copied.
Returns
The new sl::Mat object which point to the same data as that.

◆ updateCPUfromGPU()

ERROR_CODE updateCPUfromGPU ( cudaStream_t  stream = 0)

Downloads data from DEVICE (GPU) to HOST (CPU), if possible.

Parameters
stream: Specifies the GPU stream to be used to enable asynchronous overlapping. Default: 0 (synchronous)
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
If no CPU or GPU memory are available for this sl::Mat, some are directly allocated.
If verbose is set to true, you have information in case of failure.

◆ updateGPUfromCPU()

ERROR_CODE updateGPUfromCPU ( cudaStream_t  stream = 0,
int  GPU_id = 0 
)

Uploads data from HOST (CPU) to DEVICE (GPU), if possible.

Parameters
stream: Specifies the GPU stream to be used to enable asynchronous overlapping. Default: 0 (synchronous)
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
If no CPU or GPU memory are available for this sl::Mat, some are directly allocated.
If verbose is set to true, you have information in case of failure.

◆ copyTo()

ERROR_CODE copyTo ( Mat dst,
COPY_TYPE  cpyType = COPY_TYPE::CPU_CPU,
cudaStream_t  stream = 0 
) const

Copies data an other sl::Mat (deep copy).

Parameters
dst: sl::Mat where the data will be copied.
cpyType: Specifies the memory that will be used for the copy.
stream: Specifies the GPU stream to be used to enable asynchronous overlapping. Default: 0 (synchronous)
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
If the destination is not allocated or or doesn't have a compatible sl::MAT_TYPE or sl::Resolution, current memory is freed and new memory is directly allocated.

◆ setFrom()

ERROR_CODE setFrom ( const Mat src,
COPY_TYPE  cpyType = COPY_TYPE::CPU_CPU,
cudaStream_t  stream = 0 
)

Copies data from an other sl::Mat (deep copy).

Parameters
src: sl::Mat where the data will be copied from.
cpyType: Specifies the memory that will be used for the copy.
stream: Specifies the GPU stream to be used to enable asynchronous overlapping. Default: 0 (synchronous)
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
If the destination is not allocated or or doesn't have a compatible sl::MAT_TYPE or sl::Resolution, current memory is freed and new memory is directly allocated.

◆ read()

ERROR_CODE read ( const String filePath)

Reads an image from a file.

Parameters
filePath: Path of the file to read (including the name and extension).
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
This method only support images such as JPG or PNG, and can't load float format such as PCD, PLY, etc.

◆ write()

ERROR_CODE write ( const String filePath,
sl::MEM  memory_type = sl::MEM::CPU,
int  compression_level = -1 
)

Writes the sl::Mat (only if sl::MEM::CPU is available) into a file defined by its extension.

Parameters
filePath: Path of the file to write in (including the name and extension).
memory_type: Memory type (CPU or GPU) of the sl::Mat.
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 sl::MAT_TYPE.
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
Supported sl::MAT_TYPE are:

◆ setTo()

ERROR_CODE setTo ( value,
sl::MEM  memory_type = MEM::CPU 
)

Fills the sl::Mat with the given value.

This method overwrites all the matrix.

Parameters
value: Value to be copied all over the matrix.
memory_type: Which buffer to fill, CPU and/or GPU.
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
This method is templated for uchar1, uchar2, uchar3, uchar4, float1, float2, float3, float4.

◆ setValue()

ERROR_CODE setValue ( size_t  x,
size_t  y,
value,
sl::MEM  memory_type = MEM::CPU 
)

Sets a value to a specific point in the matrix.

Parameters
x: Column of the point to change.
y: Row of the point to change.
value: Value to be set.
memory_type: Which memory will be updated.
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
This method is templated for uchar1, uchar2, uchar3, uchar4, float1, float2, float3, float4.
Warning
Not efficient for sl::MEM::GPU, use it on sparse data.

◆ getValue()

ERROR_CODE getValue ( size_t  x,
size_t  y,
N *  value,
MEM  memory_type = MEM::CPU 
) const

Returns the value of a specific point in the matrix.

Parameters
x: Column of the point to get the value from.
y: Row of the point to get the value from.
memory_type: Which memory should be read.
Returns
sl::ERROR_CODE::SUCCESS if everything went well, sl::ERROR_CODE::FAILURE otherwise.
Note
This method is templated for uchar1, uchar2, uchar3, uchar4, float1, float2, float3, float4.
Warning
Not efficient for sl::MEM::GPU, use it on sparse data.

◆ getWidth()

size_t getWidth ( ) const
inline

Returns the width of the matrix.

Returns
Width of the matrix in pixels.

◆ getHeight()

size_t getHeight ( ) const
inline

Returns the height of the matrix.

Returns
Height of the matrix in pixels.

◆ getResolution()

Resolution getResolution ( ) const
inline

Returns the resolution (width and height) of the matrix.

Returns
Resolution of the matrix in pixels.

◆ getChannels()

size_t getChannels ( ) const
inline

Returns the number of values stored in one pixel.

Returns
Number of values in a pixel.

◆ getDataType()

MAT_TYPE getDataType ( ) const
inline

Returns the format of the matrix.

Returns
Format of the current sl::Mat.

◆ getMemoryType()

MEM getMemoryType ( ) const
inline

Returns the type of memory (CPU and/or GPU).

Returns
Type of allocated memory.

◆ getPtr()

N* getPtr ( MEM  memory_type = MEM::CPU) const

Returns the CPU or GPU data pointer.

Parameters
memory_type: Specifies whether you want sl::MEM::CPU or sl::MEM::GPU.
Returns
The pointer of the Mat data.

◆ getStepBytes()

size_t getStepBytes ( MEM  memory_type = MEM::CPU) const

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

Parameters
memory_type: Specifies whether you want sl::MEM::CPU or sl::MEM::GPU step.
Returns
The step in bytes of the specified memory.

◆ getStep() [1/2]

size_t getStep ( MEM  memory_type = MEM::CPU) const
inline

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

Parameters
memory_type: Specifies whether you want sl::MEM::CPU or sl::MEM::GPU step.
Returns
The step in number of elements.

◆ getStep() [2/2]

size_t getStep ( MEM  memory_type = MEM::CPU) const
inline

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

Parameters
memory_type: Specifies whether you want sl::MEM::CPU or sl::MEM::GPU step.
Returns
The step in number of elements.

◆ getPixelBytes()

size_t getPixelBytes ( ) const
inline

Returns the size of one pixel in bytes.

Returns
Size of a pixel in bytes.

◆ getWidthBytes()

size_t getWidthBytes ( ) const
inline

Returns the size of a row in bytes.

Returns
Size of a row in bytes.

◆ getInfos()

String getInfos ( )

Returns the information about the sl::Mat into a sl::String.

Returns
String containing the sl::Mat information.

◆ isInit()

bool isInit ( ) const
inline

Returns whether the sl::Mat is initialized or not.

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

◆ isMemoryOwner()

bool isMemoryOwner ( ) const
inline

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

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

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

◆ clone()

ERROR_CODE clone ( const Mat src)

Duplicates a sl::Mat by copy (deep copy).

Parameters
src: Reference of the sl::Mat to copy.

This method copies the data array(s) and it marks the new sl::Mat as the memory owner.

◆ move()

ERROR_CODE move ( Mat dst)

Moves the data of the sl::Mat to another sl::Mat.

This method gives the attribute of the current s::Mat to the specified one. (No copy.)

Parameters
dst: Reference to the sl::Mat to move to.
Note
: The current sl::Mat is then no more usable since its loose its attributes.

◆ swap()

static void swap ( Mat mat1,
Mat mat2 
)
static

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

This method swaps the pointers of the given sl::Mat.

Parameters
mat1: First matrix to swap.
mat2: Second matrix to swap.

Variables

◆ name

String name = "n/a"

Variable used in verbose mode to indicate which sl::Mat is printing informations.

Default set to "n/a" to avoid empty string if not filled.

◆ verbose

bool verbose = false

Whether the sl::Mat can display informations.

◆ timestamp

Timestamp timestamp = 0

Timestamp of the last manipulation of the data of the matrix by a method/function.