使用OpenCV检测账单

时间:2018-05-14 07:11:11

标签: android opencv

是否有任何关于在an​​droid studio中使用openCv的新教程或最新教程。我想用它来创建我们的顶点项目。它是一个应用程序,应该使用其相机(或使用图像)检测票据并确定其价值。

我已经创建了一个应用,但现在我仍然坚持如何使用opencv,因为我以前没有任何使用它的经验而且我无法获得任何体面的教程。

这是我到目前为止所创造的。 (忽略评论,我和我的兄弟正试图编写这个,他说最好注释掉一些而不是删除它。)

activity_convert.xml

  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Convert">

    <org.opencv.android.JavaCameraView
        android:id="@+id/cView"
        android:layout_width="343dp"
        android:layout_height="303dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.52"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.043" />



</android.support.constraint.ConstraintLayout>

Convert.java

package com.example.carlo.thesis;

import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.List;

public class Convert extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {

    private static String TAG = "Convert";
    JavaCameraView jview;
    Mat mRgba, imgGray, imgCanny, mHierarchy;
    Scalar CONTOUR_COLOR = new Scalar(255,0,0,255);
    static {
        if(OpenCVLoader.initDebug()){
            Log.i(TAG, "OpenCV loaded.");
        }else{
            Log.i(TAG, "OpenCV not loaded.");
        }
    }
    BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status){
                case BaseLoaderCallback.SUCCESS:{
                    jview.enableView();

                    break;
                }
                default:{
                    super.onManagerConnected(status);
                    break;
                }
            }

        }
    };

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

        jview = findViewById(R.id.cView);

        jview.setVisibility(SurfaceView.VISIBLE);
        jview.setCvCameraViewListener(this);


    }

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

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

    @Override
    protected void onResume(){
        super.onResume();
            if (OpenCVLoader.initDebug()){
                Log.i(TAG, "OpenCV loaded");
                mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
            }else{
                Log.i(TAG, "OpenCV not loaded");
                OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallBack);
            }

    }

    @Override
    public void onCameraViewStarted(int width, int height) {
        mHierarchy = new Mat();


        mRgba = new Mat(height, width, CvType.CV_8UC4);
        imgGray = new Mat(height, width, CvType.CV_8UC1);
        imgCanny = new Mat(height, width, CvType.CV_8UC1);
    }

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

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

       // List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

        mRgba = inputFrame.rgba();
        final Mat dst = new Mat(mRgba.rows(), mRgba.cols(), mRgba.type());
        mRgba.copyTo(dst);

        Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY);

        final List<MatOfPoint> points = new ArrayList<>();
        final Mat hierarchy = new Mat();

        Imgproc.findContours(dst, points, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

        Imgproc.cvtColor(dst, dst, Imgproc.COLOR_GRAY2BGR);
       /* Imgproc.Canny(mRgba, imgCanny, 50, 100);
        //Imgproc.cvtColor(imgCanny, imgGray, Imgproc.COLOR_RGB2GRAY);

        Imgproc.findContours(imgCanny, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

        Imgproc.drawContours(imgCanny, contours, -1, CONTOUR_COLOR);


        //Imgproc.findContours(mRgba, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);




        return contours.get(1);

        //return imgCanny;*/

        Mat mRgbaT = mRgba.t();
        Core.flip(mRgba.t(), mRgbaT, 1);
        Imgproc.resize(mRgbaT, mRgbaT, mRgba.size());

        return mRgbaT;
    }


}

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您可以在此处找到GitHub上的OpenCV Android演示:https://github.com/opencv/opencv/tree/master/samples/android

Android的OpenCV教程,它有Eclipse步骤。 https://docs.opencv.org/3.4/d5/df8/tutorial_dev_with_OCV_on_Android.html

您还可以在互联网上搜索OpenCV Android视频,它可以帮助您快速学习。