訂閱
糾錯(cuò)
加入自媒體

使用 OpenCV for Android 進(jìn)行圖像特征檢測(cè)

android 開(kāi)發(fā)人員,可能熟悉使用activities, fragments, intents以及最重要的一系列開(kāi)源依賴庫(kù)。但是,注入需要本機(jī)功能的依賴關(guān)系(如計(jì)算機(jī)視覺(jué)框架)并不像在 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)目,

打開(kāi)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,如果沒(méi)有,請(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ù)制粘貼到你在開(kāi)始時(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)匀粵](mé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è)

聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wèn)題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無(wú)評(píng)論

暫無(wú)評(píng)論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號(hào)
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯(cuò)
x
*文字標(biāo)題:
*糾錯(cuò)內(nèi)容:
聯(lián)系郵箱:
*驗(yàn) 證 碼:

粵公網(wǎng)安備 44030502002758號(hào)