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.Helper.VideoCapture2MatHelper Class Reference

A helper component class for obtaining video frames from a file using OpenCV's VideoCapture and converting them to OpenCV Mat format. More...

Public Member Functions

virtual bool DidUpdateThisFrame ()
 Use this to check if the Mat has changed since the last frame. Since it would not make sense to do expensive video processing in each Update call, check this value before doing any processing.
 
virtual void Dispose ()
 Releases all resource used by the VideoCapture2MatHelper object.
 
virtual Source2MatHelperColorFormat GetBaseColorFormat ()
 Return the video base color format.
 
virtual string GetDeviceName ()
 Return the active video device name.
 
virtual float GetFPS ()
 Return the video framerate.
 
virtual int GetFrameCount ()
 Return the number of frames in the current video content.
 
virtual int GetFrameIndex ()
 Return the video frame index.
 
virtual float GetFramePosRatio ()
 Return the relative position of the video file: 0=start of the film, 1=end of the film.
 
virtual int GetHeight ()
 Return the video height.
 
virtual Mat GetMat ()
 Get the mat of the current frame.
 
virtual VideoCapture GetVideoCapture ()
 Return the VideoCapture instance.
 
virtual int GetWidth ()
 Return the video width.
 
virtual void Initialize (bool autoPlay=true)
 Initialize this instance.
 
virtual void Initialize (string requestedVideoFilePath, bool autoPlay=true)
 Initialize this instance.
 
virtual bool IsInitialized ()
 Indicate whether this instance has been initialized.
 
virtual bool IsPaused ()
 Indicate whether the video is paused.
 
virtual bool IsPlaying ()
 Indicate whether the video is currently playing.
 
virtual void Pause ()
 Pause the video.
 
virtual void Play ()
 Start the video.
 
virtual void SetFrameIndex (int index)
 Set the video frame index.
 
virtual void SetFramePosRatio (float ratio)
 Set the relative position of the video file: 0=start of the film, 1=end of the film.
 
virtual void Stop ()
 Stop the video.
 
- Public Member Functions inherited from OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper
- Public Member Functions inherited from OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper

Protected Member Functions

virtual IEnumerator _Initialize ()
 Initialize this instance by coroutine.
 
virtual IEnumerator _WaitForEndOfFrameCoroutine ()
 
virtual void CallReadFrame ()
 
virtual void CancelInitCoroutine ()
 Cancel Init Coroutine.
 
virtual void CancelWaitForEndOfFrameCoroutine ()
 Cancel WaitForEndOfFrame Coroutine.
 
virtual void OnDestroy ()
 Raises the destroy event.
 
virtual void OnValidate ()
 
virtual void ReadFrame ()
 
virtual void ReleaseResources ()
 To release the resources.
 
virtual void Update ()
 

Protected Attributes

int _apiPreference = Videoio.CAP_ANY
 
Mat _imageBufferMat
 
System.Object _imageBufferMatLockObject = new System.Object()
 
bool _isPlaying = false
 
bool _loop = true
 
UnityEvent _onDisposed
 
Source2MatHelperErrorUnityEvent _onErrorOccurred
 
UnityEvent _onInitialized
 
Source2MatHelperColorFormat _outputColorFormat = Source2MatHelperColorFormat.BGR
 
string _requestedVideoFilePath = string.Empty
 
System.Object _sync = new System.Object()
 
int _timeoutFrameCount = 1500
 
VideoCapture _videoCapture
 
System.Object _videoCaptureLockObject = new System.Object()
 
bool autoPlayAfterInitialize
 If set to true play after completion of initialization.
 
Source2MatHelperColorFormat baseColorFormat = Source2MatHelperColorFormat.BGR
 The base color format.
 
Mat baseMat
 The base mat.
 
bool didUpdateThisFrame = false
 Whether the mat that can be obtained with the GetMat method has been updated in this frame. This flag is changed after waiting until WaitForEndOfFrame in the coroutine.
 
Mat frameMat
 The frame mat.
 
IEnumerator getFilePathCoroutine
 The get file path coroutine.
 
bool hasInitDone = false
 Indicates whether this instance has been initialized.
 
IEnumerator initCoroutine
 The initialization coroutine.
 
bool isInitWaiting = false
 Indicates whether this instance is waiting for initialization to complete.
 
IEnumerator waitForEndOfFrameCoroutine
 The waitForEndOfFrameCoroutine.
 

Properties

virtual int apiPreference [get, set]
 Set the apiPreference. VideoCapture API backends identifier. (Advanced Option) See ReadMe.pdf for setup instructions for using CAP_FFMPEG on Windows platforms.
 
Mat imageBufferMat [get, set]
 The image buffer mat.
 
virtual bool isPlaying [get, set]
 isPlaying
 
virtual bool loop [get, set]
 Indicate whether to play this video in a loop.
 
UnityEvent onDisposed [get, set]
 UnityEvent that is triggered when this instance is disposed.
 
Source2MatHelperErrorUnityEvent onErrorOccurred [get, set]
 UnityEvent that is triggered when this instance is error Occurred.
 
