估算视频中对象的尺寸并将其显示为叠加层

时间:2016-04-21 07:27:39

标签: android opencv image-processing android-studio-2.0

我正在尝试开发一个应用程序来测量视频中对象的尺寸,并将它们叠加在同一视频中(例如,八和宽)。与此YouTube video中的内容类似:

enter image description here

我正在使用的代码:

> Andoid MAin Activity

    package com.example.nimesh.opencv;

    import android.hardware.Camera;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.Window;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.view.Menu;
    import android.view.MenuItem;
    import org.opencv.android.CameraBridgeViewBase;
    import org.opencv.android.LoaderCallbackInterface;
    import org.opencv.android.OpenCVLoader;
    import org.opencv.android.BaseLoaderCallback;
    import org.opencv.android.JavaCameraView;
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.imgproc.Imgproc;
    import android.hardware.Camera;
    import android.hardware.Camera.PictureCallback;
    import android.hardware.Camera.Size;

    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2,View.OnTouchListener{

        private static final String TAG = "MainActivity";
        private MenuItem               mItemPreviewRGBA;
        //private MenuItem               mItemPreviewGray;
        //private MenuItem               mItemPreviewCanny;
        private MenuItem                mItemPreviewFeatures;
        private MenuItem                mItemPreviewComputeMetric;
        private File mCascadeFile;
    //    private camSet mOpenCvCameraView;
        private CameraBridgeViewBase mJavaCameraView;

        private BaseLoaderCallback mBaseLoaderCallback=new BaseLoaderCallback(this) {
            @Override
            public void onManagerConnected(int status) {
                switch (status){
                    case LoaderCallbackInterface.SUCCESS:
                    {
                        Log.i(TAG,"OpencvLoader Successfull");
                        System.loadLibrary("mixed_sample");
                        mJavaCameraView.enableView();
                        mJavaCameraView.setOnTouchListener(MainActivity.this);
                        break;
                    }
                    default:
                    {
                        super.onManagerConnected(status);
                    }
                }
            }
        };

        static {
            if(!OpenCVLoader.initDebug()){
                Log.d(TAG, "OpenCV not loaded");
            } else {
                Log.d(TAG, "OpenCV loaded");
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            Log.i(TAG, "called onCreateOptionsMenu");
            mItemPreviewRGBA = menu.add("Preview RGBA");
            // mItemPreviewGray = menu.add("Preview GRAY");
            // mItemPreviewCanny = menu.add("Canny");
            mItemPreviewComputeMetric = menu.add("Compute metric");
            mItemPreviewFeatures = menu.add("Find features");

            return true;
        }

        public void onresume()
        {

            super.onresume();
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0/>,this,mBaseLoaderCallback);
        }

        public void onDestroy()
        {
            super.onDestroy();
            if (mJavaCameraView!=null)
            {
                mJavaCameraView.disableView();
            }
        }

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

            mJavaCameraView=(JavaCameraView)findViewById(R.id.MainActivityCameraView);
            mJavaCameraView.setVisibility(SurfaceView.VISIBLE);
            mJavaCameraView.setCvCameraViewListener(this);
        }
        Mat mRgba,mRgbaF,mRgbaT;

        @Override
        public void onCameraViewStarted(int width, int height) {
            mRgba = new Mat(height, width, CvType.CV_8UC4);
            mRgbaF = new Mat(height, width, CvType.CV_8UC4);
            mRgbaT = new Mat(width, width, CvType.CV_8UC4);  // NOTE width,width is NOT a typo

        }

        @Override
        public void onCameraViewStopped() {
        }

        @Override
        public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
            mRgba = inputFrame.rgba();
            return mRgba;
        }

        private Mat                    pTs;


        @Override
        public boolean onTouch(View v, MotionEvent event) {

            TextView txt=(TextView)findViewById(R.id.textView2);
            Log.i(TAG,"onTouch event");
            Log.i(TAG,"Here");
            float x=event.getX();
            float y=event.getY();

            txt.setText("X:"+Float.toString(x)+'-'+"Y:"+Float.toString(y));
            System.out.println(event.getX());
            System.out.println(event.getY());
            Mat a = new Mat(1, 2, CvType.CV_64F); // A matrix with 1 row and 3 columns
            a.put(0, 0, event.getX()); // Set row 1 , column 1
            a.put(0, 1, event.getY()); // Set row 1 , column 2

            System.out.println("ok1");
            if (pTs.empty()){
                pTs=a.clone();
            }else{
                pTs.push_back(a);
            }
            System.out.println("ok2");
            if(pTs.rows()<=2)
               // drawPts(pTs.getNativeObjAddr(),mRgba.getNativeObjAddr());
            System.out.println("ok3");



            try{
    //            Log.i(TAG,"onTouch event");
    //            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    //            String currentDateandTime = sdf.format(new Date());
    //            String fileName = Environment.getExternalStorageDirectory().getPath() +
    //                    "/sample_picture_" + currentDateandTime + ".jpg";
    //            mOpenCvCameraView.takePicture(fileName);
    //            Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
            }
            catch (Exception ex)
            {
                throw  ex;
            }

            return false;
        }



        //public native void drawPts(long matAddrPts, long matAddrRgba);
    }

> Activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nimesh.opencv.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />


    <org.opencv.android.JavaCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/MainActivityCameraView"
        opencv:show_fps="true"
        opencv:camera_id="any" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2"
        android:layout_marginRight="43dp"
        android:layout_marginEnd="43dp"
        android:layout_alignBottom="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true" />
    <!--<Button-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="Capture"-->
        <!--android:id="@+id/button"-->
        <!--android:layout_alignParentBottom="true"-->
        <!--android:layout_centerHorizontal="true" />-->

</RelativeLayout>

0 个答案:

没有答案