0

I am at wits end with this problem.

Please note I am a really novice coder (although I'm sure you will see by my code).

The basics:

I have an image that I would like to count the number of objects in. Objects in this instance are just joined up pixels (the image has been threshold-ed and undergone binarization and binary erosion to get to this stage, code not included).

My problem:

I am trying to write some code to count how many objects there are left in this image, and within that method I call another method which is meant to remove any objects that have already been included by searching for neighbouring pixels to which they are attached. However, my current implementation of this removal method is throwing up an error: "coordinates out of bounds". I'm asking for any help solving this issue.

Code for overall object counting:

      /**
   * countObjects in image
   * 
   * @param binary image to count objects in
   * @param original image to put labels on
   * 
   * @return labelled original image for graphics overlay
   */
  public static BufferedImage countObjects(BufferedImage image, BufferedImage original){
     BufferedImage target = copyImage(image);


      int rgbBand = 0;
      boolean finished = false;
      Graphics labelColour = original.getGraphics();
      labelColour.setColor(Color.RED);

      while(!finished){ 
            finished = false;
            for ( int i=0; i<= target.getRaster().getWidth() - 1; i++ ) {
                for( int j=0; j< target.getRaster().getHeight() - 1; j++ ) {
                    int  clrz  = target.getRaster().getSample(i, j, rgbBand);
                    if (clrz == 1) { 
                        System.out.println(clrz);
                        removeObject(i, j, target);
                        labelColour.drawString( ""+count, i, j);
                        finished=true;
                    } 
                }
            }
      }

          return original;

Code for object removal:

    /**
 * 
 * @param  x
 * @param  y
 * @param newImage
 * 
 */
  private static void  removeObject( int x, int y, BufferedImage newImage ){
      int rgbBand = 0;
      int[] zero = new int[] { 0 };
      newImage.getRaster().setPixel(x, y, zero); 
        for (int a = Math.max(0, x - 1); a <= Math.min(x + 1, newImage.getRaster().getWidth()); a++) {
            for (int b = Math.max(0, y - 1); b <= Math.min(y + 1, newImage.getRaster().getHeight()); b++) {
                    int na = a;
                    int nb = b;
                    if (newImage.getRaster().getSample(na, nb, rgbBand) == 1) {
                        removeObject( nc, nd, newImage );
                    }
            }
        }
  }       

In the above removeObject method, I am trying to use a recursive technique to remove pixel coordinates from the image being counted once either they or neighbouring pixels have been labelled.

If any of this is unclear (and I know there are probably more than a few confusing parts of my code, please ask and I will explain further).

Thanks for any help.

Sirrah
  • 1,681
  • 3
  • 21
  • 34
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Dec 11 '13 at 03:52

1 Answers1

1

I dont have enough reputation to comment hence writing my comment as answer . Are u sure u dint messed up with the x and y coordinates ?I had faced a similar problem some time ago ,but I had messed up with height and width of the image.

Nihar
  • 553
  • 1
  • 10
  • 29
  • Hi, thanks for pitching in. I have tried everything (I think) in that regard. In my code I have tried to safeguard against the coordinates going out of bounds. E.g. for (int b = Math.max(0, y - 1); b <= Math.min(y + 1, newImage.getRaster().getHeight()); b++) etc.. – Sirrah Dec 11 '13 at 04:07
  • u r using a binarised image, can u remove that math.max(0,y-1) and directly use 254,in both cases WRT x and y – Nihar Dec 11 '13 at 04:11
  • What do you mean by 254? (i.e. 255 -1?) x and y in the above code aren't rgb values but coordinates for pixels in the image which are being looped through. :) – Sirrah Dec 11 '13 at 04:14
  • ok,one more thing why r u using the values of i,j=1 in the while loop,why not 0 – Nihar Dec 11 '13 at 04:21
  • I think I was just testing to see if it made a difference. I have changed them back to 0 in my own code now. I will edit the question - thank-you for pointing it out :) – Sirrah Dec 11 '13 at 04:23
  • I have improvement although not from the changing 1 to 0. I changed the loops in removeObject to something simpler (but should be the same, really) e.g.: for ( int c = x - 1; c <= x + 1; c++) and the algorithm now works... sort of, but only if I configure the thresholding so that there are no pixels left that are touching the edges of the image. If the threshold is too low, and there are pixels touching the side of the image, the error is thrown up again. – Sirrah Dec 11 '13 at 04:34