溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在android中使用openCV檢測車牌

發(fā)布時間:2021-03-25 17:17:29 來源:億速云 閱讀:190 作者:Leah 欄目:移動開發(fā)

本篇文章給大家分享的是有關(guān)怎么在android中使用openCV檢測車牌,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1、導(dǎo)入module

先從官網(wǎng)下載openCVForAndroid的sdk,以3.2.0版本為例,找到依賴庫路徑,然后導(dǎo)入module。

2、導(dǎo)入動態(tài)與靜態(tài)庫

在sdk里面找到lib目錄,把所有的.a和.so文件拷貝到項(xiàng)目的libs對應(yīng)ABI路徑下:

怎么在android中使用openCV檢測車牌

3、配置gradle

將依賴的靜態(tài)庫編譯到native-libs里面:

task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
 destinationDir file("$buildDir/native-libs")
 baseName 'native-libs'
 from fileTree(dir: 'libs', include: '**/*.so')
 into 'lib/'
}
tasks.withType(JavaCompile) {
 compileTask -> compileTask.dependsOn(nativeLibsToJar)
}
 
dependencies {
 compile fileTree(include: ['*.jar'], dir: 'libs')
 compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
 ......
}

 好了,經(jīng)過配置三步曲,我們就可以愉快地使用openCV了。

------------------------中場休息---------------------------

接下來是調(diào)用三步曲:加載openCV、初始化車牌檢測器和執(zhí)行車牌檢測

1、加載openCV

調(diào)用openCVLoader去加載,如果加載成功進(jìn)行下一步操作:

private void initOpenCV(){
 boolean result = OpenCVLoader.initDebug();
 if(result){
  Log.i(TAG, "initOpenCV success...");
  //初始化車牌檢測器
  mPlateDetector = new ObjectDetector(this, R.raw.haarcascade_license_plate,
   3, new Scalar(255, 0, 0, 0));
  mObject = new MatOfRect();
 }else {
  Log.e(TAG, "initOpenCV fail...");
 }
 }

2、初始化檢測器

使用車牌檢測的級聯(lián)分類xml文件進(jìn)行初始化:

/**
 * 創(chuàng)建級聯(lián)分類器
 * @param context 上下文
 * @param id 級聯(lián)分類器ID
 * @return 級聯(lián)分類器
 */
 private CascadeClassifier createDetector(Context context, int id) {
 CascadeClassifier javaDetector;
 InputStream is = null;
 FileOutputStream os = null;
 try {
  is = context.getResources().openRawResource(id);
  File cascadeDir = context.getDir(LICENSE_PLATE_MODEL, Context.MODE_PRIVATE);
  File cascadeFile = new File(cascadeDir, id + ".xml");
  os = new FileOutputStream(cascadeFile);
 
  byte[] buffer = new byte[4096];
  int bytesRead;
  while ((bytesRead = is.read(buffer)) != -1) {
  os.write(buffer, 0, bytesRead);
  }
 
  javaDetector = new CascadeClassifier(cascadeFile.getAbsolutePath());
  if (javaDetector.empty()) {
  javaDetector = null;
  }
 
  boolean delete = cascadeDir.delete();
  Log.i("ObjectDetector", "deleteResult=" + delete);
  return javaDetector;
 } catch (IOException e) {
  e.printStackTrace();
  return null;
 } finally {
  try {
  if (null != is) {
   is.close();
  }
  if (null != os) {
   os.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }

3、執(zhí)行車牌檢測

由于openCV操作對象是Mat,所以我們得把Bitmap轉(zhuǎn)成Mat,然后轉(zhuǎn)成Gray灰度圖去進(jìn)行檢測:

/**
 * 執(zhí)行車牌檢測
 * @param bitmap bitmap
 * @return 車牌檢測后的bitmap
 */
 private Bitmap doPlateDetecting(Bitmap bitmap){
 if(mPlateDetector != null && bitmap != null){
  Mat mRgba = new Mat();
  Mat mGray = new Mat();
  //bitmap轉(zhuǎn)成map
  Utils.bitmapToMat(bitmap, mRgba);
  //rgba轉(zhuǎn)成灰度圖
  Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);
  // 檢測車牌
  Rect[] object = mPlateDetector.detectObject(mGray, mObject);
  if(object != null && object.length > 0){
  //檢測到車牌區(qū)域
  Rect rect = object[0];
  //矩形標(biāo)識
  Imgproc.rectangle(mRgba, rect.tl(), rect.br(), mPlateDetector.getRectColor(), 3);
  }
  //mat轉(zhuǎn)回bitmap
  Utils.matToBitmap(mRgba, bitmap);
 }
 return bitmap;
 }

其中,detectObject方法體是調(diào)用cascadeClassifier的detectMultiScale來完成檢測的:

public Rect[] detectObject(Mat gray, MatOfRect object) {
 mCascadeClassifier.detectMultiScale(
  gray, // 要檢查的灰度圖像
  object, // 檢測到的車牌
  1.1, // 表示在前后兩次相繼的掃描中,搜索窗口的比例系數(shù)
  mMinNeighbors, // 默認(rèn)是3
  Objdetect.CASCADE_SCALE_IMAGE,
  getSize(gray, 80), // 檢測目標(biāo)最小值
  getSize(gray, 800)); // 檢測目標(biāo)最大值
 
 return object.toArray();
 }

以上就是怎么在android中使用openCV檢測車牌,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI