Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Created June 28, 2020 09:34
Show Gist options
  • Save kazukitanaka0611/801ec4b04dad90721563929fa95a427f to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/801ec4b04dad90721563929fa95a427f to your computer and use it in GitHub Desktop.
リアルタイムで円を検出
using OpenCvSharp;
using OpenCvSharp.Demo;
using UnityEngine;
public class RealTimeFilter : WebCamera
{
private Mat dstMat = new Mat ();
protected override void Awake ()
{
base.Awake ();
}
// Our sketch generation function
protected override bool ProcessTexture (WebCamTexture input, ref Texture2D output)
{
Mat img = OpenCvSharp.Unity.TextureToMat (input, TextureParameters);
Mat blurMat = new Mat ();
Cv2.MedianBlur (img, blurMat, 5);
Mat grayMat = new Mat ();
Cv2.CvtColor (blurMat, grayMat, ColorConversionCodes.BGR2GRAY);
Mat thresh = new Mat ();
Cv2.Threshold (grayMat, thresh, 127, 255, ThresholdTypes.BinaryInv);
CircleSegment[] circles = Cv2.HoughCircles (thresh, HoughMethods.Gradient, 1, 20, 50, 30, 0, 0);
for (int i = 0; i < circles.Length; i++)
{
Cv2.Circle (img, circles[i].Center, (int) circles[i].Radius, new Scalar (0, 255, 0), 3);
}
output = OpenCvSharp.Unity.MatToTexture (img, output);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment