Array out of bounds Exception error Any help !

edited October 2013 in Questions about Code

Hi, I got ArrayoutofBoundsException error after some trails. please help me. Since I don't have any problem with library so I posted it in programming question section.

What exactly it does: It recognizes the color of the stroke drawn on the canvas and detects it color components (R,G, B) values and generates a number which remains unchanged until a different color gets detected. Even this also not working.

**Error: ** ArrayOutofBoundsException: 259761 error

      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, (int)bs.getCentroidX(i), (int)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);
              rprev = r;
            }
          }

          if (max(r, g, b)==g) {
            if (g!=gprev) {
              G = (int)random(5, 10);
              println("G:" + G);
              gprev = g;
            }
          }   
          if (max(r, g, b)==b) {
            if (b!=bprev) {
              B = (int)random(10, 16);
              println("B:" + B);
              bprev = b;
            }
          }
        }
        //--------------------------------------------------
        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 ✓

    Can't run your sketch because I can't find the library you are using but from looking at the code I it might be line 7. I suggest you change this to

    final int MAXEDGES = (500*500);

    because if more than 25% of pixels result in getting-edge-coordinates then the X and Y arrays will be overflowing lines 106-108

  • edited October 2013

    @quark : Please click here for downloading library. Even though I have changed the line 7 but still getting the same error. it shows error on line line 64.

    Quark can you also explain me why it is not working as expected. I mean as I have explained in the above post that it should detect color of every stroke and generate a random number which should remain unchanged until it detects a different color.

    As in my code lines (69-91) shows that I have compared the components of detected color so that I can check whether detected color is green/ red/blue oriented and according to that random number gets generated.

  • I downloaded the library and ran your code without getting an error but it was not detecting any blobs.

    I changed line 15 to

    bs = new Detector(this, 0, 0, width, height, 0);

    that gave me one blob but no contours.

    I also suggest that you replace line 48

    bs.imageFindBlobs(img); // blob fiding in testLayer PGraphics

    with

    img.loadPixels();

    bs.findBlobs(img.pixels,img.width,img.height); // blob fiding in testLayer PGraphics

    based on what I saw in the source code for this library it should be a lot quicker.

    I remember looking at this library for someone else in the last forum, and I seem to remember that some methods had to be called before others. Just googled for it and found this post from a year ago it might give you some hints.

  • edited October 2013

    @quark Thanks a lot. Here, I am keep getting the error after 7-10 trials even sometimes before.

    I am using Processing 2.0.1 with blobscanner on windows 7 64bit Nvidia Gforce 8400 gfx card supported system. So when I run the above code it shows the error after some trials. I don't know why :-? :-? :-? it is not happening on your system. I'll check your sugestions but anyways can you check my other logical problem.

    Here is the VIDEO I have uploaded

    Problem 2: Problem of generating random number and which remain unchanged until it detects different color.

    **So what actually Problem is: **when I run it on my system it detects all blobs, contours and centroids. First trail of drawing colored stroke with mouse on canvas detected by the blobscanner and gives a random number and doesn't changed but as soon as you draw second stroke it changes to other number and keeps changing to other number and doens't remain constant.

  • edited October 2013

    Problem 2: Since I can't get the blob detection working then this is the best I can come up with. I have replaced the calls to red, green and blue with very fast bit operations where the colour is split up into 3 integer values in the range 0-255

      //--------------------------------------------------
      bs.findCentroids(false, false);
      colimg.loadPixels();
      int lastCol = 0; // transparent black not going to have this
      for (int i = 0; i < bs.getBlobsNumber(); i++) {
        edges.point(bs.getCentroidX(i), bs.getCentroidY(i));
        text("G:"+ i, (int)bs.getCentroidX(i), (int)bs.getCentroidY(i));
        int c = colimg.pixels[(int)bs.getCentroidX(i) + width*((int)bs.getCentroidY(i))]; // getting blob color from testLayer
        if (c != lastCol) {
          lastCol = c;
          int r = (c & 0x00ff0000) >> 16; // red channel (0-255)
          int g = (c & 0x0000ff00) >> 8;  // green channel (0-255)
          int b = (c & 0x000000ff);       // blue channel (0-255)
          int maxChannelValue = max(r, g, b);
          if (maxChannelValue == r) {
            R = (int)random(0, 5);
            println("R:" + R);
          }
          if (maxChannelValue == g) {
            G = (int)random(5, 10);
            println("G:" + G);
          }  
          if (maxChannelValue == b) {
            B = (int)random(10, 16);
            println("B:" + B);
          }
        }
      }
      //--------------------------------------------------
    
  • edited October 2013

    @quark thanks a lot ...... I think logic is correct but some how it is not stopping generating random numbers. It keeps changing after second trails but anyways ArrayOutofBoundsException and this problem is not getting resolve so I try myself.

  • edited October 2013

    @quark : Since you have changed the line 15 with bs = new Detector(this, 0, 0, width, height, 0); so its not gonna work. You need use the original code's line bs = new Detector(this, 0, 0, width, height, 255); because it doesn't detect the black color's blob. So when you want to detect the color blob then you need to draw with colored stroke so for this hit "any key" or simple "space key" twice or more to generate random colors as I have shown in the video.

    can you please last time check if you can help me.

    thanks

  • I just noticed the video link you posted earlier. When I ran the program all I got was black lines and no blobs so there is no way I can make progress.

    If you post the code that you used for the video I will have another look at it.

  • edited October 2013

    @quark: I think I understood the problem. Here this is what happening: when I draw for the first time it calculates the blob and its centroid position but as soon as I draw another stroke it sums up the current centroid position with the previous centroid position of the old blob and shows the total centroid position of the blob on the screen and when total value of the centroid position goes beyond the screen it shows error because it doesn't found any pixel out there [int c = colimg.pixels[(int)bs.getCentroidX(i) + width*((int)bs.getCentroidY(i))]; //]. And this happen because entire "testLayer" is processed by the loop every time and find old + new blob altogether.

    Similarly it keeps generating the random because testlayer gets processed by the blob detectors everytime in draw loop so it founds new blob with old color blobs and generates number which never remain constant and that is why when I draw for the first time there is one color on the screen and it show only one constant number but as soon as it founds another blob it generates the number for both the blobs old +new.

    So detecting the blob color with centroid is not a good idea so I thought of using PVector pix[ ] = bs.getBlobPixelsLocation(i);

    Now please help how to take it forward ?

    P.S. I have used the same code that I have posted above with changes you have given other than this bs = new Detector(this, 0, 0, width, height, 0);

Sign In or Register to comment.