【发布时间】:2015-07-15 05:29:27
【问题描述】:
目前我正在开发一个应用程序,它将检测照片中的圆圈。我已经设法为此编写了一个代码,但如果我让手机远离 PC 屏幕,它要么会出现误报,要么会出现误报。如何改进结果?我的意思是,有很多应用程序可以检测到小而不清楚的圆圈。
[更新]
我在摆弄GaussianBlur 和HoughCircles 中的值。改变
Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2); 到 Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 9, 9); 和双倍 param1 = 70, param2 = 72; 到 double param1 = 50, param2 = 52; 提高了结果,但还不够。
Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
CvType.CV_8UC1);
Mat grayMat = new Mat(bitmap.getWidth(), bitmap.getHeight(),
CvType.CV_8UC1);
Utils.bitmapToMat(bitmap, mat);
int colorChannels = (mat.channels() == 3) ? Imgproc.COLOR_BGR2GRAY
: ((mat.channels() == 4) ? Imgproc.COLOR_BGRA2GRAY : 1);
Imgproc.cvtColor(mat, grayMat, colorChannels);
Imgproc.GaussianBlur(grayMat, grayMat, new Size(9, 9), 2, 2);
// accumulator value
double dp = 1.2d;
// minimum distance between the center coordinates of detected circles in pixels
double minDist = 100;
int minRadius = 0, maxRadius = 0;
double param1 = 70, param2 = 72;
Mat circles = new Mat(bitmap.getWidth(),
bitmap.getHeight(), CvType.CV_8UC1);
Imgproc.HoughCircles(grayMat, circles,
Imgproc.CV_HOUGH_GRADIENT, dp, minDist, param1,
param2, minRadius, maxRadius);
int numberOfCircles = 9;
if (numberOfCircles > circles.cols()){
numberOfCircles = circles.cols();
}
for (int i=0; i<numberOfCircles; i++) {
double[] circleCoordinates = circles.get(0, i);
if(circleCoordinates == null){
break;
}
int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];
Point center = new Point(x, y);
android.graphics.Point centerC = new android.graphics.Point(x, y);
int radius = (int) circleCoordinates[2];
Core.circle(mat, center, radius, new Scalar(0,
255, 0), 4);
Core.rectangle(mat, new Point(x - 5, y - 5),
new Point(x + 5, y + 5),
new Scalar(0, 128, 255), -1);
提前致谢。
现在我使用这些 A 形点来测试代码,但我想检测照片上更小的圆圈。
【问题讨论】:
-
您是否尝试过 param1 和 param2 的不同参数集?
-
没有。我没有。你认为我应该如何尝试改变它们。增加参数 2?
-
我的第一个猜测是减少 param2。你会有更多的误报,但我认为处理误报比处理误报更容易。其次,尝试减少 param1。这提高了 Canny 边缘检测器的灵敏度,当您想要检测从背景中不明显突出的圆圈时,这可能会很好。
-
好的。我会尝试玩价值观。当圈数少于 9 个时,您知道如何修复崩溃吗?
-
发布了答案。让我知道参数调整是否成功。
标签: java android opencv geometry