使用 OpenCV for Android 進(jìn)行圖像特征檢測
android 開發(fā)人員,可能熟悉使用activities, fragments, intents以及最重要的一系列開源依賴庫。但是,注入需要本機(jī)功能的依賴關(guān)系(如計(jì)算機(jī)視覺框架)并不像在 gradle 文件中直接添加實(shí)現(xiàn)語句那樣簡單!
今天,將專注于使用 OpenCV 庫,將其作為依賴項(xiàng)注入到你的 android 應(yīng)用程序中。
那么,我們將從這次討論中得到什么?我們將能夠創(chuàng)建一個(gè) android 應(yīng)用程序并執(zhí)行所需的步驟來集成 OpenCV。
此外,我們將完成一個(gè)基于SIFT技術(shù)的圖像特征檢測算法。這將是你考慮構(gòu)建自己的 SDK 的良好起點(diǎn)。
什么是SIFT?
SIFT 代表尺度不變傅立葉變換(Scale Invariant Fourier Transform)。檢測器用于查找圖像上的興趣點(diǎn)。它使我們能夠識別圖像中的局部特征。
SIFT 的優(yōu)勢在于,即使我們大幅縮放圖像,它也能正常工作,因?yàn)樗鼘D像數(shù)據(jù)轉(zhuǎn)換為尺度不變坐標(biāo)。SIFT 使用“關(guān)鍵點(diǎn)”來表示圖像中縮放和旋轉(zhuǎn)不變的局部特征。這是我們將其用于各種應(yīng)用程序(如圖像匹配、對象檢測、場景檢測等)的基準(zhǔn)。
為了識別關(guān)鍵點(diǎn),該算法為你完成:
第 1 步:形成尺度空間——這一步確保特征與尺度無關(guān)。
第 2 步:關(guān)鍵點(diǎn)定位——這一步有助于識別合適的特征/關(guān)鍵點(diǎn)。
第 3 步:方向?qū)R——這一步確保關(guān)鍵點(diǎn)是旋轉(zhuǎn)后不變的。
第 4 步:關(guān)鍵點(diǎn)描述符——這是為每個(gè)關(guān)鍵點(diǎn)創(chuàng)建描述符的最后一步。
從這里,我們可以使用關(guān)鍵點(diǎn)和描述符來進(jìn)行特征匹配。
現(xiàn)在讓我們設(shè)置android項(xiàng)目,
打開Android Studio->New Project->Empty Activity
下載 OpenCV 4.5.1
提取文件夾,然后將 java 文件夾重命名為 OpenCVLibrary451
然后使用 File->New->Import Module 并選擇文件夾
單擊完成。然后,你必須看到該庫已添加到你的項(xiàng)目中。
點(diǎn)擊 File->Project Structure->Dependencies 并選擇 app.
單擊添加依賴項(xiàng),然后選擇 OpenCVLibrary451
確保選中JDK 11,如果沒有,請轉(zhuǎn)到 gradle 設(shè)置并將目標(biāo)版本更改為1.8。
我們只需要再添加 JNI 庫,以便調(diào)用 SIFT OpenCV 本機(jī)函數(shù)。將以下內(nèi)容粘貼到應(yīng)用程序的構(gòu)建 gradle 文件中。android下defaultConfig下面
sourceSets{
main {
jniLibs.srcDirs = ['libs']
}
}
然后將幾個(gè)文件復(fù)制粘貼到你在開始時(shí)提取的 opencv 文件夾 [from /OpenCV-android-sdk/sdk/native/libs] 下。轉(zhuǎn)到項(xiàng)目的 app 文件夾,創(chuàng)建一個(gè)名為 libs 的文件夾并粘貼文件。
同樣,在應(yīng)用程序的主文件夾中創(chuàng)建一個(gè)名為 cpp 的文件夾,然后粘貼 /OpenCV-android-sdk/sdk/libcxx_h(yuǎn)elper 中的文件。你之前提取的那個(gè)。
在 android 下 app 的 build gradle 文件中粘貼以下內(nèi)容
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
同步 grade 文件。如果一切順利,你將看到應(yīng)用程序的構(gòu)建。
要測試應(yīng)用程序,請將 bmp 粘貼到可繪制對象中。我在這里使用了 used test.bmp。
收集 bmp 文件后,將以下內(nèi)容粘貼到 resources->layout->activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.a(chǎn)ndroid.com/apk/res/android"
xmlns:app="http://schemas.a(chǎn)ndroid.com/apk/res-auto"
xmlns:tools="http://schemas.a(chǎn)ndroid.com/tools"
android:layout_width="match_parent"
android:layout_h(yuǎn)eight="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_h(yuǎn)eight="wrap_content"
android:text="Hello OpenCV Android。。。
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_h(yuǎn)eight="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/sample_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.641"
app:srcCompat="@drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后,將以下代碼粘貼到 ActivityMain.kt 中
package com.a(chǎn)ugray.siftandroid
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.a(chǎn)ppcompat.a(chǎn)pp.AppCompatActivity
import org.opencv.a(chǎn)ndroid.Utils
import org.opencv.core.Mat
import org.opencv.core.MatOfKeyPoint
import org.opencv.features2d.Features2d
import org.opencv.features2d.SIFT
import org.opencv.imgproc.Imgproc
class MainActivity : AppCompatActivity() {
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
System.loadLibrary("opencv_java4")
}
}
private var imageView: ImageView? = null
// make bitmap from image resource
private var inputImage: Bitmap? = null
private val sift = SIFT.create()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.a(chǎn)ctivity_main)
inputImage = BitmapFactory.decodeResource(resources, R.drawable.test)
imageView = findViewById<View>(R.id.imageView) as ImageView
detectAndDrawKeypoints()
}
fun detectAndDrawKeypoints() {
val rgba = Mat()
Utils.bitmapToMat(inputImage, rgba)
val keyPoints = MatOfKeyPoint()
Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGBA2GRAY)
sift.detect(rgba, keyPoints)
Features2d.drawKeypoints(rgba, keyPoints, rgba)
Utils.matToBitmap(rgba, inputImage)
imageView。。畇etImageBitmap(inputImage)
}
}
讓我們看一下上面的一些代碼,以便更好地理解
System.loadLibrary("native-lib")
System.loadLibrary("opencv_java4")
當(dāng) cmake 為我們構(gòu)建所有類并準(zhǔn)備就緒時(shí),我們?nèi)匀粵]有 SIFT 模塊,很遺憾,它移到了新版本 OpenCV 中的其他庫中。
函數(shù) detectAndDrawKeypoints() 獲取位圖并將其轉(zhuǎn)換為圖像數(shù)組(矩陣/多維數(shù)組),并使用 SIFT 模塊檢測關(guān)鍵點(diǎn)。如果圖像具有良好的對比度、細(xì)節(jié)和較少重復(fù)的圖案,檢測將產(chǎn)生盡可能多的關(guān)鍵點(diǎn)。
構(gòu)建并運(yùn)行應(yīng)用程序
我們剛剛檢測到圖像中的特征。
我們現(xiàn)在可以擴(kuò)展它來拍攝另一張圖像,獲取它的關(guān)鍵點(diǎn)并最終匹配它們以獲得相似性。
原文標(biāo)題 : 使用 OpenCV for Android 進(jìn)行圖像特征檢測

