Skip to content

Commit cb16be8

Browse files
committed
update 2.6.1
1 parent d1d8b5f commit cb16be8

11 files changed

+3557
-8
lines changed

Assets/OpenCVForUnity/Examples/Basic/MultiSource2MatHelperExample.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
using OpenCVForUnity.CoreModule;
2+
using OpenCVForUnity.ImgprocModule;
3+
using OpenCVForUnity.UnityUtils;
4+
using OpenCVForUnity.UnityUtils.Helper;
5+
using UnityEngine;
6+
using UnityEngine.SceneManagement;
7+
using UnityEngine.UI;
8+
using static OpenCVForUnity.UnityUtils.Helper.MultiSource2MatHelper;
9+
10+
namespace OpenCVForUnityExample
11+
{
12+
/// <summary>
13+
/// MultiSource2MatHelper Example
14+
///
15+
/// An example of image processing by switching between multiple input sources using MultiSource2MatHelper.
16+
/// </summary>
17+
[RequireComponent(typeof(MultiSource2MatHelper))]
18+
public class MultiSource2MatHelperExample : MonoBehaviour
19+
{
20+
enum Source2MatHelperClassNamePreset : int
21+
{
22+
WebCamTexture2MatHelper = 0,
23+
VideoCapture2MatHelper,
24+
Image2MatHelper,
25+
}
26+
27+
Source2MatHelperClassNamePreset requestedSource2MatHelperClassName = Source2MatHelperClassNamePreset.WebCamTexture2MatHelper;
28+
29+
/// <summary>
30+
/// The change camera botton.
31+
/// </summary>
32+
public Button changeCameraBotton;
33+
34+
/// <summary>
35+
/// The requested source 2 mat helper class name dropdown.
36+
/// </summary>
37+
public Dropdown requestedSource2MatHelperClassNameDropdown;
38+
39+
/// <summary>
40+
/// The texture.
41+
/// </summary>
42+
Texture2D texture;
43+
44+
/// <summary>
45+
/// The multi source to mat helper.
46+
/// </summary>
47+
MultiSource2MatHelper multiSourceToMatHelper;
48+
49+
/// <summary>
50+
/// The FPS monitor.
51+
/// </summary>
52+
FpsMonitor fpsMonitor;
53+
54+
// Use this for initialization
55+
void Start()
56+
{
57+
fpsMonitor = GetComponent<FpsMonitor>();
58+
59+
// Get the MultiSource2MatHelper component attached to the current game object
60+
multiSourceToMatHelper = gameObject.GetComponent<MultiSource2MatHelper>();
61+
62+
// Set the requested ColorFormat
63+
multiSourceToMatHelper.requestedSource2MatHelperClassName = MultiSource2MatHelperClassName.WebCamTexture2MatHelper;
64+
multiSourceToMatHelper.outputColorFormat = Source2MatHelperColorFormat.RGBA;
65+
66+
// Initialize the source to Mat helper.
67+
multiSourceToMatHelper.Initialize();
68+
69+
// Update GUI state
70+
changeCameraBotton.interactable = multiSourceToMatHelper.source2MatHelper is ICameraSource2MatHelper;
71+
requestedSource2MatHelperClassNameDropdown.value = (int)multiSourceToMatHelper.GetCurrentSource2MatHelperClassName();
72+
}
73+
74+
/// <summary>
75+
/// Raises the source to mat helper initialized event.
76+
/// </summary>
77+
public void OnSourceToMatHelperInitialized()
78+
{
79+
Debug.Log("OnSourceToMatHelperInitialized");
80+
81+
// Retrieve the current frame from the Source2MatHelper as a Mat object
82+
Mat rgbaMat = multiSourceToMatHelper.GetMat();
83+
84+
// Create a new Texture2D with the same dimensions as the Mat and RGBA32 color format
85+
texture = new Texture2D(rgbaMat.cols(), rgbaMat.rows(), TextureFormat.RGBA32, false);
86+
87+
// Convert the Mat to a Texture2D, effectively transferring the image data
88+
Utils.matToTexture2D(rgbaMat, texture);
89+
90+
// Set the Texture2D as the main texture of the Renderer component attached to the game object
91+
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
92+
93+
// Adjust the scale of the game object to match the dimensions of the texture
94+
gameObject.transform.localScale = new Vector3(rgbaMat.cols(), rgbaMat.rows(), 1);
95+
Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
96+
97+
98+
if (fpsMonitor != null)
99+
{
100+
fpsMonitor.Add("deviceName", multiSourceToMatHelper.GetDeviceName().ToString());
101+
fpsMonitor.Add("width", multiSourceToMatHelper.GetWidth().ToString());
102+
fpsMonitor.Add("height", multiSourceToMatHelper.GetHeight().ToString());
103+
fpsMonitor.Add("orientation", Screen.orientation.ToString());
104+
fpsMonitor.Add("helperClassName", multiSourceToMatHelper.GetCurrentSource2MatHelperClassName().ToString());
105+
106+
switch (multiSourceToMatHelper.source2MatHelper)
107+
{
108+
case ICameraSource2MatHelper helper:
109+
fpsMonitor.Add("camera fps", helper.GetFPS().ToString());
110+
fpsMonitor.Add("isFrontFacing", helper.IsFrontFacing().ToString());
111+
break;
112+
case IVideoSource2MatHelper helper:
113+
fpsMonitor.Add("video path", helper.requestedVideoFilePath.ToString());
114+
fpsMonitor.Add("video fps", helper.GetFPS().ToString());
115+
fpsMonitor.Add("loop", helper.loop.ToString());
116+
break;
117+
case IImageSource2MatHelper helper:
118+
fpsMonitor.Add("image path", helper.requestedImageFilePath.ToString());
119+
fpsMonitor.Add("repeat", helper.repeat.ToString());
120+
break;
121+
}
122+
123+
if (multiSourceToMatHelper.source2MatHelper is WebCamTexture2MatHelper webCamHelper)
124+
{
125+
fpsMonitor.Add("rotate90Degree", webCamHelper.rotate90Degree.ToString());
126+
fpsMonitor.Add("flipVertical", webCamHelper.flipVertical.ToString());
127+
fpsMonitor.Add("flipHorizontal", webCamHelper.flipHorizontal.ToString());
128+
}
129+
}
130+
131+
132+
// Get the width and height of the webCamTextureMat
133+
float width = rgbaMat.width();
134+
float height = rgbaMat.height();
135+
136+
// Calculate the scale factors for width and height based on the screen dimensions
137+
float widthScale = (float)Screen.width / width;
138+
float heightScale = (float)Screen.height / height;
139+
140+
// Adjust the orthographic size of the main Camera to fit the aspect ratio of the image
141+
if (widthScale < heightScale)
142+
{
143+
// If the width scale is smaller, adjust the orthographic size based on width and screen height
144+
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
145+
}
146+
else
147+
{
148+
// If the height scale is smaller or equal, adjust the orthographic size based on height
149+
Camera.main.orthographicSize = height / 2;
150+
}
151+
}
152+
153+
/// <summary>
154+
/// Raises the source to mat helper disposed event.
155+
/// </summary>
156+
public void OnSourceToMatHelperDisposed()
157+
{
158+
Debug.Log("OnSourceToMatHelperDisposed");
159+
160+
// Destroy the texture and set it to null
161+
if (texture != null)
162+
{
163+
Texture2D.Destroy(texture);
164+
texture = null;
165+
}
166+
167+
if (fpsMonitor != null)
168+
fpsMonitor.Clear();
169+
}
170+
171+
/// <summary>
172+
/// Raises the source to mat helper error occurred event.
173+
/// </summary>
174+
/// <param name="errorCode">Error code.</param>
175+
/// <param name="message">Message.</param>
176+
public void OnSourceToMatHelperErrorOccurred(Source2MatHelperErrorCode errorCode, string message)
177+
{
178+
Debug.Log("OnSourceToMatHelperErrorOccurred " + errorCode + ":" + message);
179+
180+
if (fpsMonitor != null)
181+
{
182+
fpsMonitor.consoleText = "ErrorCode: " + errorCode + ":" + message;
183+
}
184+
}
185+
186+
// Update is called once per frame
187+
void Update()
188+
{
189+
// Check if the web camera is playing and if a new frame was updated
190+
if (multiSourceToMatHelper.IsPlaying() && multiSourceToMatHelper.DidUpdateThisFrame())
191+
{
192+
// Retrieve the current frame as a Mat object
193+
Mat rgbaMat = multiSourceToMatHelper.GetMat();
194+
195+
// Add text overlay on the frame
196+
//Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
197+
198+
// Convert the Mat to a Texture2D to display it on a texture
199+
Utils.matToTexture2D(rgbaMat, texture);
200+
}
201+
}
202+
203+
/// <summary>
204+
/// Raises the destroy event.
205+
/// </summary>
206+
void OnDestroy()
207+
{
208+
// Dispose of the SourceToMatHelper object and release any resources held by it.
209+
multiSourceToMatHelper.Dispose();
210+
}
211+
212+
/// <summary>
213+
/// Raises the back button click event.
214+
/// </summary>
215+
public void OnBackButtonClick()
216+
{
217+
// Load the specified scene when the back button is clicked
218+
SceneManager.LoadScene("OpenCVForUnityExample");
219+
}
220+
221+
/// <summary>
222+
/// Raises the play button click event.
223+
/// </summary>
224+
public void OnPlayButtonClick()
225+
{
226+
multiSourceToMatHelper.Play();
227+
}
228+
229+
/// <summary>
230+
/// Raises the pause button click event.
231+
/// </summary>
232+
public void OnPauseButtonClick()
233+
{
234+
multiSourceToMatHelper.Pause();
235+
}
236+
237+
/// <summary>
238+
/// Raises the stop button click event.
239+
/// </summary>
240+
public void OnStopButtonClick()
241+
{
242+
multiSourceToMatHelper.Stop();
243+
}
244+
245+
/// <summary>
246+
/// Raises the change camera button click event.
247+
/// </summary>
248+
public void OnChangeCameraButtonClick()
249+
{
250+
multiSourceToMatHelper.requestedIsFrontFacing = !multiSourceToMatHelper.requestedIsFrontFacing;
251+
}
252+
253+
/// <summary>
254+
/// Raises the requested source 2 mat helper class name dropdown value changed event.
255+
/// </summary>
256+
public void OnRequestedSource2MatHelperClassNameDropdownValueChanged(int result)
257+
{
258+
if ((int)requestedSource2MatHelperClassName != result)
259+
{
260+
requestedSource2MatHelperClassName = (Source2MatHelperClassNamePreset)result;
261+
262+
switch (requestedSource2MatHelperClassName)
263+
{
264+
case Source2MatHelperClassNamePreset.WebCamTexture2MatHelper:
265+
multiSourceToMatHelper.requestedSource2MatHelperClassName = MultiSource2MatHelperClassName.WebCamTexture2MatHelper;
266+
break;
267+
case Source2MatHelperClassNamePreset.VideoCapture2MatHelper:
268+
multiSourceToMatHelper.requestedSource2MatHelperClassName = MultiSource2MatHelperClassName.VideoCapture2MatHelper;
269+
break;
270+
case Source2MatHelperClassNamePreset.Image2MatHelper:
271+
multiSourceToMatHelper.requestedSource2MatHelperClassName = MultiSource2MatHelperClassName.Image2MatHelper;
272+
break;
273+
}
274+
275+
// Way to perform different processing depending on the interface inherited by the helper class.
276+
changeCameraBotton.interactable = multiSourceToMatHelper.source2MatHelper is ICameraSource2MatHelper;
277+
}
278+
}
279+
}
280+
}

Assets/OpenCVForUnity/Examples/Basic/MultiSource2MatHelperExample/MultiSource2MatHelperExample.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)