使用 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)建一個 android 應(yīng)用程序并執(zhí)行所需的步驟來集成 OpenCV。
此外,我們將完成一個基于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)描述符——這是為每個關(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']
}
}
然后將幾個文件復(fù)制粘貼到你在開始時提取的 opencv 文件夾 [from /OpenCV-android-sdk/sdk/native/libs] 下。轉(zhuǎn)到項(xiàng)目的 app 文件夾,創(chuàng)建一個名為 libs 的文件夾并粘貼文件。

同樣,在應(yīng)用程序的主文件夾中創(chuàng)建一個名為 cpp 的文件夾,然后粘貼 /OpenCV-android-sdk/sdk/libcxx_h(yuǎn)elper 中的文件。你之前提取的那個。

在 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!!.setImageBitmap(inputImage)
}
}
讓我們看一下上面的一些代碼,以便更好地理解
System.loadLibrary("native-lib")
System.loadLibrary("opencv_java4")
當(dāng) cmake 為我們構(gòu)建所有類并準(zhǔn)備就緒時,我們?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個字
最新活動更多
-
11月7日立即參評>> 【評選】維科杯·OFweek 2025(第十屆)物聯(lián)網(wǎng)行業(yè)年度評選
-
11月20日立即報名>> 【免費(fèi)下載】RISC-V芯片發(fā)展現(xiàn)狀與測試挑戰(zhàn)-白皮書
-
即日-11.25立即下載>>> 費(fèi)斯托白皮書《柔性:汽車生產(chǎn)未來的關(guān)鍵》
-
11月27日立即報名>> 【工程師系列】汽車電子技術(shù)在線大會
-
11月28日立即下載>> 【白皮書】精準(zhǔn)洞察 無線掌控——283FC智能自檢萬用表
-
12月18日立即報名>> 【線下會議】OFweek 2025(第十屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會
推薦專題
- 1 特斯拉工人被故障機(jī)器人打成重傷,索賠3.6億
- 2 AI 時代,阿里云想當(dāng)“安卓” ,那誰是“蘋果”?
- 3 拐點(diǎn)已至!匯川領(lǐng)跑工控、埃斯頓份額第一、新時達(dá)海爾賦能扭虧為盈
- 4 L3自動駕駛延期,逼出車企技術(shù)自我淘汰
- 5 隱退4年后,張一鳴久違現(xiàn)身!互聯(lián)網(wǎng)大佬正集體殺回
- 6 機(jī)器人9月大事件|3家國產(chǎn)機(jī)器人沖刺IPO,行業(yè)交付與融資再創(chuàng)新高!
- 7 谷歌“香蕉”爆火啟示:國產(chǎn)垂類AI的危機(jī)還是轉(zhuǎn)機(jī)?
- 8 7倍機(jī)器人大牛股:高管股東套現(xiàn)VS機(jī)構(gòu)兇猛抱團(tuán),該信誰?
- 9 八部門聯(lián)手放行L3自動駕駛!巨頭開始拼搶萬億市場?
- 10 OpenAI發(fā)布的AI瀏覽器,市場為何反應(yīng)強(qiáng)烈?


分享













