目前我正在开发一款应用程序,它将从照片中检测出圆形。我已经成功编写了代码,但是如果我将手机离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形点来测试代码,但是我想在照片上检测到更小的圆圈。