UnityEvent onInitialized [get, set]
 UnityEvent that is triggered when this instance is initialized.
 
virtual Source2MatHelperColorFormat outputColorFormat [get, set]
 Select the output color format.
 
virtual string requestedVideoFilePath [get, set]
 Set the video file path, relative to the starting point of the "StreamingAssets" folder, or absolute path.
 
virtual int timeoutFrameCount [get, set]
 The number of frames before the initialization process times out.
 
VideoCapture videoCapture [get, set]
 videoCapture
 
- Properties inherited from OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper
- Properties inherited from OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper

Detailed Description

A helper component class for obtaining video frames from a file using OpenCV's VideoCapture and converting them to OpenCV Mat format.

The VideoCapture2MatHelper class uses OpenCV's VideoCapture to read frames from a specified video file and converts each frame to an OpenCV Mat object. This component automatically retrieves the appropriate frame based on the video's frame rate, ensuring smooth playback and accurate frame timing.

To ensure compatibility across platforms, it is recommended to use the MJPEG format for video files, as it is widely supported by the VideoCapture class.

This component is particularly useful for applications that require frame-by-frame processing of video files in Unity, allowing seamless integration with OpenCV-based image processing routines.

Note: By setting outputColorFormat to BGR, processing that does not include extra color conversion is performed.

Attach this component to a GameObject and call GetMat() to retrieve the latest video frame in Mat format. The helper class manages video file loading, frame retrieval, and frame updates internally based on the video's playback rate.

Member Function Documentation

◆ _Initialize()

virtual IEnumerator OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._Initialize ( )
protectedvirtual

Initialize this instance by coroutine.

◆ _WaitForEndOfFrameCoroutine()

virtual IEnumerator OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._WaitForEndOfFrameCoroutine ( )
protectedvirtual

◆ CallReadFrame()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.CallReadFrame ( )
protectedvirtual

◆ CancelInitCoroutine()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.CancelInitCoroutine ( )
protectedvirtual

Cancel Init Coroutine.

◆ CancelWaitForEndOfFrameCoroutine()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.CancelWaitForEndOfFrameCoroutine ( )
protectedvirtual

Cancel WaitForEndOfFrame Coroutine.

◆ DidUpdateThisFrame()

virtual bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.DidUpdateThisFrame ( )
virtual

Use this to check if the Mat has changed since the last frame. Since it would not make sense to do expensive video processing in each Update call, check this value before doing any processing.

Returns
true, if the Mat has been updated false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ Dispose()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.Dispose ( )
virtual

Releases all resource used by the VideoCapture2MatHelper object.

Call Dispose when you are finished using the VideoCapture2MatHelper. The Dispose method leaves the VideoCapture2MatHelper in an unusable state. After calling Dispose, you must release all references to the VideoCapture2MatHelper so the garbage collector can reclaim the memory that the VideoCapture2MatHelper was occupying.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetBaseColorFormat()

virtual Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetBaseColorFormat ( )
virtual

Return the video base color format.

Returns
The video base color format.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetDeviceName()

virtual string OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetDeviceName ( )
virtual

Return the active video device name.

Returns
The active video device name.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetFPS()

virtual float OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetFPS ( )
virtual

Return the video framerate.

Returns
The video framerate.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetFrameCount()

virtual int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetFrameCount ( )
virtual

Return the number of frames in the current video content.

Returns
The number of frames in the current video content.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetFrameIndex()

virtual int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetFrameIndex ( )
virtual

Return the video frame index.

Returns
The video frame index.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetFramePosRatio()

virtual float OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetFramePosRatio ( )
virtual

Return the relative position of the video file: 0=start of the film, 1=end of the film.

Returns
The relative position of the video file: 0=start of the film, 1=end of the film.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetHeight()

virtual int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetHeight ( )
virtual

Return the video height.

Returns
The video height.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetMat()

virtual Mat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetMat ( )
virtual

Get the mat of the current frame.

The Mat object's type is 'CV_8UC4' or 'CV_8UC3' or 'CV_8UC1' (ColorFormat is determined by the outputColorFormat setting). Please do not dispose of the returned mat as it will be reused.

Returns
The mat of the current frame.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetVideoCapture()

virtual VideoCapture OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetVideoCapture ( )
virtual

Return the VideoCapture instance.

Returns
The VideoCapture instance.

◆ GetWidth()

virtual int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.GetWidth ( )
virtual

Return the video width.

Returns
The video width.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ Initialize() [1/2]

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.Initialize ( bool autoPlay = true)
virtual

Initialize this instance.

Parameters
autoPlayIf set to true play after completion of initialization.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ Initialize() [2/2]

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.Initialize ( string requestedVideoFilePath,
bool autoPlay = true )
virtual

Initialize this instance.

Parameters
requestedVideoFilePathRequested video file path.
autoPlayIf set to true play after completion of initialization.

◆ IsInitialized()

virtual bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.IsInitialized ( )
virtual

Indicate whether this instance has been initialized.

Returns
true, if this instance has been initialized, false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ IsPaused()

