Unable to recognize blob for every color

edited October 2013 in Library Questions

Hi, I have used Blobscanner library for blob recognition. This library works fine and don't have any problem with it but here I have the issue with using it properly. Actually, what I am trying to do is to get the blob of drawn stroke. I have used two PGraphics layer, one for drawing blob and other for canvas drawing. Problems are when ever I tried to draw with random color it doesn't recognize the blob for every color. Also programs gets slow if so many blob gets detected. Third problem is drawing centroid and getting the pixel color at centroid. It shows very different values like I shown in the picture below

image

        import Blobscanner.*;
        PGraphics edges;
        PGraphics testLayer;
        Detector bs;
        int minimumWeight = 900;
        final int MAXEDGES = (320*240)/4; 
        int[] X ;
        int[] Y ;
        color c;
        void setup() {
          size(500, 500);
          bs = new Detector(this, 0, 0, width, height, 255);
          edges = createGraphics(width, height);
          testLayer = createGraphics(width, height);
          testLayer.beginDraw();
          testLayer.endDraw();
        }
        void draw() {
          background(255);
          if (keyPressed) {
            c = color(random(0, 255), random(0, 255), random(0, 255));
          }
          //---------------------------------
          testLayer.beginDraw();
          if (mousePressed) {
            //    testLayer.fill(c);
            //    testLayer.noStroke();
            testLayer.stroke(c);
            testLayer.strokeWeight(20);
            testLayer.line(mouseX, mouseY, pmouseX, pmouseY);
            //testLayer.ellipse(mouseX, mouseY, 30, 30);
          }
          testLayer.endDraw();
          //---------------------------------
          int k = 0;
          bs.imageFindBlobs(testLayer);
          bs.loadBlobsFeatures();
          bs.weightBlobs(false);
          bs.findCentroids(false, false);
          getEdgeCoordinates();
          edges.beginDraw();
          for (int i = 0; boolean( X[i]); i++) {
            edges.stroke(0, 255, 0);
            edges.strokeWeight(3);
            edges.point(X[i], Y[i]);//or do what you want
          } 
          //--------------------------------------------------
          bs.findCentroids(false, false);
          for (int i = 0; i < bs.getBlobsNumber(); i++) {
            edges.point(bs.getCentroidX(i), bs.getCentroidY(i));
            color c = testLayer.get((int)bs.getCentroidX(i), (int)bs.getCentroidY(i));
            float r = red(c);
            float g = green(c);
            float b = blue(c);
            println("r: " + r + " g: " + g + " b: " + b);
          }
          //--------------------------------------------------
          edges.endDraw();
          image(testLayer, 0, 0);
          image(edges, 0, 0);
        }



        void  getEdgeCoordinates() { 
          X = new int[MAXEDGES];
          Y = new int[MAXEDGES];
          int i = 0;
          for (int y = 0; y < testLayer.height; y++) {
            for (int x = 0; x < testLayer.width; x++) {
              if (bs.isEdge(x, y)  && 
                bs.getBlobWeightLabel(bs.getLabel(x, y)) >=  minimumWeight) {
                X[i] = x;
                Y[i] = y;
                i++;
              }
            }
          }
        }

