Last active
July 14, 2019 02:27
-
-
Save kazukitanaka0611/3bb9754b86a9c6aaebef8874c3cdc4fa to your computer and use it in GitHub Desktop.
OpenCV For Unityグレースケール変換最小のコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenCVForUnity.CoreModule; | |
using OpenCVForUnity.ImgprocModule; | |
using OpenCVForUnity.UnityUtils; | |
using OpenCVForUnity.UnityUtils.Helper; | |
using OpenCVForUnityExample; | |
using UnityEngine; | |
[RequireComponent(typeof(WebCamTextureToMatHelper), typeof(FpsMonitor))] | |
public class MyFilterSample : MonoBehaviour | |
{ | |
private Texture2D texture; | |
private WebCamTextureToMatHelper webCamTextureToMatHelper; | |
private FpsMonitor fpsMonitor; | |
private void Start() | |
{ | |
fpsMonitor = GetComponent<FpsMonitor>(); | |
webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper>(); | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
// Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2). | |
webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true; | |
#endif | |
webCamTextureToMatHelper.Initialize(); | |
} | |
public void OnWebCamTextureToMatHelperInitialized() | |
{ | |
Debug.Log("OnWebCamTextureToMatHelperInitialized"); | |
Mat webCamTextureMat = webCamTextureToMatHelper.GetMat(); | |
texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false); | |
gameObject.GetComponent<Renderer>().material.mainTexture = texture; | |
gameObject.transform.localScale = new Vector3(webCamTextureMat.cols(), webCamTextureMat.rows(), 1); | |
Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); | |
if (fpsMonitor != null) | |
{ | |
fpsMonitor.Add("width", webCamTextureMat.width().ToString()); | |
fpsMonitor.Add("height", webCamTextureMat.height().ToString()); | |
fpsMonitor.Add("orientation", Screen.orientation.ToString()); | |
} | |
float width = webCamTextureMat.width(); | |
float height = webCamTextureMat.height(); | |
float widthScale = Screen.width / width; | |
float heightScale = Screen.height / height; | |
if (widthScale < heightScale) | |
{ | |
Camera.main.orthographicSize = (width * Screen.height / Screen.width) / 2; | |
} | |
else | |
{ | |
Camera.main.orthographicSize = height / 2; | |
} | |
} | |
public void OnWebCamTextureToMatHelperDisposed() | |
{ | |
Debug.Log("OnWebCamTextureToMatHelperDisposed"); | |
if (texture != null) | |
{ | |
Texture2D.Destroy(texture); | |
texture = null; | |
} | |
} | |
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode) | |
{ | |
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode); | |
} | |
private void Update() | |
{ | |
if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame()) | |
{ | |
Mat rgbaMat = webCamTextureToMatHelper.GetMat(); | |
Filter(rgbaMat); | |
Utils.fastMatToTexture2D(rgbaMat, texture); | |
} | |
} | |
private void OnDestroy() | |
{ | |
webCamTextureToMatHelper.Dispose(); | |
} | |
private void Filter(Mat rgbaMat) | |
{ | |
Imgproc.cvtColor(rgbaMat, rgbaMat, Imgproc.COLOR_RGBA2GRAY); | |
Imgproc.cvtColor(rgbaMat, rgbaMat, Imgproc.COLOR_GRAY2RGBA); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment