devendradhanal

Circle Detection Android Opencv Procedure 2

Dec 4th, 2013
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1.  
  2. private void takePhoto() {
  3.         PictureCallback pictureCB = new PictureCallback() {
  4.             public void onPictureTaken(byte[] data, Camera cam) {
  5.                 byte[] photoData = data;
  6.                 try {
  7.                 } finally {
  8.                 Bitmap photo = (Bitmap) BitmapFactory.decodeByteArray(
  9.                             photoData, 0, photoData.length);
  10.                     Mat mat  = new Mat();
  11.                    
  12.                     mat = new Mat(photo.getHeight(), photo.getWidth(),
  13.                             CvType.CV_8UC4);
  14.                    
  15.                     Utils.bitmapToMat(photo, mat);
  16.  
  17.  
  18.                     // Procedure using sobel derivative
  19.                     Imgproc.cvtColor(mat, processingFrame, Imgproc.COLOR_RGBA2BGR, 3);
  20.                     Imgproc.GaussianBlur(processingFrame, processingFrame, sSize3, 2, 2, Imgproc.BORDER_DEFAULT);
  21.                     Imgproc.cvtColor(processingFrame, mGray, Imgproc.COLOR_RGBA2GRAY);
  22.                     // / Gradient X //
  23.                     Imgproc.Sobel(mGray, grad_x, CvType.CV_16S, 1, 0, 3, scale, delta,
  24.                             Imgproc.BORDER_DEFAULT);
  25.                     Imgproc.Sobel(mGray, grad_x, CvType.CV_16S, 1, 0, 3, scale, delta);
  26.                     Core.convertScaleAbs(grad_x, abs_grad_x); // / Gradient Y //
  27.                     Imgproc.Sobel(mGray, grad_y, CvType.CV_16S, 0, 1, 3, scale, delta,
  28.                             Imgproc.BORDER_DEFAULT);
  29.                     Imgproc.Sobel(mGray, grad_y, CvType.CV_16S, 0, 1, 3, scale, delta);
  30.                     Core.convertScaleAbs(grad_y, abs_grad_y);
  31.                     // / Total Gradient(approximate)
  32.                     Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
  33.                     // Procedure using sobel derivative End
  34.  
  35.                     iMinRadius = 0;
  36.                     iMaxRadius = 0;
  37.                     iCannyUpperThreshold = 100;
  38.                     iAccumulator = 300;
  39.                     Imgproc.HoughCircles(grad, mIntermediateMat, Imgproc.CV_HOUGH_GRADIENT,
  40.                                                 2.0, 10, iCannyUpperThreshold, iAccumulator,
  41.                                                 iMinRadius, iMaxRadius);
  42.                     radius = 0;
  43.                     if (mIntermediateMat.cols() > 0) {
  44.                         for (int x = 0; x < Math.min(mIntermediateMat.cols(), 10); x++) {
  45.                             double vCircle[] = mIntermediateMat.get(0, x);
  46.                                 if (vCircle == null)
  47.                                     break;
  48.                             Log.i("Circle :", "Yes ");
  49.                                 totalCirclesDetected++;
  50.                                 pt.x = Math.round(vCircle[0]);
  51.                                 pt.y = Math.round(vCircle[1]);
  52.                                 radius = (int) Math.round(vCircle[2]);
  53.                                 // draw the found circle
  54.                                 Core.circle(processingFrame, pt, radius, colorRed, iLineThickness);
  55.                         }
  56.                     } else {
  57.                         Log.i("Circle :", "No");
  58.                     }
  59.  
  60.                     }
  61.         }
  62.     };
  63.     mCamera.takePicture(null, null, pictureCB);
  64. }
Add Comment
Please, Sign In to add comment