Logic problem !

edited November 2013 in Programming Questions

Hello, I have wrote this program for blob detection. It generates a new number whenever a color blob gets detected. It seems everything working fine but some how it is not generating numbers properly. The problem is when a color blob gets detected then according to the blob color a number should generate and number remains unchanged until it detects the another blob. For example. whenever a red color oriented (max(r,g,b)==r) blob gets detected it should generate a random number and remain unchanged until it detects another red oriented color blob whose value is different from the current one but here once it detects the red oriented color blob then it keeps generating the random whether it detects another red color oriented blob or not.

        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();
          img.filter(THRESHOLD, 0.3);
          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);
            if (max(r, g, b)==r) {
              if (r!=rprev) {
                R = (int)random(0, 5);
                println("R:" + R);
              }
              else {
                rprev = r;
                println("rprev");
              }
            }
            if (max(r, g, b)==g) {
              if (g!=gprev) {
                G = (int)random(5, 10);
                println("G:" + G);
              }
              else {
                gprev = g;
                println("gprev");
              }
            }
            if (max(r, g, b)==b) {
              if (b!=bprev) {
                B = (int)random(10, 16);
                println("B:" + B);
              }
              else {
                bprev = b;
                println("bprev");
              }
            }
          }
          //--------------------------------------------------
          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++;
              }
            }
          }
        }

Answers

  • Answer ✓

    haven't run code, don't have BlobDetector, but

    The problem is when a color blob gets detected then according to the blob color a number should generate and number remains unchanged until it detects the another blob.

    code says

    if (r!=rprev) {
        R = (int)random(0, 5);
        println("R:" + R);
    }
    

    don't you need to change rprev here (rather than in the else clause)? next time through the loop this will still be true (it's in the same blob, r is still different from rprev) so it generates another (different) value for R.

    also, the else clause

    else {
        rprev = r;
    }
    

    doesn't make sense - it'll only execute if r == rprev. so why set rprev to r when it already is?

  • edited October 2013

    Koogs, silly me ... :D :D... I did the changes and problem is still there but now when I draw for the first time it does exactly as it is expected but after one trial it starts again generating random number and doesn't remain unchanged.

    Here is the link of blobscanner library please help it is urgent and after two three trials it shows "Array out of the box error" .. please help me .... :(( :((

  • edited October 2013

    Here is the code which suppose to work like this: When I draw any stroke it should detect the stroke and generate a number and remain unchanged until it detects another blob. For example. whenever a red color oriented (max(r,g,b)==r) blob gets detected it should generate a random number between 0-5 and remain unchanged until it detects another red oriented color blob whose value is different from the current one but here once it detects the red oriented color blob then it keeps generating the random whether it detects another red color oriented blob or not. similar for other colors.

    Here is the code ..

                            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.