Motion detect using opencv on android

asked 2014-06-10 23:34:45 -0500

fontain gravatar image

updated 2014-06-10 23:37:04 -0500

Hi I'm trying to implementing motion detect using opencv on android. I applied sample motion detection code and made jni call. I ran this on android, it works but processed preview is super slow. I removed motion detection process part then preview worked normally. How can I improve the the performance? I added my source code for motion detect and jni. please check and advice me.

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

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
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.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;


public class CameraViewActivity extends Activity implements
CvCameraViewListener2 {

    private CameraBridgeViewBase m_OpenCvCameraView;
    private Mat m_inputMat;
    private Mat m_dstMat;
    private Mat m_motionAvgMat;
    private Mat m_grayMat;
    private Mat m_diffMat;
    private Mat m_scaleMat;
    private static Mat m_contourKernel;
    private static Mat m_hierarchy;
    private Point m_rectPoint1;
    private Point m_rectPoint2;
    private static Point m_contourPoint;
    private Scalar m_rectColor;
    private Size m_kSize;
    private Size m_frameSize;
    private double m_totalPixels;
    private double m_motionPercent;

    private boolean isRecording = false;
    private boolean isMotionDetect = false;

    private Camera m_Camera;
    private MediaRecorder m_MediaRecorder;
    private Runnable m_Runnable;
    private Handler m_TimerHandler;
    private TextView m_tvTimer;
    private int value = 0;
    private CountDownTimer timer;

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                System.loadLibrary("motion_detect");
                m_OpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
            }
        }
    };

    public CameraViewActivity() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.local_camera_view);
        m_OpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.local_camera_surface_view);
        m_OpenCvCameraView.setCvCameraViewListener(this);
        m_tvTimer = (TextView)findViewById(R.id.tv_timer);
    }


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

    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this, mLoaderCallback);
    }

    public void onDestroy() {
        super.onDestroy();
        if (m_OpenCvCameraView != null)
            m_OpenCvCameraView.disableView();
        m_inputMat.release();
        m_dstMat.release();
        m_motionAvgMat.release();
        m_grayMat.release();
        m_diffMat.release();
        m_scaleMat.release();
        m_contourKernel.release();
        m_hierarchy.release();
    }

    public void onCameraViewStarted(int width, int height) {
        m_inputMat = new Mat();
        m_dstMat = new Mat();
        m_motionAvgMat = null;
        m_grayMat = new Mat();
        m_diffMat = new Mat();
        m_scaleMat = new Mat();
        m_rectPoint1 = new Point();
        m_rectPoint2 = new Point();
        m_rectColor = new Scalar(0, 255, 0);
        m_kSize = new Size(8, 8);
        m_motionPercent = 0.0;
        m_contourKernel = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(3, 3), new Point(1, 1));
        m_hierarchy = new Mat();
        m_contourPoint = new Point(-1, -1);
    }

    public void onCameraViewStopped() {
        m_inputMat.release();
        m_dstMat ...
(more)
edit retag flag offensive close merge delete