devendradhanal

Circle Detection Android Opencv Procedure 1

Dec 4th, 2013
1,994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. private void takePhoto() {
  2.         PictureCallback pictureCB = new PictureCallback() {
  3.             public void onPictureTaken(byte[] data, Camera cam) {
  4.                 byte[] photoData = data;
  5.                 try {
  6.                 } finally {
  7.                 Bitmap photo = (Bitmap) BitmapFactory.decodeByteArray(
  8.                             photoData, 0, photoData.length);
  9.                     Mat mat  = new Mat();
  10.                    
  11.                     mat = new Mat(photo.getHeight(), photo.getWidth(),
  12.                             CvType.CV_8UC4);
  13.                    
  14.                     Utils.bitmapToMat(photo, mat);
  15.                     // Procedure 1
  16.                     Imgproc.cvtColor(mat, processingFrame, Imgproc.COLOR_RGBA2BGR, 3);
  17.                     Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGBA2BGR, 3);
  18.                     Imgproc.cvtColor(processingFrame, processingFrame, Imgproc.COLOR_RGBA2GRAY);
  19.                     // doing a gaussian blur prevents getting a lot of false hits
  20.                     Imgproc.GaussianBlur(processingFrame, grad, new Size(5,5), 2, 2);
  21.                     // Procedure 1 End
  22.                    
  23.                     iMinRadius = 0;
  24.                     iMaxRadius = 0;
  25.                     iCannyUpperThreshold = 100;
  26.                     iAccumulator = 300;
  27.                     Imgproc.HoughCircles(grad, mIntermediateMat, Imgproc.CV_HOUGH_GRADIENT,
  28.                                                 2.0, 10, iCannyUpperThreshold, iAccumulator,
  29.                                                 iMinRadius, iMaxRadius);
  30.                     radius = 0;
  31.                     if (mIntermediateMat.cols() > 0) {
  32.                         for (int x = 0; x < Math.min(mIntermediateMat.cols(), 10); x++) {
  33.                             double vCircle[] = mIntermediateMat.get(0, x);
  34.                                 if (vCircle == null)
  35.                                     break;
  36.                             Log.i("Circle :", "Yes ");
  37.                                 totalCirclesDetected++;
  38.                                 pt.x = Math.round(vCircle[0]);
  39.                                 pt.y = Math.round(vCircle[1]);
  40.                                 radius = (int) Math.round(vCircle[2]);
  41.                                 // draw the found circle
  42.                                 Core.circle(processingFrame, pt, radius, colorRed, iLineThickness);
  43.                         }
  44.                     } else {
  45.                         Log.i("Circle :", "No");
  46.                     }
  47.             }
  48.         }
  49.     };
  50.     mCamera.takePicture(null, null, pictureCB);
  51. }
Add Comment
Please, Sign In to add comment