android opencv是啥,android OpenCv 项目创建

OpenCv 介绍

OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效--由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

opencv android版本下载地址:

android版本的下载地址

https://sourceforge.net/projects/opencvlibrary/files/opencv-android/

8dc312a41a41

图1

andoid项目中引入opencv

系统:Windows10;开发环境 android studio3.1.2 ; opencv 版本:opencv-3.4.1-android-sdk

第一步创建应用程序

8dc312a41a41

图2

第二步 创建Module,包名为org.opencv,使用这个包名和后面拷贝的java文件一致,否则需要修改java里面的引入的包;

8dc312a41a41

图3

第三步 拷贝前面下载的opencvsdk中的java文件到自己创建的module中,文件需要和sdk中的一致。

8dc312a41a41

图4

第四步构建项目会出现下面的错误,是因为aidl文件不能正常识别。没法生成对应的java文件。所以需要在创建的module中的src下面新建aidl文件夹,把原来的aidl文件移到对应的目录下参照图6

8dc312a41a41

图5

8dc312a41a41

图6

第五步重新构建系统,发现还有错误,如图7;拷贝opencv-3.4.1-android-sdk\OpenCV-android-sdk\sdk\java\res\values\attrs.xml 文件到module中的 values文件夹,参照图8,重新构建项目,项目显示正常。

8dc312a41a41

图7

8dc312a41a41

图8

第六步移动 opencv-3.4.1-android-sdk\OpenCV-android-sdk\sdk\native\libs\armeabi 文件到项目中的libs目录下,参照图9;

8dc312a41a41

图9

通过上面的步骤,opencv依赖的项目就创建成功了 。添加module到app项目中如图10

8dc312a41a41

图10

下面为验证opecv是否添加成功,利用opencv辨别图片的清晰度:

布局文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="切换图片"

android:id="@+id/changeImage"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="图片清晰度展示"

android:id="@+id/tips_tv"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="原图"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/show_iv"

/>

java代码:

package com.apkbj.www.opencvone;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

import org.opencv.android.InstallCallbackInterface;

import org.opencv.android.LoaderCallbackInterface;

import org.opencv.android.OpenCVLoader;

import org.opencv.android.Utils;

import org.opencv.core.CvType;

import org.opencv.core.Mat;

import org.opencv.imgproc.Imgproc;

import java.io.IOException;

import java.io.InputStream;

public class MainActivityextends AppCompatActivity {

static {

System.loadLibrary("opencv_java3");

}

/**

* 文件路径

*/

private Stringid ="qinchu.png";

/**

* 图片清晰度展示

*/

private TextViewtips_tv;

/**

* 待计算的图片

*/

private ImageViewshow_iv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setTitle("opencv项目搭建-- 图像模糊识别");

show_iv = (ImageView) findViewById(R.id.show_iv);

tips_tv = (TextView) findViewById(R.id.tips_tv);

findViewById(R.id.changeImage).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (id.equals("qinchu.png")) {

checkImage("muhu.png");

}else {

checkImage("qinchu.png");

}

}

});

}

/**

* 图片清晰度检验

*/

private void checkImage(String mipmap) {

id = mipmap;

InputStream is =null;

try {

is =this.getResources().getAssets().open(mipmap);

Bitmap bitmap = BitmapFactory.decodeStream(is);

show_iv.setImageBitmap(bitmap);

Boolean blur = isBlurByOpenCV(bitmap);

if (blur) {

tips_tv.setText("这张图片是模糊的");

}else {

tips_tv.setText("这张图片是清楚的");

}

}catch (IOException e) {

e.printStackTrace();

}

}

private boolean isBlurByOpenCV(Bitmap image) {

System.out.println("image.w=" + image.getWidth() +",image.h=" + image.getHeight());

int l = CvType.CV_8UC1;//8-bit grey scale image

Mat matImage =new Mat();

Utils.bitmapToMat(image, matImage);

Mat matImageGrey =new Mat();

Imgproc.cvtColor(matImage, matImageGrey, Imgproc.COLOR_BGR2GRAY);// 图像灰度化

Bitmap destImage;

destImage = Bitmap.createBitmap(image);

Mat dst2 =new Mat();

Utils.bitmapToMat(destImage, dst2);

Mat laplacianImage =new Mat();

dst2.convertTo(laplacianImage, l);

Imgproc.Laplacian(matImageGrey, laplacianImage, CvType.CV_8U);// 拉普拉斯变换

Mat laplacianImage8bit =new Mat();

laplacianImage.convertTo(laplacianImage8bit, l);

Bitmap bmp = Bitmap.createBitmap(laplacianImage8bit.cols(), laplacianImage8bit.rows(), Bitmap.Config.ARGB_8888);

Utils.matToBitmap(laplacianImage8bit, bmp);

int[] pixels =new int[bmp.getHeight() * bmp.getWidth()];

bmp.getPixels(pixels,0, bmp.getWidth(),0,0, bmp.getWidth(), bmp.getHeight());// bmp为轮廓图

int maxLap = -16777216;// 16m

for (int pixel : pixels) {

if (pixel == -1) {

continue;

}

if (pixel > maxLap)

maxLap = pixel;

}

int userOffset =3881250;// 界线(严格性)降低一点

int soglia = -6118750 + userOffset;// -6118750为广泛使用的经验值

System.out.println("maxLap=" + maxLap);

if (maxLap <= soglia) {

System.out.println("这是一张模糊图片");

}

System.out.println("==============================================\n");

soglia +=6118750 + userOffset;

maxLap +=6118750 + userOffset;

return maxLap <= soglia;

}

}

待检验的两张图11、图12:

8dc312a41a41

图11

8dc312a41a41

图12

最后一张gif图,程序运行结果:

8dc312a41a41

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值