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

Public Types

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

Static Public Member Functions

static int fastMatToTexture2D (Mat mat, Texture2D texture2D, bool flip=true, int flipCode=0, bool updateMipmaps=false, bool makeNoLongerReadable=false)
 
static int fastTexture2DToMat (Texture2D texture2D, Mat mat, bool flip=true, int flipCode=0)
 
static string getFilePath (string filepath, bool refresh=false, int timeout=0)
 Gets the readable path of a file in the "StreamingAssets" directory.
 
static IEnumerator getFilePathAsync (string filepath, Action< string > completed, Action< string, float > progressChanged=null, Action< string, string, long > errorOccurred=null, bool refresh=false, int timeout=0)
 Asynchronously retrieves the readable path of a file in the "StreamingAssets" directory using coroutines.
 
static async Task< string > getFilePathAsyncTask (string filepath, Action< string, float > progressChanged=null, Action< string, string, long > errorOccurred=null, bool refresh=false, int timeout=0, CancellationToken cancellationToken=default)
 Asynchronously retrieves the readable path of a file in the "StreamingAssets" directory using a Task.
 
static IReadOnlyList< string > getMultipleFilePaths (IReadOnlyList< string > filepaths, bool refresh=false, int timeout=0)
 Gets the multiple readable paths of a file in the "StreamingAssets" directory.
 
static IEnumerator getMultipleFilePathsAsync (IReadOnlyList< string > filepaths, Action< IReadOnlyList< string > > allCompleted, Action< string > completed=null, Action< string, float > progressChanged=null, Action< string, string, long > errorOccurred=null, bool refresh=false, int timeout=0)
 Asynchronously retrieves the multiple readable paths of files in the "StreamingAssets" directory using coroutines.
 
static async Task< IReadOnlyList< string > > getMultipleFilePathsAsyncTask (IReadOnlyList< string > filepaths, Action< string > completed=null, Action< string, float > progressChanged=null, Action< string, string, long > errorOccurred=null, bool refresh=false, int timeout=0, CancellationToken cancellationToken=default)
 Asynchronously retrieves the multiple readable paths of files in the "StreamingAssets" directory using a Task.
 
static string getVersion ()
 Returns this "OpenCV for Unity" version number.
 
static void graphicsBufferToMat (GraphicsBuffer graphicsBuffer, Mat mat, CopyMode copyMode)
 Copies data from a Unity GraphicsBuffer to a OpenCV Mat.
 
static void matToGraphicsBuffer (Mat mat, GraphicsBuffer graphicsBuffer, CopyMode copyMode)
 Copies data from a OpenCv Mat to a Unity GraphicsBuffer.
 
static void matToRenderTexture (Mat mat, RenderTexture renderTexture, GraphicsBuffer graphicsBuffer, bool flip=true, int flipCode=0)
 Converts an OpenCV Mat to a Unity RenderTexture.
 
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 void setDebugMode (bool debugMode, bool throwException=false, Action< string > callback=null)
 Sets the debug mode.
 
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 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.
 

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

◆ fastMatToTexture2D()

static int OpenCVForUnity.UnityUtils.Utils.fastMatToTexture2D ( Mat mat,
Texture2D texture2D,
bool flip = true,
int flipCode = 0,
bool updateMipmaps = false,
bool makeNoLongerReadable = false )
static
Deprecated
Use matToTexture2DRaw method instead.

◆ fastTexture2DToMat()

static int OpenCVForUnity.UnityUtils.Utils.fastTexture2DToMat ( Texture2D texture2D,
Mat mat,
bool flip = true,
int flipCode = 0 )
static
Deprecated
Use texture2DToMatRaw method instead.

◆ getFilePath()

static string OpenCVForUnity.UnityUtils.Utils.getFilePath ( string filepath,
bool refresh = false,
int timeout = 0 )
static

Gets the readable path of a file in the "StreamingAssets" directory.

Provide a relative file path based on the "StreamingAssets" directory. e.g., "foobar.txt" or "hogehoge/foobar.txt". [Android] The target file that exists in the "StreamingAssets" directory is copied to the the Application.persistentDataPath directory. [WebGL] If the target file has not yet been copied to WebGL's virtual filesystem, it is necessary to use getFilePathAsync of getFilePathAsyncTask at first.

Parameters
filepathA file path relative to the "StreamingAssets" directory.
refresh[Android] If false, the file is not copied if it already exists. If true, the file is always copied.
timeout[Android] Sets the UnityWebRequest to abort after the specified number of seconds. If set to 0, no timeout is applied. The default is 0.
Returns
Returns the readable file path in case of success and returns string.Empty in case of error.
Exceptions
ArgumentNullExceptionThrown if filepath is null.

◆ getFilePathAsync()

static IEnumerator OpenCVForUnity.UnityUtils.Utils.getFilePathAsync ( string filepath,
Action< string > completed,
Action< string, float > progressChanged = null,
Action< string, string, long > errorOccurred = null,
bool refresh = false,
int timeout = 0 )
static

Asynchronously retrieves the readable path of a file in the "StreamingAssets" directory using coroutines.