Answers

  • edited October 2013

    For those who want Answer ....

              import Blobscanner.*;
              PGraphics edges; // draw blob on this layer
              PGraphics testLayer; // drawing canvas: user draws here
              PImage img, colimg;
              Detector bs; // blob object for blob detection
              int minimumWeight = 10;
              final int MAXEDGES = (500*500)/4; 
              int[] X ;
              int[] Y ;
              color c;
              int R, G, B;
              float rprev, gprev, bprev;
              void setup() {
                size(500, 500);
                bs = new Detector(this, 0, 0, width, height, 255);  // initialaizing blob detector
                //----------------------------------------------------
                edges = createGraphics(width, height);  // creating blob detector layer
                testLayer = createGraphics(width, height); // creating canvas layer
                testLayer.beginDraw(); // initialaizing blank canvas layer otherwise it will throw null point exception
                testLayer.endDraw();
              }
              void draw() {
                background(255);
                //--------- RANDOM COLOR GENERATOR ---------
                if (keyPressed) {
                  c = color(random(255), random(255), random(255)); // random color
                }
                //-------------------------------------------
                fill(c);
                noStroke();
                ellipse(mouseX, mouseY, 10, 10);
                testLayer.beginDraw();
                if (mousePressed) {
                  //    testLayer.fill(c);
                  //    testLayer.noStroke();
                  testLayer.stroke(c);
                  testLayer.strokeWeight(10);
                  testLayer.line(mouseX, mouseY, pmouseX, pmouseY);
                  //testLayer.ellipse(mouseX, mouseY, 30, 30);
                }
                testLayer.endDraw();
                //---------------------------------
                img = testLayer.get(0, 0, width, height);
                //---------------------------------
                int k = 0;
                colimg = img.get(); // save color image in colimg for 
                img.filter(THRESHOLD, 0.3); // covnert image in greyscale so that it can detect all the blobs
                bs.imageFindBlobs(img); // blob fiding in testLayer PGraphics 
                bs.loadBlobsFeatures();
                bs.weightBlobs(false); // weight of the blob is false. you can set it true and set the weight threshold
                getEdgeCoordinates();
                edges.beginDraw();
                for (int i = 0; boolean( X[i]); i++) {
                  edges.stroke(0, 255, 0); 
                  edges.strokeWeight(1);
                  edges.point(X[i], Y[i]);//or do what you want
                } 
                //--------------------------------------------------
                bs.findCentroids(false, false);
                colimg.loadPixels();
                for (int i = 0; i < bs.getBlobsNumber(); i++) {
                  edges.point(bs.getCentroidX(i), bs.getCentroidY(i));
                  text("G:"+ i, bs.getCentroidX(i), bs.getCentroidY(i));
                  color c = colimg.pixels[(int)bs.getCentroidX(i) + width*((int)bs.getCentroidY(i))]; // getting blob color from testLayer
                  float r = red(c); // getting r values of color c
                  float g = green(c); // getting g values of color c
                  float b = blue(c); // getting b values of color c
                  println("R:" + r + " G:" + g + " B:" +b);
                  //------RED CHANNEL ---------------------
                  if (max(r, g, b)==r) {
                    if (r!=rprev) {
                      R = (int)random(0, 5);
                      rprev = r;
                      println("R:" + R + " rprev:" + rprev);
                    }
                  }
                  //------GREEN CHANNEL ---------------------
                  if (max(r, g, b)==g) {
                    if (r!=rprev) {
                      G = (int)random(5, 10);
                      gprev = g;
                      println("G:" + G + " gprev:" + gprev);
                    }
                  }
                  //------BLUE CHANNEL ---------------------
                  if (max(r, g, b)==b) {
                    if (r!=rprev) {
                      B = (int)random(10, 16);
                      bprev = b;
                      println("B:" + B + " bprev:" + bprev);
                    }
                  }
                  //------END ---------------------
                }
                //--------------------------------------------------
                edges.endDraw();
                image(colimg, 0, 0);
                image(edges, 0, 0);
              }
              //----------------------------------------------------
              void  getEdgeCoordinates() { 
                X = new int[MAXEDGES];
                Y = new int[MAXEDGES];
                int i = 0;
                for (int y = 0; y < img.height; y++) {
                  for (int x = 0; x < img.width; x++) {
                    if (bs.isEdge(x, y)  && 
                      bs.getBlobWeightLabel(bs.getLabel(x, y)) >=  minimumWeight) {
                      X[i] = x;
                      Y[i] = y;
                      i++;
                    }
                  }
                }
              }
    
Sign In or Register to comment.