2301_81235206 2024-06-04 17:55 采纳率: 0%
浏览 24

OpenCVForUnity实现摄像头实时霍夫圆检测

我在做一个摄像头可以识别圆形的东西
根据一个博主写的人脸检测修改后,摄像头打开只显示灰色,如下,无法识别圆形,请问怎么解决这些问题?

img


Console如下:

img

文章:https://blog.csdn.net/GottaYiWanLiu/article/details/90442274

步骤如下:
第一步先导入OpenCVForUnity,给quad附加例子中的WebCamTextureToMatExample;

第二步将CircleDetect脚本附加给quad,检测圆形代码如下:

using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.Structured_lightModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity;
using OpenCVForUnityExample;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CircleDetect : MonoBehaviour
{
  
    
    WebCamTextureToMatExample webcamTexToMat = new WebCamTextureToMatExample();
    Mat gray;
    Texture2D texture;
    
    public void Start()
    {
        
        
        webcamTexToMat = transform.GetComponent<WebCamTextureToMatExample>();
        Mat gray = new Mat();

        //texture = new Texture2D(640, 480);
        //GetComponent<Renderer>().material.mainTexture = texture;

        
    }
    public void ProcessImage()
    {
        //webcamTexToMat = transform.GetComponent<WebCamTextureToMatExample>();
        //Mat gray = new Mat();

        //texture = new Texture2D(640, 480);
        //GetComponent<Renderer>().material.mainTexture = texture;

        // 转换为灰度图像

        Imgproc.cvtColor(webcamTexToMat.rgbaMat, gray, Imgproc.COLOR_RGBA2GRAY);


        // 高斯模糊
        Imgproc.GaussianBlur(gray, gray, new Size(9, 9), 2, 2);


        // 检测圆形
        Mat circles = new Mat();
        Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1,10, 200, 100, 0, 0);


        // 绘制检测到的圆形
        for (int i = 0; i < circles.cols(); i++)
        {
            double[] circleData = circles.get(0, i);
            Point center = new Point(circleData[0], circleData[1]);
            int radius = (int)circleData[2];
            Imgproc.circle(webcamTexToMat.rgbaMat, center, radius, new Scalar(255, 0, 0), 5);
        }


        // 显示结果
        //Utils.matToTexture2D(webcamTexToMat.rgbaMat, texture);

    }
    

}

第三步根据博主修改WebCamTextureToMatExample中的一部分代码如图

img

  • 写回答

1条回答 默认 最新

  • weixin_44119674 2024-07-09 15:59
    关注
    
    using UnityEngine;
    using UnityEngine.UI;
    using OpenCVForUnity.CoreModule;
    using OpenCVForUnity.ImgprocModule;
    using OpenCVForUnity.UnityUtils;
    using OpenCVForUnity.VideoioModule;
    using OpenCVForUnityExample;
    
    public class CircleDetection : MonoBehaviour
    {
        public RawImage rawImage;
    
        private WebCamTexture webCamTexture;
        private Mat rgbaMat;
        private Texture2D texture;
    
        void Start()
        {
            webCamTexture = new WebCamTexture();
            webCamTexture.Play();
    
            rgbaMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
            texture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
    
            rawImage.texture = texture;
        }
    
        void Update()
        {
            if (webCamTexture.didUpdateThisFrame)
            {
                Utils.webCamTextureToMat(webCamTexture, rgbaMat);
    
                Mat grayMat = new Mat();
                Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
                Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2);
    
                Mat circles = new Mat();
                Imgproc.HoughCircles(grayMat, circles, Imgproc.HOUGH_GRADIENT, 1, grayMat.rows() / 8, 100, 20, 0, 0);
    
                for (int i = 0; i < circles.cols(); i++)
                {
                    double[] circle = circles.get(0, i);
                    if (circle == null) break;
    
                    Point center = new Point(circle[0], circle[1]);
                    int radius = (int)circle[2];
                    Imgproc.circle(rgbaMat, center, radius, new Scalar(255, 0, 0, 255), 3);
                }
    
                Utils.fastMatToTexture2D(rgbaMat, texture);
            }
        }
    
        void OnDestroy()
        {
            if (webCamTexture != null)
            {
                webCamTexture.Stop();
            }
            if (rgbaMat != null)
            {
                rgbaMat.Dispose();
            }
        }
    }
    
    
    评论

报告相同问题?

问题事件

  • 修改了问题 6月4日
  • 创建了问题 6月4日

悬赏问题

  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式