Advertisement
vincentu5

Opencv android

Mar 24th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.06 KB | None | 0 0
  1. package com.example.ball2;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.support.v7.app.ActionBar;
  5. import android.support.v4.app.Fragment;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.view.LayoutInflater;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.os.Build;
  14.  
  15. import java.text.SimpleDateFormat;
  16. import java.util.ArrayList;  
  17. import java.util.Date;
  18. import java.util.List;  
  19. import java.util.Locale;
  20.  
  21. import org.opencv.android.BaseLoaderCallback;  
  22. import org.opencv.android.CameraBridgeViewBase;  
  23. import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;  
  24. import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;  
  25. import org.opencv.android.LoaderCallbackInterface;  
  26. import org.opencv.android.OpenCVLoader;  
  27. import org.opencv.core.Core;  
  28. import org.opencv.core.CvType;  
  29. import org.opencv.core.Mat;  
  30. import org.opencv.core.Scalar;  
  31. import org.opencv.core.Size;  
  32. import org.opencv.imgproc.Imgproc;  
  33. import org.opencv.core.Point;  
  34.  
  35. import android.app.Activity;  
  36. import android.os.Bundle;  
  37. import android.util.Log;  
  38. import android.view.Menu;  
  39. import android.view.MenuItem;  
  40. import android.view.WindowManager;
  41.  
  42. import java.io.BufferedReader;
  43. import java.io.BufferedWriter;
  44. import java.io.File;
  45. import java.io.FileNotFoundException;
  46. import java.io.FileOutputStream;
  47. import java.io.FileWriter;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import java.io.InputStreamReader;
  51. import java.io.OutputStreamWriter;
  52.  
  53. import android.content.Context;
  54. import android.util.Log;
  55. import android.widget.Toast;
  56.  
  57. //for IOIO
  58. import ioio.lib.api.DigitalOutput;
  59. import ioio.lib.api.DigitalOutput.Spec.Mode;
  60. import ioio.lib.api.IOIO;
  61. import ioio.lib.api.IOIO.VersionType;
  62. import ioio.lib.api.exception.ConnectionLostException;
  63. import ioio.lib.util.BaseIOIOLooper;
  64. import ioio.lib.util.IOIOLooper;
  65. import ioio.lib.util.android.IOIOActivity;
  66. import ioio.lib.api.PwmOutput;
  67.  
  68.  
  69. public class MainActivity extends Activity implements CvCameraViewListener2  {  
  70.    
  71.        
  72.   private static final String  TAG = "OCVSample::Activity";  
  73.   private static final int    VIEW_MODE_RGBA   = 0;  
  74.   private static final int    VIEW_MODE_FEATURES = 5;  
  75.   private int mViewMode;  
  76.   private Mat mRgba;  
  77.   private Mat mIntermediateMat;  
  78.   private Mat mGray;  
  79.   private Mat mHSV;  
  80.   private Mat mThresholded;  
  81.   private Mat mThresholded2;  
  82.   private Mat array255;  
  83.   private Mat distance;  
  84.   private MenuItem mItemPreviewRGBA;  
  85.   private MenuItem mItemPreviewFeatures;  
  86.   private CameraBridgeViewBase  mOpenCvCameraView;  
  87.  
  88.   //Point for cross at center of ball
  89.   private Point pt1;
  90.   private Point pt2;
  91.   private Scalar colorRed;
  92.   //Point for cross at center of screen
  93.   private Point pt12;
  94.   private Point pt22;
  95.   private Scalar colorBlue;
  96.   //Point that will be calculated as diff from center of ball to center of screen
  97.   private Point ptcalc;  
  98.   //Set the resolution for the camera to operate
  99.   int width = 320;
  100.   int height = 240;
  101.   //TODO For ioio
  102.   private final int PAN_PIN = 3;
  103.   private final int TILT_PIN = 6;
  104.   private final int PWM_FREQ = 100;
  105.   private Point ptcalcval;
  106.  
  107.  
  108.  
  109.   private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {  
  110.  
  111.      
  112.      
  113.     @Override  
  114.     public void onManagerConnected(int status) {  
  115.       switch (status) {  
  116.         case LoaderCallbackInterface.SUCCESS:  
  117.         {  
  118.           Log.i(TAG, "OpenCV loaded successfully");  
  119.           mOpenCvCameraView.enableView();  
  120.         } break;  
  121.         default:  
  122.         {  
  123.           super.onManagerConnected(status);  
  124.         } break;  
  125.       }  
  126.     }  
  127.   };
  128.  
  129.  
  130.   public MainActivity() {  
  131.     Log.i(TAG, "Instantiated new " + this.getClass());  
  132.   }  
  133.   // Called when the activity is first created.  
  134.   @Override  
  135.   public void onCreate(Bundle savedInstanceState) {  
  136.     Log.i(TAG, "called onCreate");  
  137.     super.onCreate(savedInstanceState);  
  138.     //Keep screen on
  139.     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);  
  140.     //Set the view to the layout in the xml file
  141.     setContentView(R.layout.activity_main);  
  142.     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.displayc);  
  143.     mOpenCvCameraView.setCvCameraViewListener(this);
  144.     //Set the resolution for the input frames
  145.     mOpenCvCameraView.setMaxFrameSize(width, height);
  146.   }
  147.   //Give options when menu button is pressed
  148.   @Override  
  149.   public boolean onCreateOptionsMenu(Menu menu) {  
  150.     Log.i(TAG, "called onCreateOptionsMenu");  
  151.     mItemPreviewRGBA = menu.add("RGBA");  
  152.     mItemPreviewFeatures = menu.add("Ball");
  153.     return true;  
  154.   }
  155.   @Override  
  156.   public void onPause()  
  157.   {  
  158.     super.onPause();  
  159.     if (mOpenCvCameraView != null)  
  160.       mOpenCvCameraView.disableView();
  161.   }  
  162.   @Override  
  163.   public void onResume()  
  164.   {  
  165.     super.onResume();  
  166.     OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);  
  167.   }  
  168.   public void onDestroy() {  
  169.     super.onDestroy();  
  170.     if (mOpenCvCameraView != null)  
  171.       mOpenCvCameraView.disableView();
  172.   }  
  173.   public void onCameraViewStopped() {  
  174.         mRgba.release();  
  175.         mGray.release();  
  176.         mIntermediateMat.release();
  177.       }
  178.  
  179.   public void onCameraViewStarted(int width, int height) {  
  180.     mRgba = new Mat(height, width, CvType.CV_8UC4);  
  181.     mHSV = new Mat(height, width, CvType.CV_8UC4);  
  182.     mIntermediateMat = new Mat(height, width, CvType.CV_8UC4);  
  183.     mGray = new Mat(height, width, CvType.CV_8UC1);  
  184.     array255=new Mat(height,width,CvType.CV_8UC1);  
  185.     distance=new Mat(height,width,CvType.CV_8UC1);  
  186.     mThresholded=new Mat(height,width,CvType.CV_8UC1);  
  187.     mThresholded2=new Mat(height,width,CvType.CV_8UC1);
  188.     //Points for center of circle
  189.     pt1 = new Point(0.0D, 0.0D);
  190.     pt2 = new Point(0.0D, 0.0D);
  191.     //Color for the cross at the center of the circle
  192.     colorRed = new Scalar(50D, 0.0D, 0.0D, 255D);
  193.     //Points for center of screen
  194.     pt12 = new Point(0.0D, 0.0D);
  195.     pt22 = new Point(0.0D, 0.0D);
  196.     //Color for the cross at the center of the screen
  197.     colorBlue = new Scalar(100D, 100.0D, 0.0D, 255D);
  198.     //Point that will be calculated
  199.     ptcalc = new Point(0.0D, 0.0D);
  200.    
  201.     //TODO For IOIO
  202.     ptcalcval=new Point(0.0D, 0.0D);
  203.    
  204.     Log.d("ADebugTag", "Value: " + Float.toString(124));
  205.   }
  206.  
  207.  
  208.   public Mat onCameraFrame(CvCameraViewFrame inputFrame) {  
  209.        final int viewMode = mViewMode;  
  210.        mRgba = inputFrame.rgba();  
  211.        
  212.        if (viewMode==VIEW_MODE_RGBA) return mRgba;  
  213.          
  214.        List<Mat> lhsv = new ArrayList<Mat>(3);            
  215.        Mat circles = new Mat();  
  216.        array255.setTo(new Scalar(255));  
  217.        //Numbers bellow change the color that the ball should be
  218.        Scalar hsv_min = new Scalar(0, 50, 50, 0);  
  219.        Scalar hsv_max = new Scalar(6, 255, 255, 0);  
  220.        Scalar hsv_min2 = new Scalar(175, 50, 50, 0);  
  221.        Scalar hsv_max2 = new Scalar(179, 255, 255, 0);  
  222.  
  223.        Imgproc.cvtColor(mRgba, mHSV, Imgproc.COLOR_RGB2HSV,4);
  224.        Core.inRange(mHSV, hsv_min, hsv_max, mThresholded);                    
  225.        Core.inRange(mHSV, hsv_min2, hsv_max2, mThresholded2);  
  226.        Core.bitwise_or(mThresholded, mThresholded2, mThresholded);  
  227.        
  228.        Core.split(mHSV, lhsv); // We get 3 2D one channel Mats  
  229.        Mat S = lhsv.get(1);  
  230.        Mat V = lhsv.get(2);  
  231.        Core.subtract(array255, S, S);  
  232.        Core.subtract(array255, V, V);  
  233.        S.convertTo(S, CvType.CV_32F);  
  234.        V.convertTo(V, CvType.CV_32F);  
  235.        Core.magnitude(S, V, distance);  
  236.        
  237.        Core.inRange(distance,new Scalar(0.0), new Scalar(200.0), mThresholded2);  
  238.        Core.bitwise_and(mThresholded, mThresholded2, mThresholded);  
  239.  
  240.        // Apply the Hough Transform to find the circles  
  241.        Imgproc.GaussianBlur(mThresholded, mThresholded, new Size(9,9),0,0);  
  242.        Imgproc.HoughCircles(mThresholded, circles, Imgproc.CV_HOUGH_GRADIENT, 2, mThresholded.height()/4, 500, 50, 0, 0);      
  243.        
  244.         int rows = circles.rows();  
  245.         int elemSize = (int)circles.elemSize(); // Returns 12 (3 * 4bytes in a float)  
  246.         float[] data2 = new float[rows * elemSize/4];  
  247.        
  248.         //Create a point at the centre of the screen
  249.         Point ScreenCenter= new Point(width/2, height/2);
  250.         //Draw cross in center of screen
  251.         DrawCrossCentre(mRgba, colorBlue, ScreenCenter);
  252.        
  253.         //Write data to SD card (This cannot be inside the loop below or it will crash)        
  254.         WritetoSD();
  255.         //ioiol(ptcalcval);
  256.        
  257.         //The lines below are only run when the ball is detected
  258.         if (data2.length>0){  
  259.              circles.get(0, 0, data2); // Points to the first element and reads the whole thing  
  260.                                              // into data2  
  261.              for(int i=0; i<data2.length; i=i+3) {  
  262.                   //Create new point for centre of ball
  263.                   Point center= new Point(data2[i], data2[i+1]);  
  264.                   //Draws circle around ball
  265.                   Core.ellipse( mRgba, center, new Size((double)data2[i+2], (double)data2[i+2]), 0, 0, 360, new Scalar( 100, 0, 255 ), 2, 8, 0 );
  266.                   //Draws cross on center of ball
  267.                   DrawCross(mRgba, colorRed, center);
  268.                   //If ball detected write number on screen
  269.                   Calc(center, ScreenCenter);              
  270.              }  
  271.          }  
  272.        //TODO remove if it doesn't work
  273.        //ioiol(ptcalcval);
  274.        return mRgba;
  275.   //End of this sub
  276.   }  
  277.  
  278.  
  279. public boolean onOptionsItemSelected(MenuItem item) {  
  280.     Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);  
  281.     if (item == mItemPreviewRGBA) {  
  282.       mViewMode = VIEW_MODE_RGBA;  
  283.     } else if (item == mItemPreviewFeatures) {  
  284.       mViewMode = VIEW_MODE_FEATURES;  
  285.     }  
  286.     return true;  
  287.   }  
  288.  
  289.   //Draws cross on the center of the object detected.
  290.   public void DrawCross(Mat mat, Scalar scalar, Point pointb)
  291.   {
  292.       pt1.x = pointb.x - (double)12;
  293.       pt1.y = pointb.y;
  294.       pt2.x = pointb.x + (double)12;
  295.       pt2.y = pointb.y;
  296.       Core.line(mat, pt1, pt2, scalar, -1 + 2);
  297.       pt1.x = pointb.x;
  298.       pt1.y = pointb.y + (double)12;
  299.       pt2.x = pointb.x;
  300.       pt2.y = pointb.y - (double)12;
  301.       Core.line(mat, pt1, pt2, scalar, -1 + 2);
  302.   }
  303.  
  304.   //Draws cross in center of the screen
  305.   public void DrawCrossCentre(Mat mat, Scalar scalar, Point points)
  306.   {
  307.       //Input is point (centre of screen calculated from resolution), it has x and y coordinate
  308.       pt12.x = points.x - (double)12;
  309.       pt12.y = points.y;
  310.       pt22.x = points.x + (double)12;
  311.       pt22.y = points.y;
  312.       Core.line(mat, pt12, pt22, scalar, -1 + 3);
  313.       pt12.x = points.x;
  314.       pt12.y = points.y + (double)12;
  315.       pt22.x = points.x;
  316.       pt22.y = points.y - (double)12;
  317.       Core.line(mat, pt12, pt22, scalar, -1 + 3);
  318.   }
  319.  
  320.   //Displays the values of the axis on screen
  321.   public void Calc(Point pointb, Point points)
  322.   {
  323.       //Input is point, it has x and y coordinate
  324.       ptcalc.x = pointb.x - points.x;
  325.       ptcalc.y = points.y - pointb.y;
  326.       //Write x axis values on screen
  327.       Core.putText(mRgba, "x=" , new Point(80,100), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
  328.       Core.putText(mRgba, Double.toString(ptcalc.x), new Point(125,100), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
  329.       //Write y axis values on screen
  330.       Core.putText(mRgba, "y=" , new Point(80,150), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
  331.       Core.putText(mRgba, Double.toString(ptcalc.y), new Point(125,150), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
  332.       //ioiol(ptcalc);
  333.   }  
  334.   //Writes data to SD card
  335.   public void WritetoSD(){
  336.     try {
  337.         //Get file name and path from other sub
  338.         File myFile = getFile();
  339.         //Check if file exists
  340.         if(!myFile.exists()){
  341.             myFile.createNewFile();
  342.         }
  343.         FileWriter fileWritter = new FileWriter(myFile.getPath(),true);
  344.         BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
  345.         //Write data to text file
  346.         bufferWritter.write(Double.toString(ptcalc.x));
  347.         bufferWritter.write(",");
  348.         bufferWritter.write(Double.toString(ptcalc.y)+"\n");
  349.         bufferWritter.close();
  350.     } catch (Exception e) {
  351.         Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
  352.     }
  353.   }
  354.   //Creates directory for file and assigns name to it, will create one every minute
  355.   private File getFile() {
  356.       File directory = new File(Environment.getExternalStorageDirectory().getPath(), getPackageName());
  357.       directory.mkdirs();
  358.       if (!directory.exists()) {
  359.         if (!directory.mkdirs()) {
  360.           Log.e(TAG, "Failed to create storage directory.");
  361.           return null;
  362.         }
  363.       }
  364.       String timeStamp = new SimpleDateFormat("dd_MM_yyy-HH_mm", Locale.UK).format(new Date());
  365.       return new File(directory.getPath() + File.separator + timeStamp + ".txt");
  366. }
  367.  
  368.  
  369.  
  370.  
  371.  
  372. //This is the end of the program
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement