当我在一张灰度图上通过opencv查找霍夫圆时能够找到圆,但是一旦对那个灰度图通过OpenCV进行二值化后则无法找到圆,以下为部分代码

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_test);

Mat srcMat=new Mat();

Utils.bitmapToMat(bitmap,srcMat);

Mat grayMat=new Mat();

//转为灰度图像

Imgproc.cvtColor(srcMat,grayMat,Imgproc.COLOR_RGB2GRAY);

Mat binaryMat =new Mat();

//二值化图像

Imgproc.threshold(grayMat,binaryMat,40,255,Imgproc.THRESH_BINARY);

//下面如果将参数 grayMat 换成 binaryMat后则无法找到圆

Mat effectMat = OpenCvHelper.findEffectArea(grayMat, 5, 20, 5,

seekbarWidth.getProgress(),

seekbarHeight.getProgress(),

seekbarIntervalx.getProgress(),

seekbarIntervaly.getProgress());

if (effectMat == null) {

Toast.makeText(this, "无法找到有效区域", Toast.LENGTH_SHORT).show();

return;

}

mImage.setImageBitmap(mat2Bitmap(effectMat));

下面是OpencvHelper.findEffectArea()中查找圆的代码:

Mat matchAnchorMat = new Mat();

Imgproc.HoughCircles(mat, matchAnchorMat, Imgproc.CV_HOUGH_GRADIENT, 1, minDist, param1, param2, minRadius, maxRadius);

int size = matchAnchorMat.cols();

Log.w(TAG,"findCircle() 圆的个数:"+size);

List pointList = new ArrayList<>();

for (int i = 0; i < size; i++) {

double[] data = matchAnchorMat.get(0, i);

CircleInfo circleInfo = new CircleInfo(data[0], data[1], data[2]);

pointList.add(circleInfo);

}

return pointList;

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