views:

1006

answers:

2

I want to read in a jpeg image with a uniform gray background with several colored balls on it of the same size. I want a program which can take this image and record the coordinates of each ball. whats the best way to do this?

A: 

You can use the ImageIO library to read in an image by using one of the read() methods. This produces a BufferedImage which you can analyze for the separate colors. getRGB() is probably the best way to do this. You can compare this to the getRGB() of a Color object if you need to. That should be enough to get you started.

James
+1  A: 

I agree with James. I used the following program once to find red boxes in an image (before most of the red boxes were recolored by the community):

/**
 * @author karnokd, 2008.11.07.
 * @version $Revision 1.0$
 */
public class RedLocator {
    public static class Rect {
        int x;
        int y;
        int x2;
        int y2;
    }
    static List<Rect> rects = new LinkedList<Rect>();
    static boolean checkRect(int x, int y) {
        for (Rect r : rects) {
            if (x >= r.x && x <= r.x2 && y >= r.y && y <= r.y2) {
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) throws Exception {
        BufferedImage image = ImageIO.read(new File("fallout3worldmapfull.png"));
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                int c = image.getRGB(x,y);
                int  red = (c & 0x00ff0000) >> 16;
                int  green = (c & 0x0000ff00) >> 8;
                int  blue = c & 0x000000ff;
                // check red-ness
                if (red > 180 && green < 30 && blue < 30) {
                    if (!checkRect(x, y)) {
                        int tmpx = x;
                        int tmpy = y;
                        while (red > 180 && green < 30 && blue < 30) {
                            c = image.getRGB(tmpx++,tmpy);
                            red = (c & 0x00ff0000) >> 16;
                            green = (c & 0x0000ff00) >> 8;
                            blue = c & 0x000000ff;
                        }
                        tmpx -= 2;
                        c = image.getRGB(tmpx,tmpy);
                        red = (c & 0x00ff0000) >> 16;
                        green = (c & 0x0000ff00) >> 8;
                        blue = c & 0x000000ff;
                        while (red > 180 && green < 30 && blue < 30) {
                            c = image.getRGB(tmpx,tmpy++);
                            red = (c & 0x00ff0000) >> 16;
                            green = (c & 0x0000ff00) >> 8;
                            blue = c & 0x000000ff;
                        }
                        Rect r = new Rect();
                        r.x = x;
                        r.y = y;
                        r.x2 = tmpx;
                        r.y2 = tmpy - 2;
                        rects.add(r);
                    }
                }
            }
        }
    }
}

Might give you some hints. The image originates from here.

kd304