我在做一个摄像头可以识别圆形的东西
根据一个博主写的人脸检测修改后,摄像头打开只显示灰色,如下,无法识别圆形,请问怎么解决这些问题?
Console如下:
文章: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中的一部分代码如图