怎么在android中使用openCV检测车牌

  介绍

本篇文章给大家分享的是有关怎么在android中使用openCV检测车牌,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

<强> 1,导入模块

先从官网下载openCVForAndroid的sdk,以3.2.0版本为例,找到依赖库路径,然后导入模块。

<强> 2,导入动态与静态库

在sdk里面找到自由目录,把所有的。和。所以文件拷贝到项目的填词对应ABI路径下:

怎么在android中使用openCV检测车”牌> </p> <p> <强> 3,配置gradle </强> </p> <p>将依赖的静态库编译到native-libs里面:</p> <pre类= task  nativeLibsToJar(类型:,,,描述:,& # 39;create  a  jar  archive  of 从而native 填词# 39;),{   ,destinationDir 文件(“$ buildDir/native-libs")   ,baseName  & # 39; native-libs& # 39;   ,得到fileTree (dir: & # 39;填词# 39;,,包括:,& # 39;* */* . so # 39;)   ,into  & # 39; lib/& # 39;   }   tasks.withType (JavaCompile), {   ,compileTask →compileTask.dependsOn (nativeLibsToJar)   }   ,   dependencies  {   ,compile  fileTree(包括:,(& # 39;* . jar # 39;],, dir:, & # 39;填词# 39;)   ,compile  fileTree (dir:,“美元buildDir/native-libs",,包括:,& # 39;native-libs.jar& # 39;)   ,……   }

,好了,经过配置三步曲,我们就可以愉快地使用openCV了。

- - - - - - - - - - - - - - - - - - - - - - - -中场休息- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

接下来是调用三步曲:加载openCV,初始化车牌检测器和执行车牌检测

1,加载openCV

调用openCVLoader去加载,如果加载成功进行下一步操作:

private  void  initOpenCV () {   ,boolean  result =, OpenCVLoader.initDebug ();   ,如果(结果){   Log.i才能(标签,,“initOpenCV 成功…“);//才能初始化车牌检测器   mPlateDetector 才能=,new  ObjectDetector (R.raw.haarcascade_license_plate,这个,   ,,3,,new 标量(255,0,0,0));   时间=mObject 才能;new  MatOfRect ();   ,}else  {   Log.e才能(标签,,“initOpenCV 失败…“);   ,}   以前,}

2,初始化检测器

使用车牌检测的级联分类xml文件进行初始化:

/* *   ,*创建级联分类器   ,* @param  context 上下文   ,* @param  id 级联分类器ID   ,* @return 级联分类器   ,*/,private  CascadeClassifier  createDetector (Context 上下文,int  id), {   ,CascadeClassifier  javaDetector;   ,InputStream  is =,空;   ,FileOutputStream  os =,空;   ,try  {   时间=is 才能;context.getResources () .openRawResource (id);   File 才能;cascadeDir =, context.getDir (Context.MODE_PRIVATE LICENSE_PLATE_MODEL也);   File 才能;cascadeFile =, new 文件(cascadeDir, id  +,“.xml");   时间=os 才能;new  FileOutputStream (cascadeFile);   ,   ,,byte [], buffer =, new 字节[4096];   int 才能;bytesRead;   while 才能;((bytesRead =, is.read(缓冲)),!=,1),{   os.write才能(缓冲,0,,bytesRead);   ,,}   ,   时间=javaDetector 才能;new  CascadeClassifier (cascadeFile.getAbsolutePath ());   if 才能;(javaDetector.empty ()), {   时间=javaDetector 才能;零;   ,,}   ,   boolean 才能;delete =, cascadeDir.delete ();   Log.i才能(“ObjectDetector",,“deleteResult=? +,删除);   return 才能;javaDetector;   ,}catch  (IOException  e), {   e.printStackTrace才能();   return 才能;零;   ,}finally  {   try {才能   if 才能;(null  !=,), {   ,,is.close ();   ,,}   if 才能;(null  !=, os), {   ,,os.close ();   ,,}   ,,},catch  (IOException  e), {   e.printStackTrace才能();   ,,}   ,}   以前,}

3,执行车牌检测

由于openCV操作对象是垫,所以我们得把位图转成垫,然后转成灰色灰度图去进行检测:

/* *   ,*执行车牌检测   ,* @param  bitmap 位图   ,* @return 车牌检测后的位图   ,*/,private  Bitmap  doPlateDetecting (Bitmap 位图){   ,如果(mPlateDetector  !=, null ,,, bitmap  !=, null) {   Mat 才能;mRgba =, new 垫();   Mat 才能;mGray =, new 垫();//位图转才能成地图   Utils.bitmapToMat才能(位图,mRgba);//rgba才能转成灰度图   Imgproc.cvtColor才能(mRgba, mGray,, Imgproc.COLOR_RGBA2GRAY);//才能,检测车牌   矩形[],才能object =, mPlateDetector.detectObject (mObject mGray也);   如果才能(object  !=, null ,,, object.length 祝辞,0){//检才能测到车牌区域   Rect 才能;Rect =,对象[0];//矩才能形标识   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在android中使用openCV检测车牌