Provide a relative file path based on the "StreamingAssets" directory. e.g., "foobar.txt" or "hogehoge/foobar.txt". [Android] The target file that exists in the "StreamingAssets" directory is copied to the the Application.persistentDataPath directory. [WebGL] The target file in the "StreamingAssets" directory is copied to the WebGL's virtual filesystem.

Parameters
filepathA file path relative to the "StreamingAssets" directory.
completedA callback that is called when the process is completed. Returns a readable file path in case of success and returns string.Empty in case of error.
progressChangedAn optional callback that is called when the process is the progress. Returns the file path and a progress value (0.0 to 1.0).
errorOccurredAn optional callback that is called when the process is error occurred. Returns the file path and an error string and an error response code.
refresh[Android][WebGL] If false, the file is not copied if it already exists. If true, the file is always copied.
timeout[Android][WebGL] Sets the UnityWebRequest to abort after the specified number of seconds. If set to 0, no timeout is applied. The default is 0.
Returns
Returns an IEnumerator object. Yielding the IEnumerator in a coroutine pauses the coroutine until the UnityWebRequest completes or encounters a system error. Note: that if the IEnumerator is externally stoped, the UnityWebRequest's Abort method will not be called, meaning the download will continue in the background.
Exceptions
ArgumentNullExceptionThrown if filepath is null.

◆ getFilePathAsyncTask()

static async Task< string > OpenCVForUnity.UnityUtils.Utils.getFilePathAsyncTask ( string filepath,
Action< string, float > progressChanged = null,
Action< string, string, long > errorOccurred = null,
bool refresh = false,
int timeout = 0,
CancellationToken cancellationToken = default )
static

Asynchronously retrieves the readable path of a file in the "StreamingAssets" directory using a Task.

Provide a relative file path based on the "StreamingAssets" directory. e.g., "foobar.txt" or "hogehoge/foobar.txt". [Android] The target file that exists in the "StreamingAssets" directory is copied to the the Application.persistentDataPath directory. [WebGL] The target file in the "StreamingAssets" directory is copied to the WebGL's virtual filesystem.

Parameters
filepathA file path relative to the "StreamingAssets" directory.
progressChangedAn optional callback that is called when the process is the progress. Returns the file path and a progress value (0.0 to 1.0).
errorOccurredAn optional callback that is called when the process is error occurred. Returns the file path and an error string and an error response code.
refresh[Android][WebGL] If false, the file is not copied if it already exists. If true, the file is always copied.
timeout[Android][WebGL] Sets the UnityWebRequest to abort after the specified number of seconds. If set to 0, no timeout is applied. The default is 0.
cancellationTokenA cancellation token that can be used to cancel the download operation.
Returns
A task that represents the asynchronous download operation. The result is a readable file path where the downloaded file was saved, or string.Empty if the download fails.
Exceptions
ArgumentNullExceptionThrown if filepath is null.
InvalidOperationExceptionThrown if this method is called from a non-main thread.
OperationCanceledExceptionThrown if the download operation is canceled.

◆ getMultipleFilePaths()

static IReadOnlyList< string > OpenCVForUnity.UnityUtils.Utils.getMultipleFilePaths ( IReadOnlyList< string > filepaths,
bool refresh = false,
int timeout = 0 )
static

Gets the multiple readable paths of a file in the "StreamingAssets" directory.

Provide a relative file path based on the "StreamingAssets" directory. e.g., "foobar.txt" or "hogehoge/foobar.txt". [Android] The target file that exists in the "StreamingAssets" directory is copied to the the Application.persistentDataPath directory. [WebGL] If the target file has not yet been copied to WebGL's virtual filesystem, it is necessary to use getMultipleFilePathsAsync of getMultipleFilePathsAsyncTask at first.

Parameters
filepathsThe list of file paths relative to the "StreamingAssets" directory.
refresh[Android] If false, the file is not copied if it already exists. If true, the file is always copied.
timeout[Android] Sets the UnityWebRequest to abort after the specified number of seconds. If set to 0, no timeout is applied. The default is 0.
Returns
Returns the list of readable file paths in case of success and returns string.Empty in case of error.
Exceptions
ArgumentNullExceptionThrown if filepath is null.

◆ getMultipleFilePathsAsync()

static IEnumerator OpenCVForUnity.UnityUtils.Utils.getMultipleFilePathsAsync ( IReadOnlyList< string > filepaths,
Action< IReadOnlyList< string > > allCompleted,
Action< string > completed = null,
Action< string, float > progressChanged = null,
Action< string, string, long > errorOccurred = null,
bool refresh = false,
int timeout = 0 )
static

Asynchronously retrieves the multiple readable paths of files in the "StreamingAssets" directory using coroutines.

Provide a relative file path based on the "StreamingAssets" directory. e.g., "foobar.txt" or "hogehoge/foobar.txt". [Android] The target file that exists in the "StreamingAssets" directory is copied to the the Application.persistentDataPath directory. [WebGL] The target file in the "StreamingAssets" directory is copied to the WebGL's virtual filesystem.

