Increase the number of circles detected in an image using the BlobDetection library

I have been trying to increase the maximum number of blobs detected with the BlobDetection library from the default 1000 to 2000.

I can successfully detect up to 1000 blobs/circles, can read their x and y position and size, but for the scope of my project I would like to read an higher number of circles/blobs.

I had a look in the library and found that there’s a set amount of maximum blobs that can be detected, and that amount has been set to 1000.

Then found some documentation online here: http://www.v3ga.net/processing/BlobDetection/index-page-documentation.html#top

the bits that interests me I believe to be :
_static void setConstants(int blobMaxNb, int blobLinesMaxNb, int blobTrianglesMaxNb) _
Will set all the constants for memory allocation for different objects used by all instances of BlobDetection class:
# blobMaxNb is the maximum number of blobs that an instance of BlobDetection can store. (default value is 1000)
# blobLinesMaxNb is the maximum number of edges (“lines”) a single blob can store. (default value is 4000 )
# blobTrianglesMaxNb* is the maximum number of triangles a single blob can store. (default value is 500 )

so in my code I have been trying to add the setConstant, and change it but:
1# I cannot increase the maximum number of blob detected
2# I get a “warning” message in my code saying that “The static method setConstants(int,int,int) from the type BlobDetection should be accessed in a static way”.

I have tried several different ways to put the setConstants in my sketch and looked up online for static void, access static void, static variables, but have been unsuccessful so far.

here my code below, I have tried to clean it up as much as possible and leave as many comments as possible for readability


import blobDetection.*;

BlobDetection theBlobDetection;
PGraphics img; // this is to create my own image
//PImage img; // this is to test premade image

PrintWriter output; // to output to txt file

int counter; // to count the number of circles 

int circlesNumber = 1500; // to define how many circles I want to draw when creating my own image

int blobMaxNb = 2000; // here I am trying to increase the max number of blobs detected from the default 1000 to 2000 
int blobLinesMaxNb = 4000; // same as library default value
int blobTrianglesMaxNb = 500; // same as library default value

void setup() {
  size (880, 680);

  img = createGraphics(width, height);
  //img = loadImage("dots_11.png");

  output = createWriter("Circle_Detection.txt");   

  // DRAW IMAGE WITH CIRCLES
  img.beginDraw();
  img.background(0);
  img.noStroke();
  img.fill(255);

  int minRandom = 2;
  int maxRandom = 6;
  int edge = maxRandom;

  for (int i=0; i<circlesNumber; i++) {
    img.fill(random(175, 255));
    int r = (int)random(minRandom, maxRandom);
    img.ellipse(random(edge, img.width-edge ), random(edge, img.height-edge), r, r);
  }
  img.endDraw();


  theBlobDetection = new BlobDetection(img.width, img.height);
  theBlobDetection.setPosDiscrimination(false);
  theBlobDetection.setThreshold(0.01);
  theBlobDetection.computeBlobs(img.pixels);

  // testing this, I am trying tio increase the max number of blobs detected from the default 1000 to 2000
  theBlobDetection.setConstants(blobMaxNb, blobLinesMaxNb, blobTrianglesMaxNb); //
  // error message:
  // "The static method setConstants(int,int,int) from the type BlobDetection should be accessed in a static way"
}

void draw() {
  image(img, 0, 0, width, height);

  drawBlobsAndEdges(true, true);

  output.close(); // Finishes the txt file
  //
  noLoop();
}

void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges) {
  noFill();
  Blob b;
  EdgeVertex eA, eB;
  for (int n=0; n<theBlobDetection.getBlobNb(); n++) {
    b=theBlobDetection.getBlob(n);
    if (b!=null) {

      // COUNT CIRCLES 
      counter = counter + 1;
      // and READ POSITION and SIZE, convoluted but works
      float temp = ((b.yMin-1)* -1);
      float yTD = map(temp, 0, 1, 0, 1);
      float relSize = ((b.w+b.h)/2);
      println(nf(counter, 3, 0), nf(b.xMin, 0, 5), nf(yTD, 0, 5), nf(relSize, 0, 5));

      //PRINT TO txt file
      output.println(nf(counter+0, 3, 0) + " " + nf(b.xMin, 0, 5) + " " + nf(yTD, 0, 5) + " " + nf(relSize, 0, 5) + " "); // Write the coordinate to the file

      // Edges
      //
      if (drawEdges) {
        strokeWeight(0.5);
        stroke(0, 255, 0);
        for (int m=0; m<b.getEdgeNb(); m++) {
          eA = b.getEdgeVertexA(m);
          eB = b.getEdgeVertexB(m);
          if (eA !=null && eB !=null)
            line(eA.x*width, eA.y*height, 
              eB.x*width, eB.y*height);
        }
      }

      // Blobs
      if (drawBlobs) {
        // draw rect on circle location
        strokeWeight(1);
        stroke(255, 0, 0);
        rect(b.xMin*width, b.yMin*height, 
          b.w*width, b.h*height);
      }
    }
  }
}

I am aware that occasionally I am drawing two circles overlapping in my PGraphics img, and that they are detected as one circle, but I will deal with that separately.

any help to point me in the right direction would be appreciated!

What this means is that instead of:

theBlobDetection.setConstants(blobMaxNb, blobLinesMaxNb, blobTrianglesMaxNb);

you should be doing

BlobDetection.setConstants(blobMaxNb, blobLinesMaxNb, blobTrianglesMaxNb);

I didn’t have a chance to run your code but this modification should get you closer to your final solution.

Kf

Thanks Kf for looking into this,

I have just tried with your suggestion (and some variations on the theme) but it doesn’t seem to work. the sketch still stops counting blobs at 1000.

Ok, that will take a bit more of digging and testing. unfortunately I can’t help much today…

Kf

I have tried to approach this from another angle, trying to modify the library itself.

In the library, I have found the line that sets the max number of blobs. But then I am having trouble when repacking the library as .jar.

I have never modified a library before and did a bit of reading on that.
Looked up some tutorials online, some of which state that to create a .jar file all you need to do is rename a .zip file to .jar. But that doesn’t work for some reason. When I put the renamed .jar file in the processing library folder, processing tells me that it cannot find the library…

Now I have paused trying to modifying the library, and went back investigating how to increase the max number of blob detected via coding it in the sketch with the _static void setConstants(int blobMaxNb, int blobLinesMaxNb, int blobTrianglesMaxNb) _ line suggested by the author in the library documentation.

Any help of tips to point me in the right direction will be appreciated!