title: How can white objects of random size be detected and counted using OpenCV in Android Studio
body: <p>I'm trying to make an app in Android Studio using OpenCV that basically looks at an image of a group of dots and extracts all of the bright dots and then counts them.  The dot shape is not consistent, but the color of the dots should be white with a black background.</p>
  
  <p>I start by uploading an image like this:
  <a href="https://i.stack.imgur.com/p4jpl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p4jpl.jpg" alt="Original Image"></a></p>
  
  <p>I then take the the bitmap of the image and convert it to gray scale.  Then I check that the gray values are within a a range and return a bitmap of binary values.  The returned image looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/wdtQ4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wdtQ4.jpg" alt="Processed Image"></a></p>
  
  <p>I've unsuccessfully tried a few different methods to count these dots including using Imgproc.findContours and following along tutorials such as <a href="https://stackoverflow.com/questions/24204779/counting-objects-better-way-to-filling-holes">this one</a> but I am not looking for a particular shape.  It is mostly just a grouping of 1 to 50 pixels at a time with rgb values 225, 255, 255 and irregular shapes.  How can these individual shapes be counted?</p>
  
  <p>Here is my image processing portion of the code, the countNonZero portion is just to give me an idea of how many white pixels there are, which is 179 for this particular image </p>
  
  <pre><code>public void convertToGray(View v){
      int whitePix;
      Mat Rgba = new Mat();
      Mat grayMat = new Mat();
      Mat dots = new Mat();
      BitmapFactory.Options o = new BitmapFactory.Options();
      o.inDither=false;
      o.inSampleSize=4;
  
      int width = imageBitmap.getWidth();
      int height = imageBitmap.getHeight();
  
      grayBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
  
  
      //bitmap to MAT
  
      Utils.bitmapToMat(imageBitmap,Rgba);
  
      Imgproc.cvtColor(Rgba,Rgba,Imgproc.COLOR_RGB2GRAY);
  
      Core.inRange(Rgba,scalarLow,scalarHigh,grayMat);
  
      whitePix = Core.countNonZero(grayMat);
  
      Utils.matToBitmap(grayMat,grayBitmap);
  
       MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), grayBitmap, "Result", "Descrip");
  
      mImageView.setImageBitmap(grayBitmap);
  
  
  
  }
  </code></pre>
  
  <p>This function is called when a button is clicked after an image has been uploaded to the app.</p>