Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ball2;
- import android.support.v7.app.ActionBarActivity;
- import android.support.v7.app.ActionBar;
- import android.support.v4.app.Fragment;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.os.Build;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Locale;
- 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.Scalar;
- import org.opencv.core.Size;
- import org.opencv.imgproc.Imgproc;
- import org.opencv.core.Point;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.WindowManager;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import android.content.Context;
- import android.util.Log;
- import android.widget.Toast;
- //for IOIO
- import ioio.lib.api.DigitalOutput;
- import ioio.lib.api.DigitalOutput.Spec.Mode;
- import ioio.lib.api.IOIO;
- import ioio.lib.api.IOIO.VersionType;
- import ioio.lib.api.exception.ConnectionLostException;
- import ioio.lib.util.BaseIOIOLooper;
- import ioio.lib.util.IOIOLooper;
- import ioio.lib.util.android.IOIOActivity;
- import ioio.lib.api.PwmOutput;
- public class MainActivity extends Activity implements CvCameraViewListener2 {
- private static final String TAG = "OCVSample::Activity";
- private static final int VIEW_MODE_RGBA = 0;
- private static final int VIEW_MODE_FEATURES = 5;
- private int mViewMode;
- private Mat mRgba;
- private Mat mIntermediateMat;
- private Mat mGray;
- private Mat mHSV;
- private Mat mThresholded;
- private Mat mThresholded2;
- private Mat array255;
- private Mat distance;
- private MenuItem mItemPreviewRGBA;
- private MenuItem mItemPreviewFeatures;
- private CameraBridgeViewBase mOpenCvCameraView;
- //Point for cross at center of ball
- private Point pt1;
- private Point pt2;
- private Scalar colorRed;
- //Point for cross at center of screen
- private Point pt12;
- private Point pt22;
- private Scalar colorBlue;
- //Point that will be calculated as diff from center of ball to center of screen
- private Point ptcalc;
- //Set the resolution for the camera to operate
- int width = 320;
- int height = 240;
- //TODO For ioio
- private final int PAN_PIN = 3;
- private final int TILT_PIN = 6;
- private final int PWM_FREQ = 100;
- private Point ptcalcval;
- private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
- @Override
- public void onManagerConnected(int status) {
- switch (status) {
- case LoaderCallbackInterface.SUCCESS:
- {
- Log.i(TAG, "OpenCV loaded successfully");
- mOpenCvCameraView.enableView();
- } break;
- default:
- {
- super.onManagerConnected(status);
- } break;
- }
- }
- };
- public MainActivity() {
- Log.i(TAG, "Instantiated new " + this.getClass());
- }
- // Called when the activity is first created.
- @Override
- public void onCreate(Bundle savedInstanceState) {
- Log.i(TAG, "called onCreate");
- super.onCreate(savedInstanceState);
- //Keep screen on
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- //Set the view to the layout in the xml file
- setContentView(R.layout.activity_main);
- mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.displayc);
- mOpenCvCameraView.setCvCameraViewListener(this);
- //Set the resolution for the input frames
- mOpenCvCameraView.setMaxFrameSize(width, height);
- }
- //Give options when menu button is pressed
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- Log.i(TAG, "called onCreateOptionsMenu");
- mItemPreviewRGBA = menu.add("RGBA");
- mItemPreviewFeatures = menu.add("Ball");
- return true;
- }
- @Override
- public void onPause()
- {
- super.onPause();
- if (mOpenCvCameraView != null)
- mOpenCvCameraView.disableView();
- }
- @Override
- public void onResume()
- {
- super.onResume();
- OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
- }
- public void onDestroy() {
- super.onDestroy();
- if (mOpenCvCameraView != null)
- mOpenCvCameraView.disableView();
- }
- public void onCameraViewStopped() {
- mRgba.release();
- mGray.release();
- mIntermediateMat.release();
- }
- public void onCameraViewStarted(int width, int height) {
- mRgba = new Mat(height, width, CvType.CV_8UC4);
- mHSV = new Mat(height, width, CvType.CV_8UC4);
- mIntermediateMat = new Mat(height, width, CvType.CV_8UC4);
- mGray = new Mat(height, width, CvType.CV_8UC1);
- array255=new Mat(height,width,CvType.CV_8UC1);
- distance=new Mat(height,width,CvType.CV_8UC1);
- mThresholded=new Mat(height,width,CvType.CV_8UC1);
- mThresholded2=new Mat(height,width,CvType.CV_8UC1);
- //Points for center of circle
- pt1 = new Point(0.0D, 0.0D);
- pt2 = new Point(0.0D, 0.0D);
- //Color for the cross at the center of the circle
- colorRed = new Scalar(50D, 0.0D, 0.0D, 255D);
- //Points for center of screen
- pt12 = new Point(0.0D, 0.0D);
- pt22 = new Point(0.0D, 0.0D);
- //Color for the cross at the center of the screen
- colorBlue = new Scalar(100D, 100.0D, 0.0D, 255D);
- //Point that will be calculated
- ptcalc = new Point(0.0D, 0.0D);
- //TODO For IOIO
- ptcalcval=new Point(0.0D, 0.0D);
- Log.d("ADebugTag", "Value: " + Float.toString(124));
- }
- public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
- final int viewMode = mViewMode;
- mRgba = inputFrame.rgba();
- if (viewMode==VIEW_MODE_RGBA) return mRgba;
- List<Mat> lhsv = new ArrayList<Mat>(3);
- Mat circles = new Mat();
- array255.setTo(new Scalar(255));
- //Numbers bellow change the color that the ball should be
- Scalar hsv_min = new Scalar(0, 50, 50, 0);
- Scalar hsv_max = new Scalar(6, 255, 255, 0);
- Scalar hsv_min2 = new Scalar(175, 50, 50, 0);
- Scalar hsv_max2 = new Scalar(179, 255, 255, 0);
- Imgproc.cvtColor(mRgba, mHSV, Imgproc.COLOR_RGB2HSV,4);
- Core.inRange(mHSV, hsv_min, hsv_max, mThresholded);
- Core.inRange(mHSV, hsv_min2, hsv_max2, mThresholded2);
- Core.bitwise_or(mThresholded, mThresholded2, mThresholded);
- Core.split(mHSV, lhsv); // We get 3 2D one channel Mats
- Mat S = lhsv.get(1);
- Mat V = lhsv.get(2);
- Core.subtract(array255, S, S);
- Core.subtract(array255, V, V);
- S.convertTo(S, CvType.CV_32F);
- V.convertTo(V, CvType.CV_32F);
- Core.magnitude(S, V, distance);
- Core.inRange(distance,new Scalar(0.0), new Scalar(200.0), mThresholded2);
- Core.bitwise_and(mThresholded, mThresholded2, mThresholded);
- // Apply the Hough Transform to find the circles
- Imgproc.GaussianBlur(mThresholded, mThresholded, new Size(9,9),0,0);
- Imgproc.HoughCircles(mThresholded, circles, Imgproc.CV_HOUGH_GRADIENT, 2, mThresholded.height()/4, 500, 50, 0, 0);
- int rows = circles.rows();
- int elemSize = (int)circles.elemSize(); // Returns 12 (3 * 4bytes in a float)
- float[] data2 = new float[rows * elemSize/4];
- //Create a point at the centre of the screen
- Point ScreenCenter= new Point(width/2, height/2);
- //Draw cross in center of screen
- DrawCrossCentre(mRgba, colorBlue, ScreenCenter);
- //Write data to SD card (This cannot be inside the loop below or it will crash)
- WritetoSD();
- //ioiol(ptcalcval);
- //The lines below are only run when the ball is detected
- if (data2.length>0){
- circles.get(0, 0, data2); // Points to the first element and reads the whole thing
- // into data2
- for(int i=0; i<data2.length; i=i+3) {
- //Create new point for centre of ball
- Point center= new Point(data2[i], data2[i+1]);
- //Draws circle around ball
- 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 );
- //Draws cross on center of ball
- DrawCross(mRgba, colorRed, center);
- //If ball detected write number on screen
- Calc(center, ScreenCenter);
- }
- }
- //TODO remove if it doesn't work
- //ioiol(ptcalcval);
- return mRgba;
- //End of this sub
- }
- public boolean onOptionsItemSelected(MenuItem item) {
- Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
- if (item == mItemPreviewRGBA) {
- mViewMode = VIEW_MODE_RGBA;
- } else if (item == mItemPreviewFeatures) {
- mViewMode = VIEW_MODE_FEATURES;
- }
- return true;
- }
- //Draws cross on the center of the object detected.
- public void DrawCross(Mat mat, Scalar scalar, Point pointb)
- {
- pt1.x = pointb.x - (double)12;
- pt1.y = pointb.y;
- pt2.x = pointb.x + (double)12;
- pt2.y = pointb.y;
- Core.line(mat, pt1, pt2, scalar, -1 + 2);
- pt1.x = pointb.x;
- pt1.y = pointb.y + (double)12;
- pt2.x = pointb.x;
- pt2.y = pointb.y - (double)12;
- Core.line(mat, pt1, pt2, scalar, -1 + 2);
- }
- //Draws cross in center of the screen
- public void DrawCrossCentre(Mat mat, Scalar scalar, Point points)
- {
- //Input is point (centre of screen calculated from resolution), it has x and y coordinate
- pt12.x = points.x - (double)12;
- pt12.y = points.y;
- pt22.x = points.x + (double)12;
- pt22.y = points.y;
- Core.line(mat, pt12, pt22, scalar, -1 + 3);
- pt12.x = points.x;
- pt12.y = points.y + (double)12;
- pt22.x = points.x;
- pt22.y = points.y - (double)12;
- Core.line(mat, pt12, pt22, scalar, -1 + 3);
- }
- //Displays the values of the axis on screen
- public void Calc(Point pointb, Point points)
- {
- //Input is point, it has x and y coordinate
- ptcalc.x = pointb.x - points.x;
- ptcalc.y = points.y - pointb.y;
- //Write x axis values on screen
- Core.putText(mRgba, "x=" , new Point(80,100), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
- Core.putText(mRgba, Double.toString(ptcalc.x), new Point(125,100), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
- //Write y axis values on screen
- Core.putText(mRgba, "y=" , new Point(80,150), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
- Core.putText(mRgba, Double.toString(ptcalc.y), new Point(125,150), 3, 0.5, new Scalar(255,0,0), 1, 2, false);
- //ioiol(ptcalc);
- }
- //Writes data to SD card
- public void WritetoSD(){
- try {
- //Get file name and path from other sub
- File myFile = getFile();
- //Check if file exists
- if(!myFile.exists()){
- myFile.createNewFile();
- }
- FileWriter fileWritter = new FileWriter(myFile.getPath(),true);
- BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
- //Write data to text file
- bufferWritter.write(Double.toString(ptcalc.x));
- bufferWritter.write(",");
- bufferWritter.write(Double.toString(ptcalc.y)+"\n");
- bufferWritter.close();
- } catch (Exception e) {
- Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
- }
- }
- //Creates directory for file and assigns name to it, will create one every minute
- private File getFile() {
- File directory = new File(Environment.getExternalStorageDirectory().getPath(), getPackageName());
- directory.mkdirs();
- if (!directory.exists()) {
- if (!directory.mkdirs()) {
- Log.e(TAG, "Failed to create storage directory.");
- return null;
- }
- }
- String timeStamp = new SimpleDateFormat("dd_MM_yyy-HH_mm", Locale.UK).format(new Date());
- return new File(directory.getPath() + File.separator + timeStamp + ".txt");
- }
- //This is the end of the program
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement