Advertisement
olcayertas

OpenCv Android Optic Flow

May 23rd, 2014
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.08 KB | None | 0 0
  1. package com.mukcay.cvtest;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.PriorityQueue;
  10.  
  11. import org.opencv.android.BaseLoaderCallback;
  12. import org.opencv.android.CameraBridgeViewBase;
  13. import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
  14. import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
  15. import org.opencv.android.LoaderCallbackInterface;
  16. import org.opencv.android.OpenCVLoader;
  17. import org.opencv.core.CvType;
  18. import org.opencv.core.Mat;
  19. import org.opencv.core.MatOfByte;
  20. import org.opencv.core.MatOfFloat;
  21. import org.opencv.core.MatOfPoint;
  22. import org.opencv.core.MatOfPoint2f;
  23. import org.opencv.core.Point;
  24. import org.opencv.core.Size;
  25. import org.opencv.imgproc.Imgproc;
  26. import org.opencv.objdetect.CascadeClassifier;
  27. import org.opencv.video.Video;
  28.  
  29. import android.app.Activity;
  30. import android.content.Context;
  31. import android.os.Bundle;
  32. import android.util.Log;
  33. import android.view.SurfaceView;
  34. import android.view.WindowManager;
  35.  
  36. public class MainActivity extends Activity implements CvCameraViewListener2 {
  37.  
  38.     private String TAG = "MUKCAY";
  39.     private CameraBridgeViewBase mOpenCvCameraView;
  40.     private CascadeClassifier mCascade;
  41.     private PriorityQueue<Direction> pq;
  42.    
  43.     private List<Byte> byteStatus;
  44.     private List<Point> cornersThis;
  45.     private List<Point> cornersPrev;
  46.    
  47.     private MatOfPoint2f mMOP2fptsPrev;
  48.     private MatOfPoint2f mMOP2fptsThis;
  49.     private MatOfPoint2f mMOP2fptsSafe;
  50.    
  51.     private MatOfPoint MOPcorners;
  52.    
  53.     private MatOfFloat mMOFerr;
  54.    
  55.     private MatOfByte mMOBStatus;
  56.    
  57.     private Mat mRgba;
  58.     private Mat matOpFlowPrev;
  59.     private Mat matOpFlowThis;
  60.    
  61.     private Point pt0;
  62.     private Point pt1;
  63.     private Point pt2;
  64.    
  65.     private Size sMatSize;
  66.     private String string;
  67.    
  68.     private Direction up = new Direction("up",0);
  69.     private Direction down = new Direction("down",0);
  70.     private Direction left = new Direction("left",0);
  71.     private Direction right = new Direction("right",0);
  72.     private Direction r1;
  73.     private Direction r2;
  74.     private Direction r3;
  75.    
  76.     private int iGFFTMax = 40;
  77.     private int x;
  78.     private int y;
  79.     private double NOISE = 3;
  80.  
  81.     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
  82.         @Override
  83.         public void onManagerConnected(int status) {
  84.             switch (status) {
  85.             case LoaderCallbackInterface.SUCCESS: {
  86.                 Log.i(TAG, "OpenCV loaded successfully");
  87.                 mOpenCvCameraView.enableView();
  88.  
  89.                 try {
  90.                     Context context = getApplicationContext();
  91.                     InputStream is = context.getResources().openRawResource(
  92.                             R.raw.haarcascade_frontalface_default);
  93.                     File cascadeDir = context.getDir("cascade",
  94.                             Context.MODE_PRIVATE);
  95.                     File cascadeFile = new File(cascadeDir,
  96.                             "haarcascade_frontalface_default.xml");
  97.                     FileOutputStream os = new FileOutputStream(cascadeFile);
  98.                     byte[] buffer = new byte[4096];
  99.                     int bytesRead;
  100.  
  101.                     while ((bytesRead = is.read(buffer)) != -1) {
  102.                         os.write(buffer, 0, bytesRead);
  103.                     }
  104.  
  105.                     is.close();
  106.                     os.close();
  107.                     mCascade = new CascadeClassifier(
  108.                             cascadeFile.getAbsolutePath());
  109.  
  110.                     if (mCascade.empty()) {
  111.                         mCascade = null;
  112.                     }
  113.  
  114.                     cascadeFile.delete();
  115.                     cascadeDir.delete();
  116.                 } catch (IOException e) {
  117.                     e.printStackTrace();
  118.                 }
  119.             }
  120.                 break;
  121.  
  122.             default: {
  123.                 super.onManagerConnected(status);
  124.             }
  125.                 break;
  126.             }
  127.         }
  128.     };
  129.  
  130.     @Override
  131.     public void onResume() {
  132.         super.onResume();
  133.         OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this,
  134.                 mLoaderCallback);
  135.     }
  136.  
  137.     @Override
  138.     protected void onCreate(Bundle savedInstanceState) {
  139.         Log.i(TAG, "called onCreate");
  140.         super.onCreate(savedInstanceState);
  141.         pq = new PriorityQueue<Direction>();
  142.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  143.         setContentView(R.layout.activity_main);
  144.         mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.CVTest);
  145.         mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
  146.         mOpenCvCameraView.setCvCameraViewListener(this);
  147.  
  148.         //OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
  149.     }
  150.  
  151.     @Override
  152.     public void onPause() {
  153.         super.onPause();
  154.         if (mOpenCvCameraView != null) {
  155.             mOpenCvCameraView.disableView();
  156.         }
  157.     }
  158.  
  159.     public void onDestroy() {
  160.         super.onDestroy();
  161.         if (mOpenCvCameraView != null)
  162.             mOpenCvCameraView.disableView();
  163.     }
  164.  
  165.     public void onCameraViewStarted(int width, int height) {
  166.         byteStatus = new ArrayList<Byte>();
  167.         cornersThis = new ArrayList<Point>();
  168.         cornersPrev = new ArrayList<Point>();
  169.         mMOP2fptsPrev = new MatOfPoint2f();
  170.         mMOP2fptsThis = new MatOfPoint2f();
  171.         mMOP2fptsSafe = new MatOfPoint2f();
  172.         mMOFerr = new MatOfFloat();
  173.         mMOBStatus = new MatOfByte();
  174.         MOPcorners = new MatOfPoint();
  175.         mRgba = new Mat();
  176.         matOpFlowThis = new Mat();
  177.         matOpFlowPrev = new Mat();
  178.         pt0 = new Point(0, 0);
  179.         pt1 = new Point(0, 0);
  180.         pt2 = new Point(0, 0);
  181.         sMatSize = new Size();
  182.         string = "";
  183.         mRgba = new Mat(height, width, CvType.CV_8UC4);
  184.     }
  185.  
  186.     public void onCameraViewStopped() {
  187.     }
  188.  
  189.     public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
  190.         up.value = 0;
  191.         down.value = 0;
  192.         left.value = 0;
  193.         right.value = 0;
  194.         pq.clear();
  195.         mRgba = inputFrame.rgba();
  196.         //inputFrame.copyTo(mRgba);
  197.         sMatSize.width = mRgba.width();
  198.         sMatSize.height = mRgba.height();
  199.  
  200.         if (mMOP2fptsPrev.rows() == 0) {
  201.             Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);
  202.             matOpFlowThis.copyTo(matOpFlowPrev);
  203.             Imgproc.goodFeaturesToTrack(matOpFlowPrev, MOPcorners, iGFFTMax, 0.05, 20);
  204.             mMOP2fptsPrev.fromArray(MOPcorners.toArray());
  205.             mMOP2fptsPrev.copyTo(mMOP2fptsSafe);
  206.         } else {
  207.             matOpFlowThis.copyTo(matOpFlowPrev);
  208.             Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);
  209.             Imgproc.goodFeaturesToTrack(matOpFlowThis, MOPcorners, iGFFTMax, 0.05, 20);
  210.             mMOP2fptsThis.fromArray(MOPcorners.toArray());
  211.             mMOP2fptsSafe.copyTo(mMOP2fptsPrev);
  212.             mMOP2fptsThis.copyTo(mMOP2fptsSafe);
  213.         }
  214.  
  215.         Video.calcOpticalFlowPyrLK(matOpFlowPrev, matOpFlowThis, mMOP2fptsPrev, mMOP2fptsThis, mMOBStatus, mMOFerr);
  216.  
  217.         cornersPrev = mMOP2fptsPrev.toList();
  218.         cornersThis = mMOP2fptsThis.toList();
  219.         byteStatus = mMOBStatus.toList();
  220.  
  221.         y = byteStatus.size() - 1;
  222.  
  223.         for (x = 0; x < y; x++) {
  224.             if (byteStatus.get(x) == 1) {
  225.                 pt0 = cornersThis.get(x);
  226.                 pt2 = cornersPrev.get(x);
  227.                 double m = Math.abs(pt2.y - pt0.y ) / Math.abs(pt2.x - pt0.x);
  228.                 double distance= Math.sqrt(Math.pow((pt0.x - pt2.x),2) + Math.pow((pt0.y - pt2.y),2));
  229.                
  230.                 if(distance < NOISE)
  231.                     continue;
  232.                
  233.                 if (pt0.x < pt2.x && pt2.y < pt0.y)
  234.                     if (m > 1)
  235.                         up.value++;
  236.                     else
  237.                         right.value++;
  238.                 else if (pt0.x < pt2.x && pt2.y == pt0.y)
  239.                     right.value++;
  240.                 else if (pt0.x < pt2.x && pt2.y > pt0.y)
  241.                     if (m > 1)
  242.                         down.value++;
  243.                     else
  244.                         right.value++;
  245.                 else if (pt0.x == pt2.x && pt2.y > pt0.y)
  246.                     down.value++;
  247.                 else if (pt0.x > pt2.x && pt2.y > pt0.y)
  248.                     if (m > 1)
  249.                         down.value++;
  250.                     else
  251.                         left.value++;
  252.                 else if (pt0.x > pt2.x && pt2.y == pt0.y)
  253.                     left.value++;
  254.                 else if (pt0.x > pt2.x && pt2.y < pt0.y)
  255.                     if (m > 1)
  256.                         up.value++;
  257.                     else
  258.                         left.value++;
  259.                 else if (pt0.x == pt2.x && pt2.y < pt0.y)
  260.                     up.value++;
  261.             }
  262.         }//end of for
  263.        
  264.        
  265.         if(up.value == 0 && left.value == 0 && right.value == 0 && down.value == 0) {
  266.             string = String.format("Direction: ---");
  267.            
  268. //          runOnUiThread(new Runnable() {
  269. //              @Override
  270. //              public void run() {
  271. //                  updateDirection(string);
  272. //              }
  273. //          });
  274.            
  275.         }else{
  276.             if (left.value < right.value) {
  277.                 r1 = right;
  278.             } else r1 = left;
  279.            
  280.             if (up.value < down.value) {
  281.                 r2 = down;
  282.             } else r2 = up;
  283.            
  284.             if (r1.value < r2.value) {
  285.                 r3 = r2;
  286.             } else r3 = r1;
  287.            
  288.             string = String.format("Direction: %s", r3.name);
  289.  
  290. //          runOnUiThread(new Runnable() {
  291. //              @Override
  292. //              public void run() {
  293. //                  updateDirection(string);
  294. //              }
  295. //          });
  296.         }
  297.  
  298.         return mRgba;
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement