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

        })
相关推荐
用户2367829801686 小时前
从零实现 GIF 制作工具:LZW 压缩与 Median Cut 色彩量化
前端·javascript
棉猴6 小时前
Python海龟绘图之绘制文本
javascript·python·html·write·turtle·海龟绘图·输出文本
Highcharts.js7 小时前
线形比赛积分增长或竞赛图|Highcharts企业图表代码示列
开发语言·前端·javascript·折线图·highcharts·竞赛图
让学习成为一种生活方式7 小时前
大肠杆菌合成扑热息痛--对乙酰氨基酚--文献精读227
开发语言·前端·javascript
czlczl200209257 小时前
IN和BETWEEN在索引效能的区别
android·adb
Volunteer Technology7 小时前
ES高级搜索功能
android·大数据·elasticsearch
多秋浮沉度华年7 小时前
electron 初始使用记录
javascript·arcgis·electron
北京自在科技7 小时前
Find Hub App 小更新
android·ios·安卓·findmy·airtag
lbb 小魔仙8 小时前
2026远程办公软件夏季深度横测:ToDesk、向日葵、网易UU远程全面对比,远控白皮书
android·服务器·网络协议·tcp/ip·postgresql
竹林8188 小时前
用 wagmi v2 + WebSocket 硬磕 NFT 上架失败:一个前端开发者踩过的实时状态同步坑
javascript·next.js