OpenCV for Unity 3.0.0
Enox Software / Please refer to OpenCV official document ( http://docs.opencv.org/4.12.0/index.html ) for the details of the argument of the method.
Loading...
Searching...
No Matches
OpenCVForUnity.UnityIntegration.OpenCVMatUtils Class Reference

OpenCV Mat utilities. More...

Public Types

enum  CopyMode {
  PerPixel ,
  Contiguous
}
 Enumeration to specify the data copy method. More...
 

Static Public Member Functions

static int CopyFromMat (Mat mat, IntPtr intPtr)
 Copies the data from an OpenCV Mat to an IntPtr.
 
static int CopyFromMat (Mat mat, IntPtr intPtr, int length)
 Copies the data from an OpenCV Mat to an IntPtr.
 
static int CopyFromMat< T > (Mat mat, IntPtr intPtr, int length)
 Copies the data from an OpenCV Mat to an IntPtr.
 
static int CopyFromMat< T > (Mat mat, NativeArray< T > array)
 Copies the data from an OpenCV Mat to a NativeArray.
 
static int CopyFromMat< T > (Mat mat, NativeArray< T > array, int length)
 Copies the data from an OpenCV Mat to a NativeArray.
 
static int CopyFromMat< T > (Mat mat, Span< T > array)
 Copies the data from an OpenCV Mat to a Span.
 
static int CopyFromMat< T > (Mat mat, Span< T > array, int length)
 Copies the data from an OpenCV Mat to a Span.
 
static int CopyFromMat< T > (Mat mat, T[] array)
 Copies the data from an OpenCV Mat to a managed array.
 
static int CopyFromMat< T > (Mat mat, T[] array, int length)
 Copies the data from an OpenCV Mat to a managed array.
 
static int CopyToMat (IntPtr intPtr, Mat mat)
 Copies the data from an IntPtr to an OpenCV Mat.
 
static int CopyToMat (IntPtr intPtr, Mat mat, int length)
 Copies the data from an IntPtr to an OpenCV Mat.
 
static int CopyToMat< T > (IntPtr intPtr, Mat mat, int length)
 Copies the data from an IntPtr to an OpenCV Mat.
 
static int CopyToMat< T > (NativeArray< T > array, Mat mat)
 Copies the data from a NativeArray to an OpenCV Mat.
 
static int CopyToMat< T > (NativeArray< T > array, Mat mat, int length)
 Copies the data from a NativeArray to an OpenCV Mat.
 
static int CopyToMat< T > (ReadOnlySpan< T > array, Mat mat)
 Copies the data from a Span to an OpenCV Mat.
 
static int CopyToMat< T > (ReadOnlySpan< T > array, Mat mat, int length)
 Copies the data from a Span to an OpenCV Mat.
 
static int CopyToMat< T > (T[] array, Mat mat)
 Copies the data from a managed array to an OpenCV Mat.
 
static int CopyToMat< T > (T[] array, Mat mat, int length)
 Copies the data from a managed array to an OpenCV Mat.
 
static ComputeShader CreateMatToRenderTextureComputeShader (GraphicsBuffer graphicsBuffer, RenderTexture renderTexture, bool flip=true, int flipCode=0)
 Creates a ComputeShader for converting from Mat to RenderTexture based on the current platform and graphics API. This ComputeShader is intended to be used with the kernel index obtained from the GetKernelIndexForMatToRenderTextureComputeShader method when dispatching.
 
static ComputeShader CreateRenderTextureToMatComputeShader (RenderTexture renderTexture, GraphicsBuffer graphicsBuffer, bool flip=true, int flipCode=0)
 Creates a ComputeShader for converting from RenderTexture to Mat based on the current platform and graphics API. This ComputeShader is intended to be used with the kernel index obtained from the GetKernelIndexForRenderTextureToMatComputeShader method when dispatching.
 
static int GetKernelIndexForMatToRenderTextureComputeShader (GraphicsBuffer graphicsBuffer, RenderTexture renderTexture)
 Gets the kernel index to use for converting from Mat to RenderTexture based on the current platform and graphics API. This kernel index is intended to be used when dispatching the ComputeShader obtained from the CreateMatToRenderTextureComputeShader method.
 
static int GetKernelIndexForRenderTextureToMatComputeShader (RenderTexture renderTexture, GraphicsBuffer graphicsBuffer)
 Gets the kernel index to use for converting from RenderTexture to Mat based on the current platform and graphics API. This kernel index is intended to be used when dispatching the ComputeShader obtained from the CreateRenderTextureToMatComputeShader method.
 
static void GraphicsBufferToMat (GraphicsBuffer graphicsBuffer, Mat mat, CopyMode copyMode)
 Copies data from a Unity GraphicsBuffer to a OpenCV Mat.
 
static async Awaitable GraphicsBufferToMatAsync (GraphicsBuffer graphicsBuffer, Mat mat, CopyMode copyMode, CancellationToken cancellationToken=default)
 Copies data from a Unity GraphicsBuffer to an OpenCV Mat asynchronously.
 
static void MatToGraphicsBuffer (Mat mat, GraphicsBuffer graphicsBuffer, CopyMode copyMode)
 Copies data from a OpenCv Mat to a Unity GraphicsBuffer.
 
static async Awaitable< AsyncGPUReadbackRequest > MatToGraphicsBufferAsync (Mat mat, GraphicsBuffer graphicsBuffer, CopyMode copyMode, CancellationToken cancellationToken=default)
 Copies data from a OpenCv Mat to a Unity GraphicsBuffer asynchronously.
 
static void MatToRenderTexture (Mat mat, RenderTexture renderTexture, GraphicsBuffer graphicsBuffer, bool flip=true, int flipCode=0)
 Converts an OpenCV Mat to a Unity RenderTexture.
 
static async Awaitable< AsyncGPUReadbackRequest > MatToRenderTextureAsync (Mat mat, RenderTexture renderTexture, GraphicsBuffer graphicsBuffer, bool flip=true, int flipCode=0, CancellationToken cancellationToken=default)
 Converts an OpenCV Mat to a Unity RenderTexture asynchronously.
 
static void MatToTexture2D (Mat mat, Texture2D texture2D, bool flip=true, int flipCode=0, bool updateMipmaps=false, bool makeNoLongerReadable=false)
 Converts an OpenCV Mat to a Unity Texture2D.
 
static void MatToTexture2D (Mat mat, Texture2D texture2D, Color32[] pixels32Buffer, byte[] rawTextureDataBuffer=null, bool flip=true, int flipCode=0, bool updateMipmaps=false, bool makeNoLongerReadable=false)
 Converts an OpenCV Mat to a Unity Texture2D.
 
static void MatToTexture2D (Mat mat, Texture2D texture2D, int mipLevel, bool flip=true, int flipCode=0, bool updateMipmaps=false, bool makeNoLongerReadable=false)
 Converts an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level.
 
static void MatToTexture2D (Mat mat, Texture2D texture2D, int mipLevel, Color32[] pixels32Buffer, bool flip=true, int flipCode=0, bool updateMipmaps=false, bool makeNoLongerReadable=false)
 Converts an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level.
 
static int MatToTexture2DRaw (Mat mat, Texture2D texture2D, bool flip=true, int flipCode=0, bool updateMipmaps=false, bool makeNoLongerReadable=false)
 Copies raw data from an OpenCV Mat to a Unity Texture2D.
 
static int MatToTexture2DRaw (Mat mat, Texture2D texture2D, int mipLevel, bool flip=true, int flipCode=0, bool updateMipmaps=false, bool makeNoLongerReadable=false)
 Copies raw data from an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level.
 
static void RenderTextureToMat (RenderTexture renderTexture, Mat mat, GraphicsBuffer graphicsBuffer, bool flip=true, int flipCode=0)
 Converts a Unity RenderTexture to an OpenCV Mat.
 
static async Awaitable RenderTextureToMatAsync (RenderTexture renderTexture, Mat mat, GraphicsBuffer graphicsBuffer, bool flip=true, int flipCode=0, CancellationToken cancellationToken=default)
 Converts a Unity RenderTexture to an OpenCV Mat asynchronously.
 
static void Texture2DToMat (Texture2D texture2D, Mat mat, bool flip=true, int flipCode=0)
 Converts a Unity Texture2D to an OpenCV Mat.
 
static void Texture2DToMat (Texture2D texture2D, Mat mat, int mipLevel, bool flip=true, int flipCode=0)
 Converts a Unity Texture2D to an OpenCV Mat, targeting the specified mipmap level.
 
static int Texture2DToMatRaw (Texture2D texture2D, Mat mat, bool flip=true, int flipCode=0)
 Copies raw data from a Unity Texture2D to an OpenCV Mat.
 
static int Texture2DToMatRaw (Texture2D texture2D, Mat mat, int mipLevel, bool flip=true, int flipCode=0)
 Copies raw data from a Unity Texture2D to an OpenCV Mat, targeting the specified mipmap level.
 
static async Awaitable TextureToMatAsync (Texture texture, Mat mat, bool flip=true, int flipCode=0, int mipIndex=0, int x=0, int width=-1, int y=0, int height=-1, int z=0, int depth=1, GraphicsFormat? dstFormat=null, CancellationToken cancellationToken=default)
 Converts a Texture to an OpenCV Mat asynchronously.
 
static async Awaitable TextureToMatAsync (Texture texture, Mat mat, CancellationToken cancellationToken=default)
 Converts a Texture to an OpenCV Mat asynchronously.
 
static void TextureToTexture2D (Texture texture, Texture2D texture2D)
 OPENCV_DONT_USE_WEBCAMTEXTURE_API.
 
static void WebCamTextureToMat (WebCamTexture webCamTexture, Mat mat, bool flip=true, int flipCode=0)
 Converts a Unity WebCamTexture to an OpenCV Mat.
 
static void WebCamTextureToMat (WebCamTexture webCamTexture, Mat mat, Color32[] pixels32Buffer, bool flip=true, int flipCode=0)
 Converts a Unity WebCamTexture to an OpenCV Mat.
 

Detailed Description

OpenCV Mat utilities.

Member Enumeration Documentation

◆ CopyMode

Enumeration to specify the data copy method.

Enumerator
PerPixel 

Copies data on a per-pixel basis. If the input is 4 bytes per pixel and the output is 8 bytes per pixel, 4 bytes from the input are copied, and the remaining 4 bytes are filled with zeros. If the input is 8 bytes per pixel and the output is 4 bytes per pixel, only the first 4 bytes from the input are copied.

Contiguous 

Copies data as a contiguous block of memory.

Member Function Documentation

◆ CopyFromMat() [1/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat ( Mat mat,
IntPtr intPtr )
static

Copies the data from an OpenCV Mat to an IntPtr.

This method transfers the data stored in an OpenCV Mat object into an IntPtr. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

The entire range of data managed by the Mat will be copied to the IntPtr. If you want to specify the exact range of data to copy, please use the method that accepts a length argument.

Note: This method operates on unmanaged memory, and incorrect usage can lead to unpredictable behavior, crashes, or data corruption. Ensure that the source pointer points to a valid memory region and that the specified length does not exceed the allocated memory size for that region. Additionally, as this method involves unmanaged memory, be cautious about potential security risks. Accessing memory beyond the intended range can lead to buffer overflows and may compromise application stability and security. It is highly recommended to perform validation checks on the pointer and the specified length.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
intPtrThe destination IntPtr where the data will be copied. This pointer must reference a valid memory region.
Returns
The number of bytes copied from the Mat to the IntPtr.
Exceptions
ArgumentNullExceptionThrown when mat is null.
ArgumentExceptionThrown when intPtr == IntPtr.Zero. Thrown when !mat.isContinuous() && mat.dims() > 2.

◆ CopyFromMat() [2/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat ( Mat mat,
IntPtr intPtr,
int length )
static

Copies the data from an OpenCV Mat to an IntPtr.

This method transfers the data stored in an OpenCV Mat object into an IntPtr. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size of the memory region pointed to by the IntPtr is smaller than the data size managed by the Mat, data will be copied up to the size of the IntPtr. If the data size of the memory region pointed to by the IntPtr is larger, data will be copied up to the size managed by the Mat.

Note: This method operates on unmanaged memory, and incorrect usage can lead to unpredictable behavior, crashes, or data corruption. Ensure that the source pointer points to a valid memory region and that the specified length does not exceed the allocated memory size for that region. Additionally, as this method involves unmanaged memory, be cautious about potential security risks. Accessing memory beyond the intended range can lead to buffer overflows and may compromise application stability and security. It is highly recommended to perform validation checks on the pointer and the specified length.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
intPtrThe destination IntPtr where the data will be copied. This pointer must reference a valid memory region.
lengthThe number of elements, in terms of byte size, to copy from the source Mat to the destination IntPtr. The length represents the size in bytes, regardless of the actual type of data in the Mat or the destination.
Returns
The number of bytes copied from the Mat to the IntPtr.
Exceptions
ArgumentNullExceptionThrown when mat is null.
ArgumentExceptionThrown when intPtr == IntPtr.Zero. Thrown when !mat.isContinuous() && mat.dims() > 2.

◆ CopyFromMat< T >() [1/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat< T > ( Mat mat,
IntPtr intPtr,
int length )
static

Copies the data from an OpenCV Mat to an IntPtr.

This method transfers the data stored in an OpenCV Mat object into an IntPtr. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size of the memory region pointed to by the IntPtr is smaller than the data size managed by the Mat, data will be copied up to the size of the IntPtr. If the data size of the memory region pointed to by the IntPtr is larger, data will be copied up to the size managed by the Mat.

Note: This method operates on unmanaged memory, and incorrect usage can lead to unpredictable behavior, crashes, or data corruption. Ensure that the source pointer points to a valid memory region and that the specified length does not exceed the allocated memory size for that region. Additionally, as this method involves unmanaged memory, be cautious about potential security risks. Accessing memory beyond the intended range can lead to buffer overflows and may compromise application stability and security. It is highly recommended to perform validation checks on the pointer and the specified length.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements represented by the IntPtr. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
intPtrThe destination IntPtr where the data will be copied. This pointer must reference a valid memory region.
lengthThe number of elements of type T to copy from the source Mat to the destination IntPtr.
Returns
The number of bytes copied from the Mat to the IntPtr.
Exceptions
ArgumentNullExceptionThrown when mat is null.
ArgumentExceptionThrown when intPtr == IntPtr.Zero. Thrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyFromMat< T >() [2/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat< T > ( Mat mat,
NativeArray< T > array )
static

Copies the data from an OpenCV Mat to a NativeArray.

This method transfers the data stored in an OpenCV Mat object into a NativeArray. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the array is smaller than the data size managed by the Mat, data will be copied up to the size of the array. If the data size managed by the array is larger, data will be copied up to the size managed by the Mat.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the NativeArray. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
arrayThe destination NativeArray where the data will be copied.
Returns
The number of bytes copied from the Mat to the NativeArray.
Exceptions
ArgumentNullExceptionThrown when mat or array is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyFromMat< T >() [3/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat< T > ( Mat mat,
NativeArray< T > array,
int length )
static

Copies the data from an OpenCV Mat to a NativeArray.

This method transfers the data stored in an OpenCV Mat object into a NativeArray. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the array is smaller than the data size managed by the Mat, data will be copied up to the size of the array. If the data size managed by the array is larger, data will be copied up to the size managed by the Mat.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the NativeArray. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
arrayThe destination NativeArray where the data will be copied.
lengthThe number of elements of type T to copy from the source Mat to the destination managed array.
Returns
The number of bytes copied from the Mat to the NativeArray.
Exceptions
ArgumentNullExceptionThrown when mat or array is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyFromMat< T >() [4/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat< T > ( Mat mat,
Span< T > array )
static

Copies the data from an OpenCV Mat to a Span.

This method transfers the data stored in an OpenCV Mat object into a Span. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the array is smaller than the data size managed by the Mat, data will be copied up to the size of the array. If the data size managed by the array is larger, data will be copied up to the size managed by the Mat.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the Span. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
arrayThe destination Span where the data will be copied.
Returns
The number of bytes copied from the Mat to the Span.
Exceptions
ArgumentNullExceptionThrown when mat or array is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyFromMat< T >() [5/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat< T > ( Mat mat,
Span< T > array,
int length )
static

Copies the data from an OpenCV Mat to a Span.

This method transfers the data stored in an OpenCV Mat object into a Span. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the array is smaller than the data size managed by the Mat, data will be copied up to the size of the array. If the data size managed by the array is larger, data will be copied up to the size managed by the Mat.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the Span. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
arrayThe destination Span where the data will be copied.
lengthThe number of elements of type T to copy from the source Mat to the destination Span.
Returns
The number of bytes copied from the Mat to the Span.
Exceptions
ArgumentNullExceptionThrown when mat or array is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyFromMat< T >() [6/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat< T > ( Mat mat,
T[] array )
static

Copies the data from an OpenCV Mat to a managed array.

This method transfers the data stored in an OpenCV Mat object into a managed array. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the array is smaller than the data size managed by the Mat, data will be copied up to the size of the array. If the data size managed by the array is larger, data will be copied up to the size managed by the Mat.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the managed array. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
arrayThe destination managed array where the data will be copied.
Returns
The number of bytes copied from the Mat to the managed array.
Exceptions
ArgumentNullExceptionThrown when mat or array is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyFromMat< T >() [7/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyFromMat< T > ( Mat mat,
T[] array,
int length )
static

Copies the data from an OpenCV Mat to a managed array.

This method transfers the data stored in an OpenCV Mat object into a managed array. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the array is smaller than the data size managed by the Mat, data will be copied up to the size of the array. If the data size managed by the array is larger, data will be copied up to the size managed by the Mat.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the Mat and the array. Although its functionality is more limited compared to the Mat.get method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the managed array. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
matThe source Mat from which data will be copied. The Mat must not be disposed of or null.
arrayThe destination managed array where the data will be copied.
lengthThe number of elements of type T to copy from the source Mat to the destination managed array.
Returns
The number of bytes copied from the Mat to the managed array.
Exceptions
ArgumentNullExceptionThrown when mat or array is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyToMat() [1/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat ( IntPtr intPtr,
Mat mat )
static

Copies the data from an IntPtr to an OpenCV Mat.

This method transfers the data stored in an IntPtr to an OpenCV Mat. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

Data of the same size in bytes as the data managed by the Mat is copied from the IntPtr to the Mat. If you want to specify the exact range of data to copy, please use the method that accepts a length argument.

Note: This method operates on unmanaged memory, and incorrect usage can lead to unpredictable behavior, crashes, or data corruption. Ensure that the source pointer points to a valid memory region and that the specified length does not exceed the allocated memory size for that region. Additionally, as this method involves unmanaged memory, be cautious about potential security risks. Accessing memory beyond the intended range can lead to buffer overflows and may compromise application stability and security. It is highly recommended to perform validation checks on the pointer and the specified length.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Parameters
intPtrThe source IntPtr from which data will be copied. This pointer must reference a valid memory region.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
Returns
The number of bytes copied from the IntPtr to the Mat.
Exceptions
ArgumentNullExceptionThrown when mat is null.
ArgumentExceptionThrown when intPtr == IntPtr.Zero. Thrown when !mat.isContinuous() && mat.dims() > 2.

◆ CopyToMat() [2/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat ( IntPtr intPtr,
Mat mat,
int length )
static

Copies the data from an IntPtr to an OpenCV Mat.

This method transfers the data stored in an IntPtr to an OpenCV Mat. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size of the memory region pointed to by the IntPtr, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size of the memory region pointed to by the IntPtr.

Note: This method operates on unmanaged memory, and incorrect usage can lead to unpredictable behavior, crashes, or data corruption. Ensure that the source pointer points to a valid memory region and that the specified length does not exceed the allocated memory size for that region. Additionally, as this method involves unmanaged memory, be cautious about potential security risks. Accessing memory beyond the intended range can lead to buffer overflows and may compromise application stability and security. It is highly recommended to perform validation checks on the pointer and the specified length.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Parameters
intPtrThe source IntPtr from which data will be copied. This pointer must reference a valid memory region.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
lengthThe number of elements, in terms of byte size, to copy from the source IntPtr to the destination Mat. The length represents the size in bytes, regardless of the actual type of data in the IntPtr or the destination.
Returns
The number of bytes copied from the IntPtr to the Mat.
Exceptions
ArgumentNullExceptionThrown when mat is null.
ArgumentExceptionThrown when intPtr == IntPtr.Zero. Thrown when !mat.isContinuous() && mat.dims() > 2.

◆ CopyToMat< T >() [1/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat< T > ( IntPtr intPtr,
Mat mat,
int length )
static

Copies the data from an IntPtr to an OpenCV Mat.

This method transfers the data stored in an IntPtr to an OpenCV Mat. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data from Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size of the memory region pointed to by the IntPtr, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size of the memory region pointed to by the IntPtr.

Note: This method operates on unmanaged memory, and incorrect usage can lead to unpredictable behavior, crashes, or data corruption. Ensure that the source pointer points to a valid memory region and that the specified length does not exceed the allocated memory size for that region. Additionally, as this method involves unmanaged memory, be cautious about potential security risks. Accessing memory beyond the intended range can lead to buffer overflows and may compromise application stability and security. It is highly recommended to perform validation checks on the pointer and the specified length.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements represented by the IntPtr. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
intPtrThe source IntPtr from which data will be copied. This pointer must reference a valid memory region.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
lengthThe number of elements of type T to copy from the source IntPtr to the destination Mat.
Returns
The number of bytes copied from the IntPtr to the Mat.
Exceptions
ArgumentNullExceptionThrown when mat is null.
ArgumentExceptionThrown when intPtr == IntPtr.Zero. Thrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyToMat< T >() [2/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat< T > ( NativeArray< T > array,
Mat mat )
static

Copies the data from a NativeArray to an OpenCV Mat.

This method transfers the data stored in a NativeArray into an OpenCV Mat object. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data where Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size managed by the array, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size managed by the array.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the NativeArray. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
arrayThe source NativeArray from which data will be copied.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
Returns
The number of bytes copied from the NativeArray to the Mat.
Exceptions
ArgumentNullExceptionThrown when array or mat is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyToMat< T >() [3/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat< T > ( NativeArray< T > array,
Mat mat,
int length )
static

Copies the data from a NativeArray to an OpenCV Mat.

This method transfers the data stored in a NativeArray into an OpenCV Mat object. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data where Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size managed by the array, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size managed by the array.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the NativeArray. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
arrayThe source NativeArray from which data will be copied.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
lengthThe number of elements of type T to copy from the source NativeArray to the destination Mat.
Returns
The number of bytes copied from the NativeArray to the Mat.
Exceptions
ArgumentNullExceptionThrown when array or mat is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyToMat< T >() [4/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat< T > ( ReadOnlySpan< T > array,
Mat mat )
static

Copies the data from a Span to an OpenCV Mat.

This method transfers the data stored in a Span into an OpenCV Mat object. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data where Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size managed by the array, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size managed by the array.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the Span. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
arrayThe source Span from which data will be copied.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
Returns
The number of bytes copied from the Span to the Mat.
Exceptions
ArgumentNullExceptionThrown when array or mat is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyToMat< T >() [5/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat< T > ( ReadOnlySpan< T > array,
Mat mat,
int length )
static

Copies the data from a Span to an OpenCV Mat.

This method transfers the data stored in a Span into an OpenCV Mat object. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data where Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size managed by the array, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size managed by the array.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the Span. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
arrayThe source Span from which data will be copied.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
lengthThe number of elements of type T to copy from the source Span to the destination Mat.
Returns
The number of bytes copied from the Span to the Mat.
Exceptions
ArgumentNullExceptionThrown when array or mat is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyToMat< T >() [6/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat< T > ( T[] array,
Mat mat )
static

Copies the data from a managed array to an OpenCV Mat.

This method transfers the data stored in a managed array into an OpenCV Mat object. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data where Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size managed by the array, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size managed by the array.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the managed array. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
arrayThe source managed array from which data will be copied.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
Returns
The number of bytes copied from the managed array to the Mat.
Exceptions
ArgumentNullExceptionThrown when array or mat is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CopyToMat< T >() [7/7]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CopyToMat< T > ( T[] array,
Mat mat,
int length )
static

Copies the data from a managed array to an OpenCV Mat.

This method transfers the data stored in a managed array into an OpenCV Mat object. It supports both continuous and non-continuous Mat objects:

  • If mat.isContinuous() is false and mat.dims() exceeds 2, the method will throw an exception, as non-continuous Mats with more than 2 dimensions are unsupported.
  • If mat.isContinuous() is true, the method can copy data where Mats with any number of dimensions.

If the data size managed by the Mat is smaller than the data size managed by the array, data will be copied up to the size of the mat. If the data size managed by the Mat is larger, data will be copied up to the size managed by the array.

Note: This method is specialized for copying a continuous range of data regardless of differences in types between the array and the Mat. Although its functionality is more limited compared to the Mat.put method, it offers improved performance for such operations.

Template Parameters
TThe type of the elements in the managed array. This must be an unmanaged type (i.e., a type without references to managed objects) to allow for direct memory access.
Parameters
arrayThe source managed array from which data will be copied.
matThe destination Mat where the data will be copied. The Mat must not be disposed of or null.
lengthThe number of elements of type T to copy from the source managed array to the destination Mat.
Returns
The number of bytes copied from the managed array to the Mat.
Exceptions
ArgumentNullExceptionThrown when array or mat is null.
ArgumentExceptionThrown when !mat.isContinuous() && mat.dims() > 2.
Type Constraints
T :unmanaged 

◆ CreateMatToRenderTextureComputeShader()

static ComputeShader OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CreateMatToRenderTextureComputeShader ( GraphicsBuffer graphicsBuffer,
RenderTexture renderTexture,
bool flip = true,
int flipCode = 0 )
static

Creates a ComputeShader for converting from Mat to RenderTexture based on the current platform and graphics API. This ComputeShader is intended to be used with the kernel index obtained from the GetKernelIndexForMatToRenderTextureComputeShader method when dispatching.

Parameters
graphicsBufferThe source GraphicsBuffer.
renderTextureThe destination RenderTexture.
flipWhether to flip the image vertically.
flipCodeThe flipCode for the Core.flip() method.
Returns
The created ComputeShader.

◆ CreateRenderTextureToMatComputeShader()

static ComputeShader OpenCVForUnity.UnityIntegration.OpenCVMatUtils.CreateRenderTextureToMatComputeShader ( RenderTexture renderTexture,
GraphicsBuffer graphicsBuffer,
bool flip = true,
int flipCode = 0 )
static

Creates a ComputeShader for converting from RenderTexture to Mat based on the current platform and graphics API. This ComputeShader is intended to be used with the kernel index obtained from the GetKernelIndexForRenderTextureToMatComputeShader method when dispatching.

Parameters
renderTextureThe source RenderTexture.
graphicsBufferThe destination GraphicsBuffer.
flipWhether to flip the image vertically.
flipCodeThe flipCode for the Core.flip() method.
Returns
The created ComputeShader.

◆ GetKernelIndexForMatToRenderTextureComputeShader()

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.GetKernelIndexForMatToRenderTextureComputeShader ( GraphicsBuffer graphicsBuffer,
RenderTexture renderTexture )
static

Gets the kernel index to use for converting from Mat to RenderTexture based on the current platform and graphics API. This kernel index is intended to be used when dispatching the ComputeShader obtained from the CreateMatToRenderTextureComputeShader method.

Parameters
graphicsBufferThe source GraphicsBuffer.
renderTextureThe destination RenderTexture.
Returns
The kernel index to use for the conversion.

◆ GetKernelIndexForRenderTextureToMatComputeShader()

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.GetKernelIndexForRenderTextureToMatComputeShader ( RenderTexture renderTexture,
GraphicsBuffer graphicsBuffer )
static

Gets the kernel index to use for converting from RenderTexture to Mat based on the current platform and graphics API. This kernel index is intended to be used when dispatching the ComputeShader obtained from the CreateRenderTextureToMatComputeShader method.

Parameters
renderTextureThe source RenderTexture.
graphicsBufferThe destination GraphicsBuffer.
Returns
The kernel index to use for the conversion.

◆ GraphicsBufferToMat()

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.GraphicsBufferToMat ( GraphicsBuffer graphicsBuffer,
Mat mat,
CopyMode copyMode )
static

Copies data from a Unity GraphicsBuffer to a OpenCV Mat.

Parameters
graphicsBufferThe source Unity GraphicsBuffer.
matThe destination OpenCV Mat.
copyModeThe CopyMode enumeration specifying the method of copying data.
Exceptions
ArgumentNullExceptionThrown if mat or graphicsBuffer is null.
ArgumentExceptionThrown if data size or stride does not match.

◆ GraphicsBufferToMatAsync()

static async Awaitable OpenCVForUnity.UnityIntegration.OpenCVMatUtils.GraphicsBufferToMatAsync ( GraphicsBuffer graphicsBuffer,
Mat mat,
CopyMode copyMode,
CancellationToken cancellationToken = default )
static

Copies data from a Unity GraphicsBuffer to an OpenCV Mat asynchronously.

Parameters
graphicsBufferThe source Unity GraphicsBuffer.
matThe destination OpenCV Mat.
copyModeThe CopyMode enumeration specifying the method of copying data.
cancellationTokenA token to monitor for cancellation requests.
Exceptions
ArgumentNullExceptionThrown if mat or graphicsBuffer is null.
ArgumentExceptionThrown if data size or stride does not match.
NotSupportedExceptionThrown if compute shaders are not supported on the current platform.
OperationCanceledExceptionThrown if the operation is canceled via cancellationToken .

◆ MatToGraphicsBuffer()

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToGraphicsBuffer ( Mat mat,
GraphicsBuffer graphicsBuffer,
CopyMode copyMode )
static

Copies data from a OpenCv Mat to a Unity GraphicsBuffer.

Parameters
matThe source OpenCV Mat.
graphicsBufferThe destination Unity GraphicsBuffer.
copyModeThe CopyMode enumeration specifying the method of copying data.
Exceptions
ArgumentNullExceptionThrown if mat or graphicsBuffer is null.
ArgumentExceptionThrown if data size or stride does not match.

◆ MatToGraphicsBufferAsync()

static async Awaitable< AsyncGPUReadbackRequest > OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToGraphicsBufferAsync ( Mat mat,
GraphicsBuffer graphicsBuffer,
CopyMode copyMode,
CancellationToken cancellationToken = default )
static

Copies data from a OpenCv Mat to a Unity GraphicsBuffer asynchronously.

Parameters
matThe source OpenCV Mat.
graphicsBufferThe destination Unity GraphicsBuffer.
copyModeThe CopyMode enumeration specifying the method of copying data.
cancellationTokenCancellation token to cancel the asynchronous operation.
Exceptions
ArgumentNullExceptionThrown if mat or graphicsBuffer is null.
ArgumentExceptionThrown if data size or stride does not match.
OperationCanceledExceptionThrown when the operation is canceled via the cancellationToken .

◆ MatToRenderTexture()

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToRenderTexture ( Mat mat,
RenderTexture renderTexture,
GraphicsBuffer graphicsBuffer,
bool flip = true,
int flipCode = 0 )
static

Converts an OpenCV Mat to a Unity RenderTexture.

Parameters
matThe source OpenCV Mat. The depth must be 'CV_8U'. The Mat object must not have more than 4 channels.
renderTextureThe destination Unity RenderTexture. Must have enableRandomWrite set to true.
graphicsBufferA temporary GraphicsBuffer. If null, a new one will be created. The stride of the graphics buffer must be exactly 4 bytes.
flipWhether to flip the image vertically.
flipCodeThe flipCode for the Core.flip() method.
Exceptions
ArgumentNullExceptionThrown if renderTexture or mat is null.
ArgumentExceptionThrown if renderTexture has enableRandomWrite set to false, or if the size or format of the RenderTexture is invalid.

◆ MatToRenderTextureAsync()

static async Awaitable< AsyncGPUReadbackRequest > OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToRenderTextureAsync ( Mat mat,
RenderTexture renderTexture,
GraphicsBuffer graphicsBuffer,
bool flip = true,
int flipCode = 0,
CancellationToken cancellationToken = default )
static

Converts an OpenCV Mat to a Unity RenderTexture asynchronously.

Parameters
matThe source OpenCV Mat. The depth must be 'CV_8U'. The Mat object must not have more than 4 channels.
renderTextureThe destination Unity RenderTexture. Must have enableRandomWrite set to true.
graphicsBufferA temporary GraphicsBuffer. If null, a new one will be created. The stride of the graphics buffer must be exactly 4 bytes.
flipWhether to flip the image vertically.
flipCodeThe flipCode for the Core.flip() method.
cancellationTokenA token to monitor for cancellation requests.
Exceptions
ArgumentNullExceptionThrown if renderTexture or mat is null.
ArgumentExceptionThrown if renderTexture has enableRandomWrite set to false, or if the size or format of the RenderTexture is invalid.
OperationCanceledExceptionThrown if the operation is canceled via cancellationToken .

◆ MatToTexture2D() [1/4]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToTexture2D ( Mat mat,
Texture2D texture2D,
bool flip = true,
int flipCode = 0,
bool updateMipmaps = false,
bool makeNoLongerReadable = false )
static

Converts an OpenCV Mat to a Unity Texture2D.

This method converts an OpenCV Mat to a Unity Texture2D. Conversion is possible even when the number of bytes per pixel differs, such as from Mat(8UC1) to Texture2D(RGBA32). In the case of multi-channel color to 1-channel, it is converted to grayscale. Performance is optimal when the per-pixel data size and color order match, such as with Mat(8UC4) and Texture2D(RGBA32). If the texture format is not RGBA32, BGRA32, RGB24, Alpha8, or R8, the Texture2D.SetPixels32 and Texture2D.GetPixels32 methods are used. In such cases, it is recommended to use the pixels32Buffer argument to avoid repeated memory allocations.

Parameters
matThe source Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE). For other CvTypes or color orders, use MatToTexture2DRaw.
texture2DThe destination Texture2D must have the same size as the source Mat. The destination Texture2D supports the following formats. (Texture2D.SetPixels32)
flipIf true, the Mat is flipped before conversion. The default is true, as the Mat must be flipped to align with the coordinate system of the destination Texture2D image.
flipCodeSpecifies how to flip the Mat: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
updateMipmapsIf true, mipmaps are recalculated after conversion. The default is false.
makeNoLongerReadableIf true, system memory copy of a texture is released. The default is false.
Exceptions
ArgumentNullExceptionThrown when mat or texture2D is null.
ArgumentException

◆ MatToTexture2D() [2/4]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToTexture2D ( Mat mat,
Texture2D texture2D,
Color32[] pixels32Buffer,
byte[] rawTextureDataBuffer = null,
bool flip = true,
int flipCode = 0,
bool updateMipmaps = false,
bool makeNoLongerReadable = false )
static

Converts an OpenCV Mat to a Unity Texture2D.

This method converts an OpenCV Mat to a Unity Texture2D. Conversion is possible even when the number of bytes per pixel differs, such as from Mat(8UC1) to Texture2D(RGBA32). In the case of multi-channel color to 1-channel, it is converted to grayscale. Performance is optimal when the per-pixel data size and color order match, such as with Mat(8UC4) and Texture2D(RGBA32). If the texture format is not RGBA32, BGRA32, RGB24, Alpha8, or R8, the Texture2D.SetPixels32 and Texture2D.GetPixels32 methods are used. In such cases, it is recommended to use the pixels32Buffer argument to avoid repeated memory allocations.

Parameters
matThe source Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE). For other CvTypes or color orders, use MatToTexture2DRaw.
texture2DThe destination Texture2D must have the same size as the source Mat. The destination Texture2D supports the following formats. (Texture2D.SetPixels32)
pixels32BufferAn optional array for receiving pixel data as Color32. Using this array helps avoid memory allocation each frame. Ensure the array is initialized to a length matching the texture’s width * height. (Texture2D.GetPixels32)
rawTextureDataBufferAn optional array for receiving raw texture data. This only works when the "OPENCV_DONT_USE_UNSAFE_CODE" symbol is defined and texture2D has no mipmaps (mipmapCount == 1). Passing a byte array can help avoid memory allocation each frame. Ensure the array length matches the texture's raw data size. (Texture2D.GetRawTextureData) The default value is null.
flipIf true, the Mat is flipped before conversion. The default is true, as the Mat must be flipped to align with the coordinate system of the destination Texture2D image.
flipCodeSpecifies how to flip the Mat: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
updateMipmapsIf true, mipmaps are recalculated after conversion. The default is false.
makeNoLongerReadableIf true, system memory copy of a texture is released. The default is false.
Exceptions
ArgumentNullExceptionThrown when mat or texture2D is null.
ArgumentException

◆ MatToTexture2D() [3/4]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToTexture2D ( Mat mat,
Texture2D texture2D,
int mipLevel,
bool flip = true,
int flipCode = 0,
bool updateMipmaps = false,
bool makeNoLongerReadable = false )
static

Converts an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level.

This method converts an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level. Conversion is possible even when the number of bytes per pixel differs, such as from Mat(8UC1) to Texture2D(RGBA32). In the case of multi-channel color to 1-channel, it is converted to grayscale. It is recommended to use the pixels32Buffer argument to avoid repeated memory allocations.

Parameters
matThe source Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE). For other CvTypes or color orders, use MatToTexture2DRaw. The source Mat must be the same size as the mipmap of the destination Texture2D.
texture2DThe destination Texture2D most be supports mipmaps, as the conversion will target themipLevel . The destination Texture2D supports the following formats. (Texture2D.SetPixels32)
mipLevelThe mipmap level to which the Mat will be converted. The level must be within the range supported by the destination Texture2D.
flipIf true, the Mat is flipped before conversion. The default is true, as the Mat must be flipped to align with the coordinate system of the destination Texture2D image.
flipCodeSpecifies how to flip the Mat: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
updateMipmapsIf true, mipmaps are recalculated after conversion. The default is false.
makeNoLongerReadableIf true, system memory copy of a texture is released. The default is false.
Exceptions
ArgumentNullExceptionThrown when mat or texture2D is null.
ArgumentException

◆ MatToTexture2D() [4/4]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToTexture2D ( Mat mat,
Texture2D texture2D,
int mipLevel,
Color32[] pixels32Buffer,
bool flip = true,
int flipCode = 0,
bool updateMipmaps = false,
bool makeNoLongerReadable = false )
static

Converts an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level.

This method converts an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level. Conversion is possible even when the number of bytes per pixel differs, such as from Mat(8UC1) to Texture2D(RGBA32). In the case of multi-channel color to 1-channel, it is converted to grayscale. It is recommended to use the pixels32Buffer argument to avoid repeated memory allocations.

Parameters
matThe source Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE). For other CvTypes or color orders, use MatToTexture2DRaw. The source Mat must be the same size as the mipmap of the destination Texture2D.
texture2DThe destination Texture2D most be supports mipmaps, as the conversion will target themipLevel . The destination Texture2D supports the following formats. (Texture2D.SetPixels32)
mipLevelThe mipmap level to which the Mat will be converted. The level must be within the range supported by the destination Texture2D.
pixels32BufferAn optional array for receiving pixel data as Color32. Using this array helps avoid memory allocation each frame. Ensure the array is initialized to a length matching the texture’s mipmap width * height. (Texture2D.GetPixels32)
flipIf true, the Mat is flipped before conversion. The default is true, as the Mat must be flipped to align with the coordinate system of the destination Texture2D image.
flipCodeSpecifies how to flip the Mat: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
updateMipmapsIf true, mipmaps are recalculated after conversion. The default is false.
makeNoLongerReadableIf true, system memory copy of a texture is released. The default is false.
Exceptions
ArgumentNullExceptionThrown when mat or texture2D is null.
ArgumentException

◆ MatToTexture2DRaw() [1/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToTexture2DRaw ( Mat mat,
Texture2D texture2D,
bool flip = true,
int flipCode = 0,
bool updateMipmaps = false,
bool makeNoLongerReadable = false )
static

Copies raw data from an OpenCV Mat to a Unity Texture2D.

This method copies raw data from an OpenCV Mat to a Unity Texture2D. There are no specific requirements for the size or type of the Mat and Texture2D; data is copied up to the maximum size that fits within the data size of the destination Texture2D, including mipmaps. A common use case for this method is writing a Mat with BGRA color order to a Texture2D in the BGRA32 format. If the "OPENCV_DONT_USE_UNSAFE_CODE" symbol is defined, the following operating conditions are added: mat.isContinuous() == true, and the data size of Texture2D is the same as or larger than the data size of Mat.

Parameters
matThe source Mat must be 2-dimensional.
texture2DThe destination Texture2D.
flipIf true, the Mat is flipped before copies. The default is true, as the Mat must be flipped to align with the coordinate system of the destination Texture2D image.
flipCodeSpecifies how to flip the Mat: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
updateMipmapsIf true, mipmaps are recalculated after copies. The default is false.
makeNoLongerReadableIf true, system memory copy of a texture is released. The default is false.
Returns
Returns the number of bytes actually written to the destination Texture2D.
Exceptions
ArgumentNullExceptionThrown when mat or texture2D is null.
ArgumentException

◆ MatToTexture2DRaw() [2/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.MatToTexture2DRaw ( Mat mat,
Texture2D texture2D,
int mipLevel,
bool flip = true,
int flipCode = 0,
bool updateMipmaps = false,
bool makeNoLongerReadable = false )
static

Copies raw data from an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level.

This method copies raw data from an OpenCV Mat to a Unity Texture2D, targeting the specified mipmap level. There are no specific requirements for the size or type of the Mat and Texture2D; data is copied up to the maximum size that fits within the data size of the mipmap of the destination Texture2D.

Parameters
matThe source Mat must be 2-dimensional.
texture2DThe destination Texture2D.
mipLevelThe mipmap level to which the Mat will be converted. The level must be within the range supported by the destination Texture2D.
flipIf true, the Mat is flipped before copies. The default is true, as the Mat must be flipped to align with the coordinate system of the destination Texture2D image.
flipCodeSpecifies how to flip the Mat: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
updateMipmapsIf true, mipmaps are recalculated after copies. The default is false.
makeNoLongerReadableIf true, system memory copy of a texture is released. The default is false.
Returns
Returns the number of bytes actually written to the destination Texture2D.
Exceptions
ArgumentNullExceptionThrown when mat or texture2D is null.
ArgumentException

◆ RenderTextureToMat()

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.RenderTextureToMat ( RenderTexture renderTexture,
Mat mat,
GraphicsBuffer graphicsBuffer,
bool flip = true,
int flipCode = 0 )
static

Converts a Unity RenderTexture to an OpenCV Mat.

Parameters
renderTextureThe source Unity RenderTexture. Must have enableRandomWrite set to true.
matThe destination OpenCV Mat. The depth must be 'CV_8U'. The Mat object must not have more than 4 channels.
graphicsBufferA temporary GraphicsBuffer. If null, a new one will be created. The stride of the graphics buffer must be exactly 4 bytes.
flipWhether to flip the image vertically.
flipCodeThe flipCode for the Core.flip() method.
Exceptions
ArgumentNullExceptionThrown if renderTexture or mat is null.
ArgumentExceptionThrown if renderTexture has enableRandomWrite set to false, or if the size or format of the RenderTexture is invalid.

◆ RenderTextureToMatAsync()

static async Awaitable OpenCVForUnity.UnityIntegration.OpenCVMatUtils.RenderTextureToMatAsync ( RenderTexture renderTexture,
Mat mat,
GraphicsBuffer graphicsBuffer,
bool flip = true,
int flipCode = 0,
CancellationToken cancellationToken = default )
static

Converts a Unity RenderTexture to an OpenCV Mat asynchronously.

Parameters
renderTextureThe source Unity RenderTexture. Must have enableRandomWrite set to true.
matThe destination OpenCV Mat. The depth must be 'CV_8U'. The Mat object must not have more than 4 channels.
graphicsBufferA temporary GraphicsBuffer. If null, a new one will be created. The stride of the graphics buffer must be exactly 4 bytes.
flipWhether to flip the image vertically.
flipCodeThe flipCode for the Core.flip() method.
cancellationTokenA token to monitor for cancellation requests.
Exceptions
ArgumentNullExceptionThrown if renderTexture or mat is null.
ArgumentExceptionThrown if renderTexture has enableRandomWrite set to false, or if the size or format of the RenderTexture is invalid.
OperationCanceledExceptionThrown if the operation is canceled via cancellationToken .

◆ Texture2DToMat() [1/2]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.Texture2DToMat ( Texture2D texture2D,
Mat mat,
bool flip = true,
int flipCode = 0 )
static

Converts a Unity Texture2D to an OpenCV Mat.

This method converts a Unity Texture2D to an OpenCV Mat. Conversion is possible even when the number of bytes per pixel differs, such as from Texture2D(RGBA32) to Mat(8UC1). In the case of multi-channel color to 1-channel, it is converted to grayscale. Performance is optimal when the per-pixel data size and color order match, such as with Texture2D(RGBA32) and Mat(8UC4). If the texture format is not RGBA32, BGRA32, RGB24, Alpha8, or R8, the Texture2D.GetPixels32 methods are used.

Parameters
texture2DThe source Texture2D must have the same size as the destination Mat. The source Texture2D supports the following formats. (Texture2D.GetPixels32)
matThe destination Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE). For other CvTypes or color orders, use Texture2DToMatRaw.
flipIf true, the pixel data retrieved from the Texture2D is flipped before conversion. The default is true, as the pixel data must be flipped to align with the coordinate system of the destination Mat.
flipCodeSpecifies how to flip the Textrue2D image: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
Exceptions
ArgumentNullExceptionThrown when texture2D or mat is null.
ArgumentException

◆ Texture2DToMat() [2/2]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.Texture2DToMat ( Texture2D texture2D,
Mat mat,
int mipLevel,
bool flip = true,
int flipCode = 0 )
static

Converts a Unity Texture2D to an OpenCV Mat, targeting the specified mipmap level.

This method converts a Unity Texture2D to an OpenCV Mat, targeting the specified mipmap level. Conversion is possible even when the number of bytes per pixel differs, such as from Texture2D(RGBA32) to Mat(8UC1). In the case of multi-channel color to 1-channel, it is converted to grayscale.

Parameters
texture2DThe source Texture2D supports the following formats. (Texture2D.GetPixels32) The source Texture2D most be supports mipmaps, as the conversion will target themipLevel .
matThe destination Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE). For other CvTypes or color orders, use Texture2DToMatRaw. The destination Mat must be the same size as the mipmap of the destination Texture2D.
mipLevelThe mipmap level to which the Mat will be converted. The level must be within the range supported by the source Texture2D.
flipIf true, the pixel data retrieved from the Texture2D is flipped before conversion. The default is true, as the pixel data must be flipped to align with the coordinate system of the destination Mat.
flipCodeSpecifies how to flip the Textrue2D image: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
Exceptions
ArgumentNullExceptionThrown when texture2D or mat is null.
ArgumentException

◆ Texture2DToMatRaw() [1/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.Texture2DToMatRaw ( Texture2D texture2D,
Mat mat,
bool flip = true,
int flipCode = 0 )
static

Copies raw data from a Unity Texture2D to an OpenCV Mat.

This method copies raw data from a Unity Texture2D to an OpenCV Mat. There are no specific requirements for the size or type of the Texture2D and Mat; data is copied up to the maximum size that fits within the data size of the destination Mat. A common use case for this method is writing a Texture2D in the BGRA32 format to a Mat with BGRA color order.

Parameters
texture2DThe source Texture2D.
matThe destination Mat must be 2-dimensional.
flipIf true, the pixel data retrieved from the Texture2D is flipped before copies. The default is true, as the pixel data must be flipped to align with the coordinate system of the destination Mat.
flipCodeSpecifies how to flip the Textrue2D image: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
Returns
Returns the number of bytes actually written to the destination Mat.
Exceptions
ArgumentNullExceptionThrown when texture2D or mat is null.
ArgumentException

◆ Texture2DToMatRaw() [2/2]

static int OpenCVForUnity.UnityIntegration.OpenCVMatUtils.Texture2DToMatRaw ( Texture2D texture2D,
Mat mat,
int mipLevel,
bool flip = true,
int flipCode = 0 )
static

Copies raw data from a Unity Texture2D to an OpenCV Mat, targeting the specified mipmap level.

This method copies raw data from a Unity Texture2D to an OpenCV Mat, targeting the specified mipmap level. There are no specific requirements for the size or type of the Texture2D and Mat; data is copied up to the maximum size that fits within the data size of the mipmap of the destination Mat.

Parameters
texture2DThe source Texture2D.
matThe destination Mat must be 2-dimensional.
mipLevelThe mipmap level to which the Mat will be converted. The level must be within the range supported by the source Texture2D.
flipIf true, the pixel data retrieved from the Texture2D is flipped before copies. The default is true, as the pixel data must be flipped to align with the coordinate system of the destination Mat.
flipCodeSpecifies how to flip the Textrue2D image: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
Returns
Returns the number of bytes actually written to the destination Mat.
Exceptions
ArgumentNullExceptionThrown when texture2D or mat is null.
ArgumentException

◆ TextureToMatAsync() [1/2]

static async Awaitable OpenCVForUnity.UnityIntegration.OpenCVMatUtils.TextureToMatAsync ( Texture texture,
Mat mat,
bool flip = true,
int flipCode = 0,
int mipIndex = 0,
int x = 0,
int width = -1,
int y = 0,
int height = -1,
int z = 0,
int depth = 1,
GraphicsFormat? dstFormat = null,
CancellationToken cancellationToken = default )
static

Converts a Texture to an OpenCV Mat asynchronously.

Parameters
textureThe source Texture to be converted.
matThe destination Mat that will store the converted texture data.
flipIf true, the pixel data retrieved from the Texture2D is flipped before copies. The default is true, as the pixel data must be flipped to align with the coordinate system of the destination Mat.
flipCodeSpecifies how to flip the Textrue2D image: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
mipIndexIndex of the mipmap to be fetched.
xStarting X coordinate in pixels of the Texture data to be fetched.
widthWidth in pixels of the Texture data to be fetched.
yStarting Y coordinate in pixels of the Texture data to be fetched.
heightHeight in pixels of the Texture data to be fetched.
zDepth in pixels for Texture3D being fetched. Number of layers for TextureCube, TextureArray and TextureCubeArray.
depthDepth in pixels for Texture3D being fetched. Number of layers for TextureCube, TextureArray and TextureCubeArray.
dstFormatTarget GraphicsFormat of the data. If the target format is different from the format stored on the GPU, the conversion is automatic.
cancellationTokenCancellation token to cancel the asynchronous operation.
Exceptions
ArgumentNullExceptionThrown if texture or mat is null.
NotSupportedExceptionThrown when the graphics format of the texture is not supported for reading using AsyncGPUReadback.
OperationCanceledExceptionThrown when the operation is canceled via the cancellationToken .
InvalidOperationExceptionThrown when AsyncGPUReadback completes with an error.

◆ TextureToMatAsync() [2/2]

static async Awaitable OpenCVForUnity.UnityIntegration.OpenCVMatUtils.TextureToMatAsync ( Texture texture,
Mat mat,
CancellationToken cancellationToken = default )
static

Converts a Texture to an OpenCV Mat asynchronously.

Parameters
textureThe source Texture to be converted.
matThe destination Mat that will store the converted texture data.
cancellationTokenCancellation token to cancel the asynchronous operation.
Exceptions
ArgumentNullExceptionThrown if texture or mat is null.
NotSupportedExceptionThrown when the graphics format of the texture is not supported for reading using AsyncGPUReadback.
OperationCanceledExceptionThrown when the operation is canceled via the cancellationToken .
InvalidOperationExceptionThrown when AsyncGPUReadback completes with an error.

◆ TextureToTexture2D()

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.TextureToTexture2D ( Texture texture,
Texture2D texture2D )
static

OPENCV_DONT_USE_WEBCAMTEXTURE_API.

Converts a Texture to a Texture2D.

This method converts a Texture to a Texture2D. The Texture and the Texture2D must be the same size.

Parameters
textureThe source Texture.
texture2DThe destination Texture2D. It must have a TextureFormat of RGBA32, ARGB32, RGB24, RGBAFloat, or RGBAHalf.
Exceptions
ArgumentNullExceptionThrown when texture or texture2D is null.
ArgumentException

◆ WebCamTextureToMat() [1/2]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.WebCamTextureToMat ( WebCamTexture webCamTexture,
Mat mat,
bool flip = true,
int flipCode = 0 )
static

Converts a Unity WebCamTexture to an OpenCV Mat.

This method converts a Unity WebCamTexture image to an OpenCV Mat. Conversion is possible even when the number of bytes per pixel differs, such as from WebCamTexture(RGBA32) to Mat(8UC1). In the case of multi-channel color to 1-channel, it is converted to grayscale. Performance is optimal when the per-pixel data size and color order match, such as with Texture2D(RGBA32) and Mat(8UC4). It is recommended to use the pixels32Buffer argument to avoid repeated memory allocations.

Parameters
webCamTextureThe source WebCamTexture must have the same size as the destination Mat.
matThe destination Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE).
flipIf true, the pixel data retrieved from the WebCamTexture is flipped before conversion. The default is true, as the pixel data must be flipped to align with the coordinate system of the destination Mat.
flipCodeSpecifies how to flip the WebCamTexture image: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
Exceptions
ArgumentNullExceptionThrown when webCamTexture or mat is null.
ArgumentException

◆ WebCamTextureToMat() [2/2]

static void OpenCVForUnity.UnityIntegration.OpenCVMatUtils.WebCamTextureToMat ( WebCamTexture webCamTexture,
Mat mat,
Color32[] pixels32Buffer,
bool flip = true,
int flipCode = 0 )
static

Converts a Unity WebCamTexture to an OpenCV Mat.

This method converts a Unity WebCamTexture image to an OpenCV Mat. Conversion is possible even when the number of bytes per pixel differs, such as from WebCamTexture(RGBA32) to Mat(8UC1). In the case of multi-channel color to 1-channel, it is converted to grayscale. Performance is optimal when the per-pixel data size and color order match, such as with Texture2D(RGBA32) and Mat(8UC4). It is recommended to use the pixels32Buffer argument to avoid repeated memory allocations.

Parameters
webCamTextureThe source WebCamTexture must have the same size as the destination Mat.
matThe destination Mat must be 2-dimensional, with a CvType of 'CV_8UC4' (RGBA), 'CV_8UC3' (RGB), or 'CV_8UC1' (GRAYSCALE).
pixels32BufferAn optional array for receiving pixel data as Color32. Using this array helps avoid memory allocation each frame. Ensure the array is initialized to a length matching the texture’s width * height. (WebCamTexture.GetPixels32)
flipIf true, the pixel data retrieved from the WebCamTexture is flipped before conversion. The default is true, as the pixel data must be flipped to align with the coordinate system of the destination Mat.
flipCodeSpecifies how to flip the WebCamTexture image: Vertical flipping of the image (flipCode == 0) to flip around the x-axis, horizontal flipping of the image (flipCode > 0, e.g., 1) to flip around the y-axis, and simultaneous horizontal and vertical flipping (flipCode < 0, e.g., -1) to flip around both axes. The default is 0.
Exceptions
ArgumentNullExceptionThrown when webCamTexture or mat is null.
ArgumentException

The documentation for this class was generated from the following files: