我使用OpenCV处理的帧不会仅在应用调整大小时显示

时间:2018-08-26 02:38:04

标签: android xml android-activity opencv4android

此代码从移动相机获取帧并进行一些图像处理,但是返回的帧不会在我的应用程序中显示“只是调整大小的帧”。考虑到阈值后,出现的灰色rgb图像没有任何问题。

package com.example.digitech.opencvtest;


import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.ArraySet;
import android.util.Log;
import android.util.Size;
import android.view.SurfaceView;
import android.view.TextureView;
import android.widget.TextView;

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.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.imgproc.Imgproc;
import org.opencv.utils.Converters;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {

    private  static String TAG="MainActivity";

    JavaCameraView javaCameraView;
    Mat mRgba,gray,th,th1,mragbaresized,finalout;
    List<MatOfPoint> contours= new ArrayList<>();



    BaseLoaderCallback mloaderCallBack=new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case BaseLoaderCallback.SUCCESS: {
                    javaCameraView.enableView();
                    break;
                }
                default: {
                    super.onManagerConnected(status);
                    break;
                }
            }
        }
    };
    static{

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        javaCameraView=(JavaCameraView)findViewById(R.id.java_camera_view);
        javaCameraView.setVisibility(SurfaceView.VISIBLE);
        javaCameraView.setCvCameraViewListener(this);
    }
    @Override
    protected void onPause(){
        super.onPause();
        if(javaCameraView!=null)
            javaCameraView.disableView();
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(javaCameraView!=null)
            javaCameraView.disableView();
    }
    @Override
    protected  void onResume(){
        super.onResume();
        if(OpenCVLoader.initDebug()){
            Log.d(TAG,"success");
            mloaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
        else {
            Log.d(TAG,"not success");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION,this,mloaderCallBack);
        }
    }

    @Override
    public void onCameraViewStarted(int width, int height) {
        mRgba=new Mat(width,height, CvType.CV_8UC4);
        mragbaresized=new Mat(width,height, CvType.CV_8UC4);
        gray=new Mat(width,height, CvType.CV_8UC1);
        th=new Mat(width,height, CvType.CV_8S);
        th1=new Mat(width,height, CvType.CV_8S);
        finalout=new Mat(200,200, CvType.CV_8S);

    }

    @Override
    public void onCameraViewStopped() {

    }

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

        //Imgproc.resize(mragbaresized,mRgba,new org.opencv.core.Size(200,200));



        Imgproc.cvtColor(mRgba,gray,Imgproc.COLOR_RGB2GRAY);
        Imgproc.adaptiveThreshold(gray,th,255,Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,13,2);
        Imgproc.adaptiveThreshold(gray,th1,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,31,2);

        Mat hierarchy=new Mat();
        Imgproc.findContours(th,contours,hierarchy,Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
        double maxarea=0;
        MatOfPoint max_contour=new MatOfPoint();
        Iterator<MatOfPoint> iterator=contours.iterator();
        while (iterator.hasNext()){
            MatOfPoint contour =iterator.next();
            double area=Imgproc.contourArea(contour);
            if(area>maxarea){
                maxarea=area;
                max_contour=contour;
            }
        }
        System.out.println(max_contour);

        MatOfPoint hull =new MatOfPoint();
        Imgproc.convexHull(max_contour,hull);
        MatOfPoint2f simplifed =new MatOfPoint2f();
        MatOfPoint2f newhull =new MatOfPoint2f(hull.toArray());
        Imgproc.approxPolyDP(newhull,simplifed,0.08*Imgproc.arcLength(newhull,true),true);
        List simplifedlist =simplifed.toList();

        int x=simplifedlist.size();
        Log.d(TAG,"x is:"+String.valueOf(simplifedlist));

        List<Point> windowslist = new ArrayList<>();
        windowslist.add(new Point(200,200));
        windowslist.add(new Point(0,200));
        windowslist.add(new Point(0,0));
        windowslist.add(new Point(200,0));

        Mat windows2f= Converters.vector_Point2f_to_Mat(windowslist);



        if(x==4 && maxarea>14000 && maxarea<30000){



            Mat m=  Imgproc.getPerspectiveTransform(simplifed,windows2f);
            Imgproc.warpAffine(th1,finalout,m,new org.opencv.core.Size(200,200));


            }




        // Imgproc.approxPolyDP();



        return th1;
    }
}

0 个答案:

没有答案