Parameters
filepathsThe list of file paths relative to the "StreamingAssets" directory.
allCompletedA callback that is called when all processes are completed. Returns a list of file paths. Returns a readable file path in case of success and returns string.Empty in case of error.
completedAn optional callback that is called when one process is completed. Returns a readable file path in case of success and returns empty in case of error.
progressChangedAn optional callback that is called when one process is the progress. Returns the file path and a progress value (0.0 to 1.0).
errorOccurredAn optional callback that is called when one process is error occurred. Returns the file path and an error string and an error response code.
refresh[Android][WebGL] If false, the file is not copied if it already exists. If true, the file is always copied.
timeout[Android][WebGL] Sets the UnityWebRequest to abort after the specified number of seconds. If set to 0, no timeout is applied. The default is 0.
Returns
Returns an IEnumerator object. Yielding the IEnumerator in a coroutine pauses the coroutine until the UnityWebRequest completes or encounters a system error. Note: that if the IEnumerator is externally stoped, the UnityWebRequest's Abort method will not be called, meaning the download will continue in the background.
Exceptions
ArgumentNullExceptionThrown if filepath is null.

◆ getMultipleFilePathsAsyncTask()

static async Task< IReadOnlyList< string > > OpenCVForUnity.UnityUtils.Utils.getMultipleFilePathsAsyncTask ( IReadOnlyList< string > filepaths,
Action< string > completed = null,
Action< string, float > progressChanged = null,
Action< string, string, long > errorOccurred = null,
bool refresh = false,
int timeout = 0,
CancellationToken cancellationToken = default )
static

Asynchronously retrieves the multiple readable paths of files in the "StreamingAssets" directory using a Task.

Provide a relative file path based on the "StreamingAssets" directory. e.g., "foobar.txt" or "hogehoge/foobar.txt". [Android] The target file that exists in the "StreamingAssets" directory is copied to the the Application.persistentDataPath directory. [WebGL] The target file in the "StreamingAssets" directory is copied to the WebGL's virtual filesystem.

Parameters
filepathsThe list of file paths relative to the "StreamingAssets" directory.
completedAn optional callback that is called when one process is completed. Returns a readable file path in case of success and returns empty in case of error.
progressChangedAn optional callback that is called when one process is the progress. Returns the file path and a progress value (0.0 to 1.0).
errorOccurredAn optional callback that is called when one process is error occurred. Returns the file path and an error string and an error response code.
refresh[Android][WebGL] If false, the file is not copied if it already exists. If true, the file is always copied.
timeout[Android][WebGL] Sets the UnityWebRequest to abort after the specified number of seconds. If set to 0, no timeout is applied. The default is 0.
cancellationTokenA cancellation token that can be used to cancel the download operation.
Returns
A task that represents the asynchronous download operation. The result is a list of readable file paths where the downloaded file was saved, or string.Empty if the download fails.
Exceptions
ArgumentNullExceptionThrown if filepath is null.
InvalidOperationExceptionThrown if this method is called from a non-main thread.
OperationCanceledExceptionThrown if the download operation is canceled.

◆ getVersion()

static string OpenCVForUnity.UnityUtils.Utils.getVersion ( )
static

Returns this "OpenCV for Unity" version number.

Returns
this "OpenCV for Unity" version number

◆ graphicsBufferToMat()

static void OpenCVForUnity.UnityUtils.Utils.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.

◆ matToGraphicsBuffer()

static void OpenCVForUnity.UnityUtils.Utils.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.

◆ matToRenderTexture()

static void OpenCVForUnity.UnityUtils.Utils.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.

◆ matToTexture2D() [1/4]

static void OpenCVForUnity.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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.

◆ setDebugMode()

static void OpenCVForUnity.UnityUtils.Utils.setDebugMode ( bool debugMode,
bool throwException = false,
Action< string > callback = null )
static

Sets the debug mode.

If debugMode is true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.However, if throwException is true, CvException is thrown instead of calling Debug.LogError (msg).

Please use as follows.

{
// CVException handling
// Publish CVException to Debug.LogError.
Utils.setDebugMode(true, false);
Mat m3 = m1 / m2; // element type is different.
// Throw CVException.
Utils.setDebugMode(true, true);
try
{
Mat m4 = m1 / m2; // element type is different.
}
catch (Exception e)
{
Debug.Log("CVException: " + e);
}
Utils.setDebugMode(false);
}
n-dimensional dense array class
Definition Mat_ValueTuple.cs:11
Definition Utils_ComputeShader.cs:14
static void setDebugMode(bool debugMode, bool throwException=false, Action< string > callback=null)
Sets the debug mode.
Definition Utils.cs:2088
Parameters
debugModeIf true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
throwExceptionIf true, CvException is thrown instead of calling Debug.LogError (msg).
callbackCallback called when an OpenCV error occurs on the Native side.

◆ texture2DToMat() [1/2]

static void OpenCVForUnity.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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

◆ textureToTexture2D()

static void OpenCVForUnity.UnityUtils.Utils.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.UnityUtils.Utils.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.UnityUtils.Utils.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: