title: Coin detection using android opencv
body: <p>I am trying to detect coin ( circle ) detection using Opencv4Android.
  So far I have tried two approaches </p>
  
  <p>1 ) Regular method : </p>
  
  <pre><code>// convert image to grayscale
  Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);
  // apply Gaussian Blur 
  Imgproc.GaussianBlur(mGray, mGray, sSize5, 2, 2);
  
  iMinRadius = 20;
  iMaxRadius = 400;
  iAccumulator = 300;
  iCannyUpperThreshold = 100;
  
  //apply houghCircles 
  Imgproc.HoughCircles(mGray, mIntermediateMat, Imgproc.CV_HOUGH_GRADIENT, 2.0, mGray.rows() / 8, 
    iCannyUpperThreshold, iAccumulator, iMinRadius, iMaxRadius);
      if (mIntermediateMat.cols() &gt; 0)
      for (int x = 0; x &lt; Math.min(mIntermediateMat.cols(), 10); x++) {
        double vCircle[] = mIntermediateMat.get(0,x);
          if (vCircle == null)
            break;
            pt.x = Math.round(vCircle[0]);
            pt.y = Math.round(vCircle[1]);
            radius = (int)Math.round(vCircle[2]);
            // draw the found circle
            Core.circle(mRgba, pt, radius, colorRed, iLineThickness);
     }
  </code></pre>
  
  <p>2 ) Sobel and then Hough Cicles</p>
  
  <pre><code>// apply Gaussian Blur 
      Imgproc.GaussianBlur(mRgba, mRgba, sSize3, 2, 2,
              Imgproc.BORDER_DEFAULT);
  
      // / Convert it to grayscale
      Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);
      // / Gradient X
  
      Imgproc.Sobel(mGray, grad_x, CvType.CV_16S, 1, 0, 3, scale, delta,
              Imgproc.BORDER_DEFAULT);
      Core.convertScaleAbs(grad_x, abs_grad_x);
  
      // / Gradient Y
      Imgproc.Sobel(mGray, grad_y, CvType.CV_16S, 0, 1, 3, scale, delta,
              Imgproc.BORDER_DEFAULT);
      Core.convertScaleAbs(grad_y, abs_grad_y);
      // / Total Gradient (approximate)
      Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
  
      iCannyUpperThreshold = 100;
  
      Imgproc.HoughCircles(grad, mIntermediateMat,
              Imgproc.CV_HOUGH_GRADIENT, 2.0, grad.rows() / 8,
              iCannyUpperThreshold, iAccumulator, iMinRadius, iMaxRadius);
  
      if (mIntermediateMat.cols() &gt; 0)
          for (int x = 0; x &lt; Math.min(mIntermediateMat.cols(), 10); x++) {
              double vCircle[] = mIntermediateMat.get(0, x);
  
              if (vCircle == null)
                  break;
  
              pt.x = Math.round(vCircle[0]);
              pt.y = Math.round(vCircle[1]);
              radius = (int) Math.round(vCircle[2]);
              // draw the found circle
              Core.circle(mRgba, pt, radius, colorRed, iLineThickness);
      }
  </code></pre>
  
  <p>method one gives fair result in case of coin detection and method two gives better result </p>
  
  <p>Out of these two methods second method processing is slow but results are good </p>
  
  <p>Both of these methods are working when camera frmae is caputured using JavaCameraView or NativeCameraView from opencv library .</p>
  
  <p>If I use same procedure on image captured from android naive image capture intent which returns Bitmap , I am unable to get any results at all i.e. no circles are detected at all.</p>
  
  <p>In methods one sometimes I get circle detected when using Bitmap captured using android camera intent.</p>
  
  <p>I also tried changing the captured bitmap as suggested in this <a href="https://stackoverflow.com/questions/17390289/convert-bitmap-to-mat-after-capture-image-using-android-camera">Post </a> but still no circle detection.</p>
  
  <p>Can anybody tell me what modifications I have to do.</p>
  
  <p>And also I want to know which algorithm will give better results in coin ( circle ) detection but with less processing.</p>
  
  <p>I have played with various values of houghCircle method and also tried canny edge out put as intput to houghCircles but its not considerably good enough.</p>