Android使用OpenCV

读大学时报了一个课程叫《动目标检测》,那时候大受启发,原来摄像头可以这么玩的,通过摄像头捕捉图像,经过图像处理和分析找出运动目标。上了那个课程,简直就是打开了视野,后来做了一些简单的体感游戏,基于两帧相减得出运动的像素,通过运动的像素分析出玩家的肢体运动,从而操控游戏。当时水平有限,还没深入了解光流之类的算法,但已经了解到图像处理的高深莫测,是未来发展的很好的方向。摄像头是机器人的眼睛,开发摄像头的玩法就是在开发机器人的眼睛,赋予机器人超强的视觉能力。

图像处理有各种各样的算法,为了避免开发者重复发明轮子和提高算法运行效率,一般需要用第三方的库,比如OpenCV。OpenCV是一个实现了很多图像处理算法的库,方便我们高效实现图像处理。这个库是开源的库,而且还在不断的更新维护,近几年陆陆续续新增了很多与时俱进的算法包括ML、AR、SLAM。

下面讲述如何在Android平台上面调用OpenCV库。

OpenCV各个平台和版本的下载地址:http://wiki.opencv.org.cn/index.php/Download

解压Android版本的库后,有下面几个文件目录:

LICENSE     
README.android  
apk     
samples     
sdk

Android平台使用OpenCV有多种方式:
1、使用JAVA版本

  • &quot;OpenCV-android-sdk/sdk/java&quot;</code>作为Model导入到AndroidStudio的工程里面,并引用
  • &quot;OpenCV-android-sdk/sdk/native/libs&quot;</code>拷贝到工程目录libs里,可根据需要的平台拷贝,并引用

完成上面两步即可使用OpenCV

测试代码如下(自行添加调用摄像头的权限):

public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2{

    private JavaCameraView cameraView;
    private Mat rgba;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cameraView = findViewById(R.id.cameraView);
        cameraView.setCvCameraViewListener(this);
    }

    @Override
    public void onCameraViewStarted(int width, int height) {
        rgba = new Mat(width, height, CvType.CV_8UC4);
    }

    @Override
    public void onCameraViewStopped() {
        rgba.release();
    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        rgba = inputFrame.rgba();
        return rgba;
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
        } else {
            cameraView.enableView();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (cameraView != null) {
            cameraView.disableView();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (cameraView != null) {
            cameraView.disableView();
        }
    }
}

layout文件加上:

 <org.opencv.android.JavaCameraView
        android:id="@+id/cameraView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:camera_id="back"
    />

2、使用native版本

#####################################################

附上Mac上配置OpenCv,并使用cmake编译c++项目的教程链接
https://blog.csdn.net/v_xchen_v/article/details/70161673

24 评论

  1. May I simply just say what a relief to find a person that actually understands what they’re discussing on the net. You certainly know how to bring a problem to light and make it important. A lot more people have to look at this and understand this side of the story. I can’t believe you’re not more popular given that you certainly possess the gift.

    http:///www.manulescu.com – manu manumanu

  2. This is a truly riveting website I must say.

    Too polluted that there are not too various websites like This anymore. But I am exhilarated that I bring about it. Meet scribble literary works on it .

    You can see my website here as well: xbody bucuresti

  3. First of all I want to say awesome blog! I had a quick
    question in which I’d like to ask if you do not mind.
    I was curious to find out how you center yourself and clear your head before writing.

    I have had a tough time clearing my mind
    in getting my thoughts out there. I truly do take pleasure in writing but
    it just seems like the first 10 to 15 minutes are wasted just trying to figure out how to begin. Any recommendations or tips?
    Thank you!

发表评论

电子邮件地址不会被公开。