virtual bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.IsPaused ( )
virtual

Indicate whether the video is paused.

Returns
true, if the video is paused, false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ IsPlaying()

virtual bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.IsPlaying ( )
virtual

Indicate whether the video is currently playing.

Returns
true, if the video is playing, false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ OnDestroy()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.OnDestroy ( )
protectedvirtual

Raises the destroy event.

◆ OnValidate()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.OnValidate ( )
protectedvirtual

◆ Pause()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.Pause ( )
virtual

◆ Play()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.Play ( )
virtual

◆ ReadFrame()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.ReadFrame ( )
protectedvirtual

◆ ReleaseResources()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.ReleaseResources ( )
protectedvirtual

To release the resources.

◆ SetFrameIndex()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.SetFrameIndex ( int index)
virtual

Set the video frame index.

Parameters
indexThe video frame index.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ SetFramePosRatio()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.SetFramePosRatio ( float ratio)
virtual

Set the relative position of the video file: 0=start of the film, 1=end of the film.

Parameters
ratioThe relative position of the video file: 0=start of the film, 1=end of the film.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ Stop()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.Stop ( )
virtual

◆ Update()

virtual void OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.Update ( )
protectedvirtual

Member Data Documentation

◆ _apiPreference

int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._apiPreference = Videoio.CAP_ANY
protected

◆ _imageBufferMat

Mat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._imageBufferMat
protected

◆ _imageBufferMatLockObject

System.Object OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._imageBufferMatLockObject = new System.Object()
protected

◆ _isPlaying

bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._isPlaying = false
protected

◆ _loop

bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._loop = true
protected

◆ _onDisposed

UnityEvent OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._onDisposed
protected

◆ _onErrorOccurred

Source2MatHelperErrorUnityEvent OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._onErrorOccurred
protected

◆ _onInitialized

UnityEvent OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._onInitialized
protected

◆ _outputColorFormat

Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._outputColorFormat = Source2MatHelperColorFormat.BGR
protected

◆ _requestedVideoFilePath

string OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._requestedVideoFilePath = string.Empty
protected

◆ _sync

System.Object OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._sync = new System.Object()
protected

◆ _timeoutFrameCount

int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._timeoutFrameCount = 1500
protected

◆ _videoCapture

VideoCapture OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._videoCapture
protected

◆ _videoCaptureLockObject

System.Object OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper._videoCaptureLockObject = new System.Object()
protected

◆ autoPlayAfterInitialize

bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.autoPlayAfterInitialize
protected

If set to true play after completion of initialization.

◆ baseColorFormat

Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.baseColorFormat = Source2MatHelperColorFormat.BGR
protected

The base color format.

◆ baseMat

Mat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.baseMat
protected

The base mat.

◆ didUpdateThisFrame

bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.didUpdateThisFrame = false
protected

Whether the mat that can be obtained with the GetMat method has been updated in this frame. This flag is changed after waiting until WaitForEndOfFrame in the coroutine.

◆ frameMat

Mat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.frameMat
protected

The frame mat.

◆ getFilePathCoroutine

IEnumerator OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.getFilePathCoroutine
protected

The get file path coroutine.

◆ hasInitDone

bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.hasInitDone = false
protected

Indicates whether this instance has been initialized.

◆ initCoroutine

IEnumerator OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.initCoroutine
protected

The initialization coroutine.

◆ isInitWaiting

bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.isInitWaiting = false
protected

Indicates whether this instance is waiting for initialization to complete.

◆ waitForEndOfFrameCoroutine

IEnumerator OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.waitForEndOfFrameCoroutine
protected

The waitForEndOfFrameCoroutine.

Property Documentation

◆ apiPreference

virtual int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.apiPreference
getset

Set the apiPreference. VideoCapture API backends identifier. (Advanced Option) See ReadMe.pdf for setup instructions for using CAP_FFMPEG on Windows platforms.

◆ imageBufferMat

Mat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.imageBufferMat
getsetprotected

The image buffer mat.

◆ isPlaying

virtual bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.isPlaying
getsetprotected

isPlaying

◆ loop

virtual bool OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.loop
getset

Indicate whether to play this video in a loop.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ onDisposed

UnityEvent OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.onDisposed
getset

UnityEvent that is triggered when this instance is disposed.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ onErrorOccurred

Source2MatHelperErrorUnityEvent OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.onErrorOccurred
getset

UnityEvent that is triggered when this instance is error Occurred.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ onInitialized

UnityEvent OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.onInitialized
getset

UnityEvent that is triggered when this instance is initialized.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ outputColorFormat

virtual Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.outputColorFormat
getset

Select the output color format.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ requestedVideoFilePath

virtual string OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.requestedVideoFilePath
getset

Set the video file path, relative to the starting point of the "StreamingAssets" folder, or absolute path.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ timeoutFrameCount

virtual int OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.timeoutFrameCount
getset

The number of frames before the initialization process times out.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ videoCapture

VideoCapture OpenCVForUnity.UnityUtils.Helper.VideoCapture2MatHelper.videoCapture
getsetprotected

videoCapture


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