如下:

using UnityEngine;
using UnityEngine.UI;
using OpenCvSharp;

public class Circle_Text : MonoBehaviour
{

    //このScriptはMainCameraにアタッチしてください
    public RenderTexture renderTexture;             //mainCameraにつけるRendertexture(アタッチしてね)
    Texture2D kakunin, dstTexture;
    Camera mainCamera;
    Mat mats;

    Vector3 Circle;

    void Start()
    {
        //mainCamera = GetComponent<Camera>();
        //kakunin = CreateTexture2D(renderTexture);
        //Tex2D_to_Mat_show(kakunin);
    }
    private void Update()
    {
        mainCamera = GetComponent<Camera>();
        kakunin = CreateTexture2D(renderTexture);
        mats = Tex2D_to_Mat(kakunin);
        HoughCircles(mats);
    }

    /// ここでTextur2Dに変換しているよ
    /// </summary>
    /// <param name="rt"></param>
    /// <returns></returns>
    Texture2D CreateTexture2D(RenderTexture rt)
    {
        //Texture2Dを作成
        Texture2D texture2D = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false, false);

        //subCameraにRenderTextureを入れる
        mainCamera.targetTexture = rt;

        //手動でカメラをレンダリングします
        mainCamera.Render();

        RenderTexture.active = rt;
        texture2D.ReadPixels(new UnityEngine.Rect(0, 0, rt.width, rt.height), 0, 0);
        texture2D.Apply();

        ////元に戻す別のカメラを用意してそれをRenderTexter用にすれば下のコードはいらないです。
        //mainCamera.targetTexture = null;
        //RenderTexture.active = null;
        return texture2D;
    }

    Mat Tex2D_to_Mat(Texture2D tex)
    {
        //Texture2D -> Mat
        Mat srcMat = OpenCvSharp.Unity.TextureToMat(tex);

        //Mat grayMat = new Mat();
        //Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.RGBA2GRAY);


        // Mat → Texture2D
        //if (this.dstTexture == null)
        //{
        //    this.dstTexture = new Texture2D(grayMat.Width, grayMat.Height, TextureFormat.RGBA32, false);
        //}

        //OpenCvSharp.Unity.MatToTexture(grayMat, this.dstTexture);

        //// 表示
        //GameObject.FindObjectOfType<RawImage>().texture = this.dstTexture;

        return srcMat;
    }
    void HoughCircles(Mat Mat)
    {
        Mat dst = new Mat();
        //1:因为霍夫圆检测对噪声比较敏感,所以首先对图像做一个中值滤波或高斯滤波(噪声如果没有可以不做)
        Mat m1 = new Mat();
        Cv2.MedianBlur(Mat, m1, 3); //  ksize必须大于1且是奇数

        //2:转为灰度图像
        Mat m2 = new Mat();
        Cv2.CvtColor(m1, m2, ColorConversionCodes.BGR2GRAY);

        //3:霍夫圆检测:使用霍夫变换查找灰度图像中的圆。
        /*
         * 参数:
         *      1:输入参数: 8位、单通道、灰度输入图像
         *      2:实现方法:目前,唯一的实现方法是HoughCirclesMethod.Gradient
         *      3: dp      :累加器分辨率与图像分辨率的反比。默认=1
         *      4:minDist: 检测到的圆的中心之间的最小距离。(最短距离-可以分辨是两个圆的,否则认为是同心圆-                            src_gray.rows/8)
         *      5:param1:   第一个方法特定的参数。[默认值是100] canny边缘检测阈值低
         *      6:param2:   第二个方法特定于参数。[默认值是100] 中心点累加器阈值 – 候选圆心
         *      7:minRadius: 最小半径
         *      8:maxRadius: 最大半径
         * 
         */
        CircleSegment[] cs = Cv2.HoughCircles(m2, HoughMethods.Gradient, 1, 80, 70, 30, 50, 100);
        Mat.CopyTo(dst);
        // Vec3d vec = new Vec3d();
        for (int i = 0; i < cs.Length; i++)
        {
            //画圆
            Cv2.Circle(dst, (int)cs[i].Center.X, (int)cs[i].Center.Y, (int)cs[i].Radius, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
            //加强圆心显示
            Cv2.Circle(dst, (int)cs[i].Center.X, (int)cs[i].Center.Y, 3, new Scalar(0, 0, 255), 2, LineTypes.AntiAlias);
        }
        // Mat → Texture2D
        if (this.dstTexture == null)
        {
            this.dstTexture = new Texture2D(dst.Width, dst.Height, TextureFormat.RGBA32, false);
        }

        OpenCvSharp.Unity.MatToTexture(dst, this.dstTexture);

        // 表示
        GameObject.FindObjectOfType<RawImage>().texture = this.dstTexture;
        Cv2.WaitKey(1);

        //using (new Window("InputImage", WindowMode.AutoSize, dst))
        //{
        //    Cv2.WaitKey(1);
        //}

    }


}

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/HanaKoo/p/16206780.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!