發(fā)表評論
請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個(gè)字
最新活動(dòng)更多
-
即日-9.16點(diǎn)擊進(jìn)入 >> 【限時(shí)福利】TE 2025國際物聯(lián)網(wǎng)展·深圳站
-
10月23日立即報(bào)名>> Works With 開發(fā)者大會(huì)深圳站
-
10月24日立即參評>> 【評選】維科杯·OFweek 2025(第十屆)物聯(lián)網(wǎng)行業(yè)年度評選
-
11月27日立即報(bào)名>> 【工程師系列】汽車電子技術(shù)在線大會(huì)
-
12月18日立即報(bào)名>> 【線下會(huì)議】OFweek 2025(第十屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
-
精彩回顧立即查看>> 【限時(shí)下載】ADI中國三十周年感恩回饋助力企業(yè)升級!
推薦專題
- 1 阿里首位程序員,“掃地僧”多隆已離職
- 2 先進(jìn)算力新選擇 | 2025華為算力場景發(fā)布會(huì)暨北京xPN伙伴大會(huì)成功舉辦
- 3 宇樹機(jī)器人撞人事件的深度剖析:六維力傳感器如何成為人機(jī)安全的關(guān)鍵屏障
- 4 清華跑出具身智能獨(dú)角獸:給機(jī)器人安上眼睛和大腦,融資近20億
- 5 踢館大廠和微軟,剖析WPS靈犀的AI實(shí)用主義
- 6 特朗普要求英特爾首位華人 CEO 辭職
- 7 騰訊 Q2 財(cái)報(bào)亮眼:AI 已成第二增長曲線
- 8 AI版“四萬億刺激”計(jì)劃來了
- 9 谷歌吹響AI沖鋒號,AI還有哪些機(jī)會(huì)
- 10 蘋果把身家押在Siri上:一場輸不起的自我革命