title: Find circles image processing in OpenCV/JaVa
body: <p>I'm using opencv and java to find circles on an image, I have the image below so far. I'm using Hough to find the circles with the code like this :</p>
  
  <pre><code>    public static Vector&lt;Mat&gt; circles(Mat img){
  
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
      long start_time = System.nanoTime();
      Imgproc.resize(img, img, new Size(450,250));
  
      Mat gray = new Mat();
      Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
      Imgproc.blur(gray, gray, new Size(3, 3));
  
      Mat edges = new Mat();
      int lowThreshold = 40;
      int ratio = 3;
      Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);
  
      Mat circles = new Mat();
      Vector&lt;Mat&gt; circlesList = new Vector&lt;Mat&gt;();
  
      Imgproc.HoughCircles(edges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 60, 200, 20, 30, 0 );
  
  
      System.out.println("#rows " + circles.rows() + " #cols " + circles.cols());
      double x = 0.0;
      double y = 0.0;
      int r = 0;
  
      for( int i = 0; i &lt; circles.rows(); i++ )
      {
        double[] data = circles.get(i, 0);
        for(int j = 0 ; j &lt; data.length ; j++){
             x = data[0];
             y = data[1];
             r = (int) data[2];
        }
        Point center = new Point(x,y);
        // circle center
        Core.circle( img, center, 3, new Scalar(0,255,0), -1);
        // circle outline
        Core.circle( img, center, r, new Scalar(0,0,255), 1);
  
       Imshow im1 = new Imshow("Hough");
       im1.showImage(img); 
  
       Rect bbox = new Rect((int)Math.abs(x-r), (int)Math.abs(y-r), (int)2*r, (int)2*r);
       Mat croped_image = new Mat(img, bbox);
       Imgproc.resize(croped_image, croped_image, new Size(160,160));
       circlesList.add(croped_image);
       Imshow m2 = new Imshow("cropedImage");
       m2.showImage(croped_image); 
      }
  
      long end_time = System.nanoTime();
      long duration = (end_time - start_time)/1000000;  //divide by 1000000 to get milliseconds.
      System.out.println("duration :  " + duration * 0.001 + " s"); 
      return circlesList;
  }
  </code></pre>
  
  <p>BUT it always detects only one circle.</p>
  
  <p><a href="https://i.stack.imgur.com/mTP9Z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mTP9Z.jpg" alt="enter image description here"></a></p>
  
  <p>My Question is how I can detect all the circles in an image using java/OpenCV ?</p>
  
  <p>Note:-</p>
  
  <p>1- I'm using Mat called circles in HoughCircles function parameters , because the function requires a Mat in Java.</p>
  
  <p>2- I'm using openCV 2.4.11 version.</p>