Android 集成OpenCV

记录自己在学习使用OpenCV的过程 我使用的是4.10.0 版本

Android 集成OpenCV 步骤

  1. 下载OpenCV
  2. 新建工程
  3. 依赖OpenCV
  4. 初始化及逻辑处理

1、下载OpenCV 并解压到自己的电脑

官网 地址:https://opencv.org/releases/

个人地址:https://pan.baidu.com/s/19fPjLcV9Y7ZRL3-Uqy-XAQ

提取码: encv

2、新建工程

在Android Studio 中创建一个空的项目

步骤


3、依赖 OpenCV

a、集成OpenCV到工程


b、在app中添加依赖




至此app已依赖OpenCV

初始化及逻辑处理

1、初始化
java 复制代码
  if (OpenCVLoader.initLocal()) {
            Log.i(TAG, "OpenCV loaded successfully")
        } else {
            Log.e(TAG, "OpenCV initialization failed!")
            Toast.makeText(this, "OpenCV initialization failed!", Toast.LENGTH_LONG)
                .show()
            return
        }
2、逻辑处理

本次为图片灰度处理的例子

本案例中使用了registerForActivityResult 跳转页面,因为StartActivityForResult 已过时

java 复制代码
  val getImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result->
           if (result.resultCode == Activity.RESULT_OK) {
               val uri: Uri? = result.data?.data
               println(result.data)
               println(uri)
               bitmap = BitmapFactory.decodeStream(uri?.let { contentResolver.openInputStream(it) })
               imageView.setImageBitmap(bitmap)
           }
        }
        click(getImage)
java 复制代码
 fun click(getImage: ActivityResultLauncher<Intent>) {
        button.setOnClickListener{
            val intents = Intent(Intent.ACTION_GET_CONTENT).apply {
                type="image/*"
                addCategory(Intent.CATEGORY_OPENABLE)
            }
            getImage.launch(intents);
        }
        button2.setOnClickListener {
            if (bitmap == null){
                return@setOnClickListener;
            }
            var bit = bitmap!!.copy(Bitmap.Config.ARGB_8888, false);
            var src = Mat(bit?.height!!, bit?.width!!, CvType.CV_8UC3)
            Utils.bitmapToMat(bit,src);
            Imgproc.cvtColor(src,src,Imgproc.COLOR_BGR2GRAY);
            Utils.matToBitmap(src,bitmap);
            imageView.setImageBitmap(bitmap)
        }
    }

布局文件

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MissingConstraints"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:srcCompat="@tools:sample/avatars"
        android:scaleType="fitXY"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加载图片"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginLeft="20dp"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="灰度化"
       app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginRight="20dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

至此本次图片灰度处理已经结束

相关推荐
小鸡吃米…11 小时前
机器学习 - K - 中心聚类
人工智能·机器学习·聚类
好奇龙猫11 小时前
【AI学习-comfyUI学习-第三十节-第三十一节-FLUX-SD放大工作流+FLUX图生图工作流-各个部分学习】
人工智能·学习
沈浩(种子思维作者)11 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
saoys11 小时前
Opencv 学习笔记:图像掩膜操作(精准提取指定区域像素)
笔记·opencv·学习
minhuan11 小时前
大模型应用:大模型越大越好?模型参数量与效果的边际效益分析.51
人工智能·大模型参数评估·边际效益分析·大模型参数选择
Cherry的跨界思维12 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
MM_MS12 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
ASF1231415sd12 小时前
【基于YOLOv10n-CSP-PTB的大豆花朵检测与识别系统详解】
人工智能·yolo·目标跟踪
水如烟13 小时前
孤能子视角:“意识“的阶段性回顾,“感质“假说
人工智能
Carl_奕然13 小时前
【数据挖掘】数据挖掘必会技能之:A/B测试
人工智能·python·数据挖掘·数据分析