Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Last active July 14, 2019 06:55
Show Gist options
  • Save kazukitanaka0611/66b570b10a933f1c010e4ef3cfe74875 to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/66b570b10a933f1c010e4ef3cfe74875 to your computer and use it in GitHub Desktop.
OpenCV For Unityno
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
{
public bool isGray = false;
public bool isHist = false;
public bool isThreshold = false;
[Range(0, 255)]
public double thresh = 0;
public bool isMorphologOpen = false;
public bool isCanny = false;
[Range(0, 200)]
public double cannyThreshold1 = 80;
[Range(0, 200)]
public double cannyThreshold2 = 100;
public bool isSobel = false;
[Range(0, 200)]
public int sobelDx = 0;
[Range(0, 200)]
public int sobelDy = 1;
public bool isLaplacian = false;
[Range(3, 9)]
public int laplacianDdepth = 3;
public bool isBitwiseNot = false;
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)
{
// グレースケール
if (isGray)
{
Imgproc.cvtColor(rgbaMat, rgbaMat, Imgproc.COLOR_RGBA2GRAY);
}
if (isHist)
{
// ヒストグラム平滑化
Imgproc.equalizeHist(rgbaMat, rgbaMat);
}
if (isThreshold)
{
// 二値化
//Imgproc.threshold(rgbaMat, rgbaMat, 0, 255.0, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Imgproc.threshold(rgbaMat, rgbaMat, thresh, 255.0, Imgproc.THRESH_BINARY);
if (isMorphologOpen)
{
Mat kernel = Mat.ones(3, 3, CvType.CV_8UC1);
Point anchor = new Point(-1, -1);
Imgproc.morphologyEx(rgbaMat, rgbaMat, Imgproc.MORPH_OPEN, kernel, anchor);
Imgproc.morphologyEx(rgbaMat, rgbaMat, Imgproc.MORPH_CLOSE, kernel, anchor);
}
}
if (isCanny)
{
// Cannyエッジ検出
Imgproc.Canny(rgbaMat, rgbaMat, cannyThreshold1, cannyThreshold2);
}
if (isSobel)
{
// Sobelエッジ検出
Imgproc.Sobel(rgbaMat, rgbaMat, -1, sobelDx, sobelDy);
}
if (isLaplacian)
{
// Laplacianエッジ検出
Imgproc.Laplacian(rgbaMat, rgbaMat, 0, laplacianDdepth);
}
if (isBitwiseNot)
{
// ネガポジの反転
OpenCVForUnity.CoreModule.Core.bitwise_not(rgbaMat, rgbaMat);
}
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