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

        })
相关推荐
limingade5 小时前
手机转SIP-手机做中继网关-落地线路对接软交换呼叫中心
android·智能手机·手机转sip·手机做sip中继网关·sip中继
RainbowC05 小时前
GapBuffer高效标记管理算法
android·算法
写不来代码的草莓熊5 小时前
vue前端面试题——记录一次面试当中遇到的题(9)
前端·javascript·vue.js
程序员码歌5 小时前
豆包Seedream4.0深度体验:p图美化与文生图创作
android·前端·后端
郝学胜-神的一滴6 小时前
Three.js光照技术详解:为3D场景注入灵魂
开发语言·前端·javascript·3d·web3·webgl
m0dw6 小时前
vue懒加载
前端·javascript·vue.js·typescript
、花无将6 小时前
PHP:下载、安装、配置,与apache搭建
android·php·apache
shaominjin1237 小时前
Android 约束布局(ConstraintLayout)的权重机制:用法与对比解析
android·网络
Python智慧行囊8 小时前
图像处理-opencv(一)
人工智能·opencv·计算机视觉
cecyci8 小时前
如何实现AI聊天机器人的打字机效果?
前端·javascript