Updating Ids based on location in HashMap

edited October 2013 in Programming Questions

Hi, I am trying to update blobs ids based on their old location in HashMap. I am doing this because I want to avoid rechecking the old blobs for some calculation such as getting the blob color of current blob and generating some random values based on the colors.

Basically blobscanner return the blob ids based on their location in Y direction (Top -bottom) but I don't want this I want it to keep the old id for the old blob and generate new id for new blob independent of the blob location.

Here the problem is HashMap is not updating the ids. Let me tell you this is first time when I am trying to use HashMaps.

      import Blobscanner.*;
      import java.util.*;
      HashMap<PVector, Integer> hm;
      PGraphics testLayer; 
      PImage img, colimg;
      Detector bd;
      color c;
      int R, G, B;
      int lastCol = 255;
      int xt, yt;
      void setup() {
        size(500, 500);
        testLayer = createGraphics(width, height);
        testLayer.beginDraw(); 
        testLayer.endDraw();
        testLayer = createGraphics(width, height);
        testLayer.beginDraw(); 
        testLayer.endDraw();
        bd = new Detector( this, 0, 0, width, height, 255 );
        hm = new HashMap<PVector, Integer>();
      }
      int prev; 
      void draw() {
        background(255);
        //--------- RANDOM COLOR GENERATOR ---------
        if (keyPressed) {
          c = color(random(255), random(255), random(255));
        }
        //-------------------------------------------
        fill(c);
        noStroke();
        ellipse(mouseX, mouseY, 10, 10);
        testLayer.beginDraw();
        if (mousePressed) {
          testLayer.stroke(c);
          testLayer.strokeWeight(10);
          testLayer.line(mouseX, mouseY, pmouseX, pmouseY);
        }

        //-----------------PILOT CODE---------------------
        //------------------------------------------------
        testLayer.endDraw();
        //---------------------------------
        img = testLayer.get(0, 0, width, height);
        //---------------------------------
        colimg = img.get();
        img.filter(THRESHOLD, 0.3);
        img.loadPixels();
        bd.imageFindBlobs(img);
        bd.loadBlobsFeatures();
        bd.weightBlobs(false);
        //------------------------------------------------
        //  for (int i = 0; i < bd.getBlobsNumber(); i++) {
        //    PVector[] pixloc = bd.getBlobPixelsLocation(i);
        //    xt = (int)pixloc[0].x;
        //    yt = (int)pixloc[0].y;
        //  }

          for (int i = 0; i < bd.getBlobsNumber(); i++) {
            PVector[] pixloc = bd.getBlobPixelsLocation(i);
            PVector loc = new PVector(pixloc[0].x, pixloc[0].y);
            hm.put(loc, bd.getLabel(i));
          }

        if(!flag)
        for (int i = 0; i < bd.getBlobsNumber(); i++) {
          PVector[] pixloc = bd.getBlobPixelsLocation(i);
          PVector loc = new PVector(pixloc[0].x, pixloc[0].y);
          if (  hm.get(loc)!=bd.getLabel(i) ) {
            hm.put(loc, bd.getLabel(i));
          }
        }

        image(colimg, 0, 0);

        for (int i = 0; i < bd.getBlobsNumber(); i++) {
          PVector[] tempLoc = bd.getBlobPixelsLocation(i);
          text(bd.getLabel(i), tempLoc[0].x, tempLoc[0].y);
        }
      }
      boolean flag=true;
      void mouseDragged() {
        flag=true;
      }

      void mouseReleased() {
        flag= false;
      }
Sign In or Register to comment.