1+ #if ! ( PLATFORM_LUMIN && ! UNITY_EDITOR )
2+
3+ using OpenCVForUnity . CoreModule ;
4+ using OpenCVForUnity . ImgprocModule ;
5+ using OpenCVForUnity . UnityUtils ;
6+ using OpenCVForUnity . UnityUtils . Helper ;
7+ using UnityEngine ;
8+ using UnityEngine . SceneManagement ;
9+
10+ namespace OpenCVForUnityExample
11+ {
12+ /// <summary>
13+ /// Image Correction Example
14+ /// An example of image correction including contrast, brightness, gamma, and thresholding.
15+ /// Referring to https://docs.opencv.org/3.4/d3/dc1/tutorial_basic_linear_transform.html.
16+ /// </summary>
17+ [ RequireComponent ( typeof ( WebCamTextureToMatHelper ) ) ]
18+ public class ImageCorrectionExample : MonoBehaviour
19+ {
20+ private float contrast = 1f ;
21+ private float brightness = 0f ;
22+ private float gamma = 1f ;
23+ private bool thresholdEnabled = false ;
24+ private float threshold = 127f ;
25+
26+ private Mat lut ;
27+
28+ /// <summary>
29+ /// The gray1 mat.
30+ /// </summary>
31+ Mat grayMat ;
32+
33+ /// <summary>
34+ /// The texture.
35+ /// </summary>
36+ Texture2D texture ;
37+
38+ /// <summary>
39+ /// The webcam texture to mat helper.
40+ /// </summary>
41+ WebCamTextureToMatHelper webCamTextureToMatHelper ;
42+
43+ // Use this for initialization
44+ void Start ( )
45+ {
46+ webCamTextureToMatHelper = gameObject . GetComponent < WebCamTextureToMatHelper > ( ) ;
47+ webCamTextureToMatHelper . outputColorFormat = WebCamTextureToMatHelper . ColorFormat . RGB ;
48+ webCamTextureToMatHelper . Initialize ( ) ;
49+
50+ // Create the initial LUT
51+ CreateLUT ( ) ;
52+ }
53+
54+ /// <summary>
55+ /// Raises the webcam texture to mat helper initialized event.
56+ /// </summary>
57+ public void OnWebCamTextureToMatHelperInitialized ( )
58+ {
59+ Debug . Log ( "OnWebCamTextureToMatHelperInitialized" ) ;
60+
61+ Mat webCamTextureMat = webCamTextureToMatHelper . GetMat ( ) ;
62+
63+ texture = new Texture2D ( webCamTextureMat . cols ( ) , webCamTextureMat . rows ( ) , TextureFormat . RGB24 , false ) ;
64+ Utils . matToTexture2D ( webCamTextureMat , texture ) ;
65+
66+ gameObject . GetComponent < Renderer > ( ) . material . mainTexture = texture ;
67+
68+ gameObject . transform . localScale = new Vector3 ( webCamTextureMat . cols ( ) , webCamTextureMat . rows ( ) , 1 ) ;
69+ Debug . Log ( "Screen.width " + Screen . width + " Screen.height " + Screen . height + " Screen.orientation " + Screen . orientation ) ;
70+
71+
72+ float width = webCamTextureMat . width ( ) ;
73+ float height = webCamTextureMat . height ( ) ;
74+
75+ float widthScale = ( float ) Screen . width / width ;
76+ float heightScale = ( float ) Screen . height / height ;
77+ if ( widthScale < heightScale )
78+ {
79+ Camera . main . orthographicSize = ( width * ( float ) Screen . height / ( float ) Screen . width ) / 2 ;
80+ }
81+ else
82+ {
83+ Camera . main . orthographicSize = height / 2 ;
84+ }
85+
86+ grayMat = new Mat ( webCamTextureMat . rows ( ) , webCamTextureMat . cols ( ) , CvType . CV_8UC1 ) ;
87+ }
88+
89+ /// <summary>
90+ /// Raises the webcam texture to mat helper disposed event.
91+ /// </summary>
92+ public void OnWebCamTextureToMatHelperDisposed ( )
93+ {
94+ Debug . Log ( "OnWebCamTextureToMatHelperDisposed" ) ;
95+
96+ if ( grayMat != null )
97+ {
98+ grayMat . Dispose ( ) ;
99+ grayMat = null ;
100+ }
101+
102+ if ( texture != null )
103+ {
104+ Texture2D . Destroy ( texture ) ;
105+ texture = null ;
106+ }
107+ }
108+
109+ /// <summary>
110+ /// Raises the webcam texture to mat helper error occurred event.
111+ /// </summary>
112+ /// <param name="errorCode">Error code.</param>
113+ public void OnWebCamTextureToMatHelperErrorOccurred ( WebCamTextureToMatHelper . ErrorCode errorCode )
114+ {
115+ Debug . Log ( "OnWebCamTextureToMatHelperErrorOccurred " + errorCode ) ;
116+ }
117+
118+ // Update is called once per frame
119+ void Update ( )
120+ {
121+ if ( webCamTextureToMatHelper . IsPlaying ( ) && webCamTextureToMatHelper . DidUpdateThisFrame ( ) )
122+ {
123+
124+ Mat rgbMat = webCamTextureToMatHelper . GetMat ( ) ;
125+
126+ // Adjust brightness and contrast
127+ Core . convertScaleAbs ( rgbMat , rgbMat , contrast , brightness ) ;
128+
129+ // Adjust gamma value if it has changed
130+ if ( lut == null || Mathf . Abs ( gamma - ( float ) lut . get ( 0 , 0 ) [ 0 ] ) > float . Epsilon )
131+ {
132+ gamma = Mathf . Max ( gamma , 0.01f ) ; // Ensure gamma is non-zero
133+ CreateLUT ( ) ;
134+ }
135+
136+ // Apply gamma correction using the LUT
137+ Core . LUT ( rgbMat , lut , rgbMat ) ;
138+
139+ // Apply threshold
140+ if ( thresholdEnabled )
141+ {
142+ // Convert the image to grayscale
143+ Imgproc . cvtColor ( rgbMat , grayMat , Imgproc . COLOR_RGB2GRAY ) ;
144+
145+ // Apply thresholding
146+ Imgproc . threshold ( grayMat , grayMat , threshold , 255 , Imgproc . THRESH_BINARY ) ;
147+
148+ // Convert the image to RGB
149+ Imgproc . cvtColor ( grayMat , rgbMat , Imgproc . COLOR_GRAY2RGB ) ;
150+ }
151+
152+ Imgproc . putText ( rgbMat , "contrast:" + contrast . ToString ( "F2" ) + " brightness:" + brightness . ToString ( "F2" ) + " gamma:" + gamma . ToString ( "F2" ) + " threshold:" + threshold . ToString ( "F2" ) , new Point ( 5 , rgbMat . rows ( ) - 10 ) , Imgproc . FONT_HERSHEY_SIMPLEX , 0.6 , new Scalar ( 255 , 255 , 255 , 255 ) , 1 , Imgproc . LINE_AA , false ) ;
153+
154+ Utils . matToTexture2D ( rgbMat , texture ) ;
155+ }
156+ }
157+
158+ private void CreateLUT ( )
159+ {
160+ lut = new Mat ( 1 , 256 , CvType . CV_8UC1 ) ;
161+
162+ for ( int i = 0 ; i < 256 ; i ++ )
163+ {
164+ double gammaCorrection = Mathf . Pow ( ( float ) i / 255f , 1f / gamma ) * 255f ;
165+ lut . put ( 0 , i , gammaCorrection ) ;
166+ }
167+ }
168+
169+ /// <summary>
170+ /// Raises the destroy event.
171+ /// </summary>
172+ void OnDestroy ( )
173+ {
174+ webCamTextureToMatHelper . Dispose ( ) ;
175+ }
176+
177+ /// <summary>
178+ /// Raises the back button click event.
179+ /// </summary>
180+ public void OnBackButtonClick ( )
181+ {
182+ SceneManager . LoadScene ( "OpenCVForUnityExample" ) ;
183+ }
184+
185+ /// <summary>
186+ /// Raises the play button click event.
187+ /// </summary>
188+ public void OnPlayButtonClick ( )
189+ {
190+ webCamTextureToMatHelper . Play ( ) ;
191+ }
192+
193+ /// <summary>
194+ /// Raises the pause button click event.
195+ /// </summary>
196+ public void OnPauseButtonClick ( )
197+ {
198+ webCamTextureToMatHelper . Pause ( ) ;
199+ }
200+
201+ /// <summary>
202+ /// Raises the stop button click event.
203+ /// </summary>
204+ public void OnStopButtonClick ( )
205+ {
206+ webCamTextureToMatHelper . Stop ( ) ;
207+ }
208+
209+ /// <summary>
210+ /// Raises the change camera button click event.
211+ /// </summary>
212+ public void OnChangeCameraButtonClick ( )
213+ {
214+ webCamTextureToMatHelper . requestedIsFrontFacing = ! webCamTextureToMatHelper . requestedIsFrontFacing ;
215+ }
216+
217+ public void OnContrastSliderValueChanged ( float value )
218+ {
219+ contrast = value ;
220+ }
221+
222+ public void OnBrightnessSliderValueChanged ( float value )
223+ {
224+ brightness = value ;
225+ }
226+
227+ public void OnGammaSliderValueChanged ( float value )
228+ {
229+ gamma = value ;
230+ }
231+
232+ public void OnApplayThresholdToggleValueChanged ( bool value )
233+ {
234+ thresholdEnabled = value ;
235+ }
236+
237+ public void OnThresholdSliderValueChanged ( float value )
238+ {
239+ threshold = value ;
240+ }
241+ }
242+ }
243+
244+ #endif
0 commit comments