• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ramonrabello/OpenCV-Kotlin-Starter: Starter project sample that integrates OpenC ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

ramonrabello/OpenCV-Kotlin-Starter

开源软件地址(OpenSource Url):

https://github.com/ramonrabello/OpenCV-Kotlin-Starter

开源编程语言(OpenSource Language):

Java 98.7%

开源软件介绍(OpenSource Introduction):

OpenCV-Kotlin-Starter

This is a starter project that integrates OpenCV for Android allowing to build Computer Vision based apps.

Motivation

This sample was created to help Android developers to integrate OpenCV in Android Studio. It can be useful when you need to create or adapt your Android apps to use Computer Vision based algorithms and techniques provided by OpenCV. This project is a mix of the effort result in research, StackOverflow's answers, and my experience using OpenCV for Android.

Project Structure

The project is composed basically of 2 modules: app and opencv-lib. The former is a sample Android application module while the later is a library module that has all dependencies and source code of OpenCV to run correctly on Android Studio and Gradle. If you clone this repository, you will need almost anything to setup regarding OpenCV integration. But if you want to do this manually, please read the next section.

Libraries

This sample app was built using as minimum library as possible in order to keep it very clean. The current version of this project uses the following dependencies:

  • Kotlin JDK 7 Standard Library
  • Support Libraries (appcompat-v7, design)
  • ConstraintLayout 1.1.2
  • JUnit 4 (for Unit Testing)
  • Support Test Runner (for running Instrumentation Testing)
  • JUnit 4 Test Rules
  • Espresso 3.0.2 (for UI Testing)

Downloading and Setup

  1. Go to the OpenCV releases page and click on Android pack link of the current version section
  2. Unzip the file named opencv-<version>-android-sdk
  3. In Android Studio, go to File > New > Import Module... and the choose <opencv_root_folder>/sdk/java and wait for the sync to finish
  4. Create the jniLibs folder inside src/main and copy the content of <opencv_root_folder>/sdk/native/libs to it. This could take a longer time because all .so for all device architecture (x86, mips, ...) for each OpenCV modules (imgproc, imcodecs, highui,...) will also be copied. IMPORTANT: this process can increase a lot the overall size of the final APK. So, if you want to fine tune and optimize to run in a minimal configuration, just delete the unused modules.
  5. Create the folder aidl inside src/main and copy org/opencv/engine/OpenCVEngineInterface.aidl to it.
  6. To finish, just import the opencv-lib library module inside the build.gradle of the app module.
  7. Sync your project and wait for the build to finish. If all the above steps succeed, voila! Congratulations! You do had correctly integrated OpenCV inside your app.

Kotlin Extension Functions for OpenCV

The package extensions inside the app module contains some extension functions related to Mat class. So now, instead of doing the following boilerplate code to convert a Bitmap using the Canny Edge Transform:

val bitmap = ... // get a Bitmap
val mat = Mat()
Utils.bitmapToMat(bitmap, mat)
Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY)
Imgproc.Canny(mat, mat, 50, 50)
val newBitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Config.ARGB_8888)
Utils.matToBitmap(mat, newBitmap)
imageView.setImageBitmap(newBitmap)

Thanks to the great power of Kotlin extension functions, you can reduce considerably all this boilerplate by doing only this:

// using default values for thresholds
mat.canny(srcBitmap) {
    imageView.setImageBitmap(it) // 'it' is the result Bitmap
}

// using custom values for thresholds
mat.canny(srcBitmap, 50, 100) {
    imageView.setImageBitmap(it) // it is the result Bitmap
}

All the pre-processing like converting to gray scale channel, Mat instantiation, Bitmap conversion, etc are being handled by the Mat.canny() extension function. The available extension functions are:

  • Mat.toGray(): Converts to gray scale channel (from RGB)
  • Mat.gaussianBlur(): Apply Gaussian Blur Algorithm
  • Mat.canny(): Apply Canny Edge Algorithm
  • Mat.threshold(): Apply Threshold Algorithm (BINARY or BINARY_INV)
  • Mat.adaptiveThreshold(): Apply Adaptive Threshold Algorithm
  • Mat.toBitmap(): Converts Mat to Bitmap representation
  • Mat.inGray(): Checks if Bitmap Mat is in gray scale
  • Bitmap.toMat(): Converts to a Mat representation of a Bitmap

More extensions functions will be added in the future. Any pull request, suggestions, bug issues are welcome to better enhance and evolve this project.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap