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 = 0
                    var maxIndex = 0

                    for (x in 0 until circles.cols()) {
                        val circleVec = circles[0, x]
                        val radius = circleVec[2].toInt()
                        if (radius > maxRadius) {
                            maxRadius = radius
                            maxIndex = 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()
            }

        })
相关推荐
花花鱼1 小时前
vue3 axios ant-design-vue cdn的方式使用
前端·javascript·vue.js
叶落无痕521 小时前
关于安卓App自动化的一些想法
android·运维·自动化·android studio
架构师ZYL2 小时前
node.js+Koa框架+MySQL实现注册登录
前端·javascript·数据库·mysql·node.js
gxhlh3 小时前
React Native防止重复点击
javascript·react native·react.js
miao_zz3 小时前
基于react native的锚点
android·react native·react.js
安卓美女3 小时前
Android自定义View性能优化
android·性能优化
一只小白菜~3 小时前
实现实时Web应用,使用AJAX轮询、WebSocket、还是SSE呢??
前端·javascript·websocket·sse·ajax轮询
jingling5553 小时前
后端开发刷题 | 数字字符串转化成IP地址
java·开发语言·javascript·算法
Dingdangr4 小时前
Android中的四大组件
android
mg6685 小时前
安卓玩机工具-----无需root权限 卸载 禁用 删除当前机型app应用 ADB玩机工具
android