Created
July 15, 2019 04:55
-
-
Save kazukitanaka0611/21e582b3c6068ed2091975fa2a7c21ad to your computer and use it in GitHub Desktop.
OpenCV For Unit GaussianBlurの最小コード
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 | |
{ | |
[Range(0, 200)] | |
public float gaussianSize = 0; | |
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; | |
} | |
//_bg = webCamTextureMat.clone(); | |
} | |
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.GaussianBlur(rgbaMat, rgbaMat, new Size(gaussianSize, gaussianSize), 0); | |
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