【发布时间】:2015-07-09 14:32:11
【问题描述】:
我需要任何 C# 和/或 OpenCV 专家的帮助,以使我的圆检测脚本更准确。
在 OpenCV 中,圆检测是通过称为 HoughCircles 算法或框架的方法完成的。
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
我正在使用 OpenCV 的 C# 包装器(用于 Unity)
OpenCVforUnity HughCircles
这又直接基于 OpenCV 的官方 java 包装器。
我的圆圈检测代码如下(当然没有OpenCv依赖) 我还附上了 2 张图片,以便您查看结果。 需要哪些改变来改善结果?我还附上了原始的 2 张图片以供参考。
using UnityEngine;
using System.Collections;
using System;
using OpenCVForUnity;
public class HoughCircleSample : MonoBehaviour{
Point pt;
// Use this for initialization
void Start ()
{
Texture2D imgTexture = Resources.Load ("balls2_bw") as Texture2D;
Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC3);
Utils.texture2DToMat (imgTexture, imgMat);
//Debug.Log ("imgMat dst ToString " + imgMat.ToString ());
Mat grayMat = new Mat ();
Imgproc.cvtColor (imgMat, grayMat, Imgproc.COLOR_RGB2GRAY);
Imgproc.Canny (grayMat, grayMat, 50, 200);
Mat circles = new Mat();
int minRadius = 0;
int maxRadius = 0;
// Apply the Hough Transform to find the circles
Imgproc.HoughCircles(grayMat, circles, Imgproc.CV_HOUGH_GRADIENT, 3, grayMat.rows() / 8, 200, 100, minRadius, maxRadius);
Debug.Log ("circles toString " + circles.ToString ());
Debug.Log ("circles dump" + circles.dump ());
if (circles.cols() > 0)
for (int x = 0; x < Math.Min(circles.cols(), 10); x++)
{
double[] vCircle = circles.get(0, x);
if (vCircle == null)
break;
pt = new Point(Math.Round(vCircle[0]), Math.Round(vCircle[1]));
int radius = (int)Math.Round(vCircle[2]);
// draw the found circle
Core.circle(imgMat, pt, radius, new Scalar(255, 0, 0), 1);
}
Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
Utils.matToTexture2D (imgMat, texture);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
}
}
【问题讨论】:
-
如果 C++ 中的解决方案没问题,请上传原始图像以便我尝试。但是接下来由您决定转换为 C#
-
看看是否符合您的预期..
-
谢谢,但仅更改累加器阈值会产生几乎相同的结果。不过我越来越近了,请看下文。
-
你也删除了 Canny 步骤吗?
标签: c# opencv unity3d hough-transform