您的位置:首页 > 科技 > IT业 > Android 结合Opencv检测画面中的圆

Android 结合Opencv检测画面中的圆

2024/10/6 16:23:57 来源:https://blog.csdn.net/qq_37324563/article/details/142061829  浏览:    关键词:Android 结合Opencv检测画面中的圆

记下来不用找

Opencv资源https://download.csdn.net/download/qq_37324563/89729678

版本可能比较老,凑合用吧

     mBinding.cameraview.setCvCameraViewListener(object : CameraBridgeViewBase.CvCameraViewListener2 {/***  当摄像机预览开始时,这个方法就会被调用。在调用该方法之后,框架将通过onCameraFrame()回调向客户端发送。** @param width  - 帧的宽度* @param height - 帧的高度*/override fun onCameraViewStarted(width: Int, height: Int) {//定义Mat对象rgba = Mat(width, height, CvType.CV_8UC4)}/*** 当摄像机预览由于某种原因被停止时,这个方法就会被调用。*在调用这个方法之后,不会通过onCameraFrame()回调来传递任何帧。*/override fun onCameraViewStopped() {}/*** 当需要完成框架的交付时,将调用此方法。*返回值-是一个修改后的帧,需要在屏幕上显示。* @param inputFrame*/override fun onCameraFrame(inputFrame: CameraBridgeViewBase.CvCameraViewFrame): Mat? {val input = inputFrame.gray()val circles = Mat()Imgproc.blur(input, input, Size(7.0, 7.0), Point(2.0, 2.0))Imgproc.HoughCircles(input, circles, Imgproc.CV_HOUGH_GRADIENT, 2.0, 100.0, 100.0, 90.0, 0, 1000)if (circles.cols() > 0) {var maxRadius = 0var maxIndex = 0for (x in 0 until circles.cols()) {val circleVec = circles[0, x]val radius = circleVec[2].toInt()if (radius > maxRadius) {maxRadius = radiusmaxIndex = x}}val center = Point(circles[0, maxIndex][0].toInt().toDouble(), circles[0, maxIndex][1].toInt().toDouble())//  Imgproc.circle(input, center, 3, Scalar(255.0, 255.0, 255.0), 5)Imgproc.circle(input, center, maxRadius, Scalar(255.0, 0.0, 0.0), 2) // 使用不同的颜色突出显示最大半径的圆}circles.release()input.release()return inputFrame.rgba()}})

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com