VPI - Vision Programming Interface

3.0 Release

OpenCV Interoperability

Interoperability with OpenCV library. More...

Functions

VPIStatus vpiImageCreateWrapperOpenCVMat (const cv::Mat &mat, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
 Wraps a cv::Mat in an VPIImage with the given image format. More...
 
VPIStatus vpiImageCreateWrapperOpenCVMat (const cv::Mat &mat, uint64_t flags, VPIImage *img)
 Wraps a cv::Mat in a VPIImage and infers the image format from its type. More...
 
VPIStatus vpiImageSetWrappedOpenCVMat (VPIImage img, const cv::Mat &mat)
 Redefines the wrapped cv::Mat of an existing VPIImage wrapper. More...
 
VPIStatus vpiImageCreateWrapperOpenCVMatPlanes (const std::vector< cv::Mat > &matPlanes, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
 Wraps a vector of cv::Mats in a VPIImage, each cv::Mat representing one image plane. More...
 
VPIStatus vpiImageSetWrappedOpenCVMatPlanes (VPIImage img, const std::vector< cv::Mat > &matPlanes)
 Replaces the wrapped cv::Mat planes with new ones. More...
 
VPIStatus vpiImageDataExportOpenCVMat (const VPIImageData &imgData, cv::Mat *mat)
 Fills an existing cv::Mat with data from VPIImageData coming from a locked VPIImage. More...
 
VPIStatus vpiImageDataExportOpenCVMatPlanes (const VPIImageData &imgData, std::vector< cv::Mat > *matPlanes)
 Fills a vector of cv::Mat with all planes from VPIImageData. More...
 
VPIStatus vpiImageDataImportOpenCVMat (const cv::Mat &mat, VPIImageData *imgData)
 Fills an existing VPIImageData with data from given cv::Mat. More...
 
VPIStatus vpiImageDataImportOpenCVMat (const cv::Mat &mat, VPIImageFormat fmt, VPIImageData *imgData)
 Fills an existing VPIImageData with data from given cv::Mat, forcing the given image format. More...
 
VPIStatus vpiImageDataImportOpenCVMatPlanes (const std::vector< cv::Mat > &matPlanes, VPIImageFormat fmt, VPIImageData *imgData)
 Fills a vector of cv::Mat describing all planes from given VPIImageData forcing the given image format. More...
 

Detailed Description

Interoperability with OpenCV library.

VPI allows easy interoperability with OpenCV. This makes it easy to wrap existing cv::Mat object into VPIImage to be used in VPI pipelines.

It's also possible to create a cv::Mat out of a VPIImageData.

Function Documentation

◆ vpiImageCreateWrapperOpenCVMat() [1/2]

VPIStatus vpiImageCreateWrapperOpenCVMat ( const cv::Mat &  mat,
VPIImageFormat  fmt,
uint64_t  flags,
VPIImage img 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Wraps a cv::Mat in an VPIImage with the given image format.

It also wraps YUV images in 4:2:2 (packed and semi-planar) and 4:2:0 (semi-planar) formats that are understood by cv::cvtColor.

The cv::Mat must not be destroyed while there's a VPIImage still wrapping it. Failure to do so leads to undefined behavior.

Parameters
[in]matThe OpenCV matrix to be wrapped.
  • Must not be empty.
  • Number of dimensions must be 2.
[in]fmtThe image format associated with mat.
  • Format must be consistent with mat dimensions and type.
[in]flagsBit field specifying the desired characteristics of the image. VPI_BACKEND_CPU flag is always added internally.
  • The field must be a combination of zero or more of the following flags:
    • VPIBackend flags. This image can be used in algorithms running in these backends. If no backend flags are passed, it'll consider all backends supported by the active VPIContext, but disable the backends that are incompatible with the given image parameters.
    • Common object flags.
  • If backends are given, they must enabled in current context.
[out]imgPointer to memory that will receive the created image handle.
Return values
VPI_ERROR_INVALID_IMAGE_FORMATfmt is invalid or inconsistent with mat.
VPI_ERROR_INVALID_ARGUMENTmat is empty or has wrong number of dimensions.
VPI_ERROR_INVALID_ARGUMENTOutput img is NULL.
VPI_ERROR_OUT_OF_MEMORYNot enough resources to create image.
VPI_ERROR_INVALID_CONTEXTCurrent context is destroyed.
VPI_ERROR_INVALID_OPERATIONRequested backend isn't enabled in current context.
VPI_SUCCESSOperation executed successfully.

Definition at line 117 of file OpenCVInterop.hpp.

+ Here is the call graph for this function:

◆ vpiImageCreateWrapperOpenCVMat() [2/2]

VPIStatus vpiImageCreateWrapperOpenCVMat ( const cv::Mat &  mat,
uint64_t  flags,
VPIImage img 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Wraps a cv::Mat in a VPIImage and infers the image format from its type.

The rules to infer the image type are:

  • unsigned and float 3-channel images are taken to be BGR
  • unsigned and float 4-channel images are taken to be BGRA
  • other types are mapped to non-color vpi image formats with the same pixel layout.

The cv::Mat must not be destroyed while there's a VPIImage still wrapping it. Failure to do so leads to undefined behavior.

Parameters
[in]matThe OpenCV matrix to be wrapped.
  • Number of dimensions must be 2.
[in]flagsBit field specifying the desired characteristics of the image. VPI_BACKEND_CPU flag is always added internally.
  • The field must be a combination of zero or more of the following flags:
    • VPIBackend flags. This image can be used in algorithms running in these backends. If no backend flags are passed, it'll consider all backends supported by the active VPIContext, but disable the backends that are incompatible with the given image parameters.
    • Common object flags
  • If backends are given, they must enabled in current context.
[out]imgPointer to memory that will receive the created image handle.
Return values
VPI_ERROR_INVALID_IMAGE_FORMATmat type is not supported.
VPI_ERROR_INVALID_ARGUMENTmat is empty or has wrong number of dimensions.
VPI_ERROR_INVALID_ARGUMENTOutput img is NULL.
VPI_ERROR_OUT_OF_MEMORYNot enough resources to create image.
VPI_ERROR_INVALID_CONTEXTCurrent context is destroyed.
VPI_ERROR_INVALID_OPERATIONRequested backend isn't enabled in current context.
VPI_SUCCESSOperation executed successfully.

Definition at line 166 of file OpenCVInterop.hpp.

+ Here is the call graph for this function:

◆ vpiImageSetWrappedOpenCVMat()

VPIStatus vpiImageSetWrappedOpenCVMat ( VPIImage  img,
const cv::Mat &  mat 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Redefines the wrapped cv::Mat of an existing VPIImage wrapper.

The new cv::Mat must have the same characteristics (type and dimensions) of the existing cv::Mat being wrapped.

This operation is efficient and does not allocate memory. The wrapped memory will be accessible to the same backends specified during wrapper creation.

The wrapped cv::Mat must be valid while it's still being wrapped.

Parameters
[in]imgHandle to an image.
[in]matcv::Mat object to be wrapped.
  • Number of dimensions must be 2.
  • The existing wrapped image and mat must have same dimensions and compatible formats.
Return values
VPI_ERROR_INVALID_ARGUMENTmat is empty or has wrong number of dimensions.
VPI_ERROR_INVALID_ARGUMENTOutput img is NULL.
VPI_ERROR_INVALID_IMAGE_FORMATmat type is not supported.
VPI_ERROR_INVALID_OPERATIONmat dimensions and/or format don't match img.
VPI_ERROR_INVALID_OPERATIONimg is locked.
VPI_ERROR_INVALID_OPERATIONimg is not created by vpiImageCreateWrapperOpenCVMat or vpiImageCreateWrapper with correct buffer type.
VPI_SUCCESSOperation executed successfully.

Definition at line 207 of file OpenCVInterop.hpp.

+ Here is the call graph for this function:

◆ vpiImageCreateWrapperOpenCVMatPlanes()

VPIStatus vpiImageCreateWrapperOpenCVMatPlanes ( const std::vector< cv::Mat > &  matPlanes,
VPIImageFormat  fmt,
uint64_t  flags,
VPIImage img 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Wraps a vector of cv::Mats in a VPIImage, each cv::Mat representing one image plane.

The wrapped cv::Mats must not be destroyed while there's a VPIImage still wrapping it. The std::vector object itself isn't used so it can be destroyed, or even be temporary. Failure to keep cv::Mats allocated leads to undefined behavior.

Parameters
[in]matPlanesVector of cv::Mat, each one representing one image plane.
  • Vector can't be empty.
  • cv::Mat in each plane must not be empty.
  • cv::Mat dimensions must be 2.
[in]fmtThe image format associated with matPlanes.
  • Format must be consistent with matPlanes. Namely, the number of planes in fmt must match matPlanes size, and each plane must be consistent with corresponding cv::Mat type.
[in]flagsBit field specifying the desired characteristics of the image. VPI_BACKEND_CPU flag is always added internally.
  • The field must be a combination of zero or more of the following flags:
    • VPIBackend flags. This image can be used in algorithms running in these backends. If no backend flags are passed, it'll consider all backends supported by the active VPIContext, but disable the backends that are incompatible with the given image parameters.
    • Common object flags
  • If backends are given, they must enabled in current context.
[out]imgPointer to memory that will receive the created image handle.
Return values
VPI_ERROR_INVALID_IMAGE_FORMATfmt is not supported.
VPI_ERROR_INVALID_IMAGE_FORMATmatPlanes type not consistent with fmt.
VPI_ERROR_INVALID_ARGUMENTSome cv::Mat in matPlanes is empty, or matPlanes is empty.
VPI_ERROR_INVALID_OPERATIONRequested backend isn't enabled in current context.
VPI_SUCCESSOperation executed successfully.

Definition at line 263 of file OpenCVInterop.hpp.

+ Here is the call graph for this function:

◆ vpiImageSetWrappedOpenCVMatPlanes()

VPIStatus vpiImageSetWrappedOpenCVMatPlanes ( VPIImage  img,
const std::vector< cv::Mat > &  matPlanes 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Replaces the wrapped cv::Mat planes with new ones.

The new cv::Mat vector must have the same size, and contained cv::Mats must have the same characteristics (type and dimensions) of the existing cv::Mats being wrapped.

The VPIImage must have been created by vpiImageCreateWrapperOpenCVMatPlanes.

This operation is efficient and does not allocate memory. The wrapped memory will be accessible to the same backends specified during wrapper creation.

The wrapped cv::Mats must be valid while it's still being wrapped. The std::vector object itself can be destroyed, or even be temporary.

Parameters
[in]imgHandle to an image.
[in]matPlanesVector of cv::Mat, each one representing one image plane.
Return values
VPI_ERROR_INVALID_IMAGE_FORMATImage format is not supported.
VPI_ERROR_INVALID_ARGUMENTInput matPlanes is NULL.
VPI_ERROR_INVALID_ARGUMENTInput imgData is NULL.
VPI_ERROR_INVALID_OPERATIONimg is not created by vpiImageCreateWrapperOpenCVMat or vpiImageCreateWrapper with correct buffer type.
VPI_SUCCESSOperation executed successfully.

Definition at line 304 of file OpenCVInterop.hpp.

+ Here is the call graph for this function:

◆ vpiImageDataExportOpenCVMat()

VPIStatus vpiImageDataExportOpenCVMat ( const VPIImageData imgData,
cv::Mat *  mat 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Fills an existing cv::Mat with data from VPIImageData coming from a locked VPIImage.

No image contents will be copied, they're just referenced by cv::Mat. The result can be used in OpenCV operations.

If imgData has 2 or 3 planes that are contiguous in memory and represent some multi-planar formats as understood by cv::cvtColor such as NV12 or YUV422, the resulting mat will be filled so that cv::cvtColor understand it as having these types. In other works, they can be used with cv::COLOR_YUV2RGB_NV12 or COLOR_YUV2RGB_UYVY.

Parameters
[in]imgDataReference to VPIImageData returned by vpiImageLockData. If the pixel type of a plane is VPI_PIXEL_TYPE_DEFAULT, it'll be inferred from the image format given.
  • Image format must be representable by a cv::Mat.
[out]matcv::Mat that will refer to imgData contents.
  • Mandatory, can't be NULL.
Return values
VPI_ERROR_INVALID_ARGUMENTOutput mat is NULL.
VPI_ERROR_INVALID_IMAGE_FORMATimgData is invalid.
VPI_SUCCESSOperation executed successfully.

Definition at line 346 of file OpenCVInterop.hpp.

◆ vpiImageDataExportOpenCVMatPlanes()

VPIStatus vpiImageDataExportOpenCVMatPlanes ( const VPIImageData imgData,
std::vector< cv::Mat > *  matPlanes 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Fills a vector of cv::Mat with all planes from VPIImageData.

No image contents will be copied, they're just referenced by cv::Mat. The result can be used in OpenCV operations.

Parameters
[in]imgDataReference to VPIImageData returned by vpiImageLockData. If the pixel type of a plane is VPI_PIXEL_TYPE_DEFAULT, it'll be inferred from the image format given.
[out]matPlanesVector of cv::Mat, each one wrapping one image plane in imgData.
  • Mandatory, can't be NULL.
Return values
VPI_ERROR_INVALID_ARGUMENTOutput matPlanes is NULL.
VPI_ERROR_INVALID_IMAGE_FORMATimgData is invalid.
VPI_SUCCESSOperation executed successfully.

Definition at line 375 of file OpenCVInterop.hpp.

◆ vpiImageDataImportOpenCVMat() [1/2]

VPIStatus vpiImageDataImportOpenCVMat ( const cv::Mat &  mat,
VPIImageData imgData 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Fills an existing VPIImageData with data from given cv::Mat.

It performs the inverse operation of vpiImageDataExportOpenCVMat. Please refer to this function's description on what is the criteria used for image format deduction.

Parameters
[in]matReference to cv::Mat with information to be filled in output
  • It must not be empty.
  • cv::Mat number of dimensions equal to 2.
[out]imgDataVPIImageData that will be filled. Existing contents are discarded.
  • Mandatory, must not be NULL.
Return values
VPI_ERROR_INVALID_ARGUMENTInput mat is empty.
VPI_ERROR_INVALID_ARGUMENTmat has wrong number of dimensions.
VPI_ERROR_INVALID_ARGUMENTimgData is NULL.
VPI_SUCCESSOperation executed successfully.

Definition at line 405 of file OpenCVInterop.hpp.

◆ vpiImageDataImportOpenCVMat() [2/2]

VPIStatus vpiImageDataImportOpenCVMat ( const cv::Mat &  mat,
VPIImageFormat  fmt,
VPIImageData imgData 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Fills an existing VPIImageData with data from given cv::Mat, forcing the given image format.

It performs the inverse operation of vpiImageDataExportOpenCVMat. Please refer to this function's description on what is the criteria used for image format deduction.

Parameters
[in]matReference to cv::Mat with information to be filled in output
  • It must not be empty.
  • cv::Mat number of dimensions equal to 2.
[in]fmtThe image format associated with mat.
  • It must be consistent with mat dimesions and type.
[out]imgDataVPIImageData that will be filled. Existing contents are discarded.
  • Mandatory, must not be NULL.
Return values
VPI_ERROR_INVALID_ARGUMENTInput mat is empty.
VPI_ERROR_INVALID_ARGUMENTimgData is NULL.
VPI_ERROR_INVALID_ARGUMENTmat has wrong number of dimensions.
VPI_ERROR_INVALID_IMAGE_FORMATfmt not consistent with mat.
VPI_SUCCESSOperation executed successfully.

Definition at line 440 of file OpenCVInterop.hpp.

◆ vpiImageDataImportOpenCVMatPlanes()

VPIStatus vpiImageDataImportOpenCVMatPlanes ( const std::vector< cv::Mat > &  matPlanes,
VPIImageFormat  fmt,
VPIImageData imgData 
)
inline

#include </opt/nvidia/vpi3/include/vpi/OpenCVInterop.hpp>

Fills a vector of cv::Mat describing all planes from given VPIImageData forcing the given image format.

It performs the inverse operation of vpiImageDataExportOpenCVMatPlanes. Please refer to this function's description on what is the criteria used for image format deduction.

The image format isn't deduced from the input cv::Mat, but is given by the user. If the format isn't compatible with the cv::Mat, an error is returned.

Parameters
[in]matPlanesVector of cv::Mat, each describing one plane.
  • It must not be empty.
  • cv::Mat in each plane must not be empty.
  • cv::Mat in each plane must have number of dimensions equal to 2.
[in]fmtThe image format associated with matPlanes.
  • It must be consistent with the matPlanes size and the contained cv::Mat's dimensions and type.
[out]imgDataVPIImageData that will be filled. Existing contents are discarded.
  • Mandatory, must not be NULL.
Return values
VPI_ERROR_INVALID_ARGUMENTimgData is NULL.
VPI_ERROR_INVALID_ARGUMENTmatPlanes is empty.
VPI_ERROR_INVALID_ARGUMENTSome cv::Mat in matPlanes is empty or has wrong number of dimensions.
VPI_ERROR_INVALID_IMAGE_FORMATfmt not consistent with matPlanes.
VPI_SUCCESSOperation executed successfully.

Definition at line 480 of file OpenCVInterop.hpp.