使用 OpenCV for Android 進(jìn)行圖像特征檢測(cè)
android 開發(fā)人員,可能熟悉使用activities, fragments, intents以及最重要的一系列開源依賴庫(kù)。但是,注入需要本機(jī)功能的依賴關(guān)系(如計(jì)算機(jī)視覺框架)并不像在 gradle 文件中直接添加實(shí)現(xiàn)語(yǔ)句那樣簡(jiǎn)單!
今天,將專注于使用 OpenCV 庫(kù),將其作為依賴項(xiàng)注入到你的 android 應(yīng)用程序中。
那么,我們將從這次討論中得到什么?我們將能夠創(chuàng)建一個(gè) android 應(yīng)用程序并執(zhí)行所需的步驟來(lái)集成 OpenCV。
此外,我們將完成一個(gè)基于SIFT技術(shù)的圖像特征檢測(cè)算法。這將是你考慮構(gòu)建自己的 SDK 的良好起點(diǎn)。
什么是SIFT?
SIFT 代表尺度不變傅立葉變換(Scale Invariant Fourier Transform)。檢測(cè)器用于查找圖像上的興趣點(diǎn)。它使我們能夠識(shí)別圖像中的局部特征。
SIFT 的優(yōu)勢(shì)在于,即使我們大幅縮放圖像,它也能正常工作,因?yàn)樗鼘D像數(shù)據(jù)轉(zhuǎn)換為尺度不變坐標(biāo)。SIFT 使用“關(guān)鍵點(diǎn)”來(lái)表示圖像中縮放和旋轉(zhuǎn)不變的局部特征。這是我們將其用于各種應(yīng)用程序(如圖像匹配、對(duì)象檢測(cè)、場(chǎng)景檢測(cè)等)的基準(zhǔn)。
為了識(shí)別關(guān)鍵點(diǎn),該算法為你完成:
第 1 步:形成尺度空間——這一步確保特征與尺度無(wú)關(guān)。
第 2 步:關(guān)鍵點(diǎn)定位——這一步有助于識(shí)別合適的特征/關(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)和描述符來(lái)進(jìn)行特征匹配。
現(xiàn)在讓我們?cè)O(shè)置android項(xiàng)目,
打開Android Studio->New Project->Empty Activity
下載 OpenCV 4.5.1
提取文件夾,然后將 java 文件夾重命名為 OpenCVLibrary451
然后使用 File->New->Import Module 并選擇文件夾
單擊完成。然后,你必須看到該庫(kù)已添加到你的項(xiàng)目中。
點(diǎn)擊 File->Project Structure->Dependencies 并選擇 app.
單擊添加依賴項(xiàng),然后選擇 OpenCVLibrary451
確保選中JDK 11,如果沒有,請(qǐng)轉(zhuǎn)到 gradle 設(shè)置并將目標(biāo)版本更改為1.8。
我們只需要再添加 JNI 庫(kù),以便調(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)建。
要測(cè)試應(yīng)用程序,請(qǐng)將 bmp 粘貼到可繪制對(duì)象中。我在這里使用了 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 中的其他庫(kù)中。
函數(shù) detectAndDrawKeypoints() 獲取位圖并將其轉(zhuǎn)換為圖像數(shù)組(矩陣/多維數(shù)組),并使用 SIFT 模塊檢測(cè)關(guān)鍵點(diǎn)。如果圖像具有良好的對(duì)比度、細(xì)節(jié)和較少重復(fù)的圖案,檢測(cè)將產(chǎn)生盡可能多的關(guān)鍵點(diǎn)。
構(gòu)建并運(yùn)行應(yīng)用程序
我們剛剛檢測(cè)到圖像中的特征。
我們現(xiàn)在可以擴(kuò)展它來(lái)拍攝另一張圖像,獲取它的關(guān)鍵點(diǎn)并最終匹配它們以獲得相似性。
原文標(biāo)題 : 使用 OpenCV for Android 進(jìn)行圖像特征檢測(cè)

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
7月8日立即報(bào)名>> 【在線會(huì)議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身智能機(jī)器人產(chǎn)業(yè)技術(shù)創(chuàng)新應(yīng)用論壇
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
-
即日-2025.8.1立即下載>> 《2024智能制造產(chǎn)業(yè)高端化、智能化、綠色化發(fā)展藍(lán)皮書》
推薦專題
- 1 AI 眼鏡讓百萬(wàn) APP「集體失業(yè)」?
- 2 豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
- 3 一文看懂視覺語(yǔ)言動(dòng)作模型(VLA)及其應(yīng)用
- 4 “支付+”時(shí)代,支付即生態(tài) | 2025中國(guó)跨境支付十大趨勢(shì)
- 5 中國(guó)最具實(shí)力AI公司TOP10
- 6 特斯拉Robotaxi上路,馬斯克端上畫了十年的餅
- 7 張勇等人退出阿里合伙人
- 8 AI的夏天:第四范式VS云從科技VS地平線機(jī)器人
- 9 深圳跑出40億超級(jí)隱形冠軍:賣機(jī)器人年入6.1億,港股上市
- 10 “AI六小虎”到了下一個(gè)賽點(diǎn)