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()
            }

        })
相关推荐
茶憶19 小时前
UniApp 安卓端实现文件的生成,写入,获取文件大小以及压缩功能
android·javascript·vue.js·uni-app
2501_9159214319 小时前
uni-app 的 iOS 打包与上架流程,多工具协作
android·ios·小程序·uni-app·cocoa·iphone·webview
Lei活在当下1 天前
【Perfetto从入门到精通】4.使用 heapprofd 工具采样追踪 Java/Native 内存分配
android·性能优化·架构
alexhilton1 天前
学会在Jetpack Compose中加载Lottie动画资源
android·kotlin·android jetpack
信看1 天前
NMEA-GNSS-RTK 定位html小工具
前端·javascript·html
爱吃大芒果1 天前
Flutter 主题与深色模式:全局样式统一与动态切换
开发语言·javascript·flutter·ecmascript·gitcode
king王一帅1 天前
流式渲染 Incremark、ant-design-x markdown、streammarkdown-vue 全流程方案对比
前端·javascript·人工智能
summerkissyou19871 天前
Android-Camera-为啥不移到packages/module
android·相机
liang_jy1 天前
Android UID
android·面试