1

我想读取具有统一灰色背景的 JPEG 图像,上面有几个相同大小的彩色球。我想要一个可以拍摄这张照片并记录每个球坐标的程序。最好的方法是什么?

4

2 回答 2

2

我同意詹姆斯的观点。我曾经使用以下程序在图像中查找红框(在大多数红框被社区重新着色之前):

/**
 * @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);
                    }
                }
            }
        }
    }
}

可能会给你一些提示。图片来源于这里

于 2009-06-26T19:52:44.567 回答
0

您可以使用ImageIO库通过其中一种 read() 方法读取图像。这会产生一个BufferedImage,您可以分析它以获取单独的颜色。getRGB() 可能是最好的方法。如果需要,您可以将其与Color对象的 getRGB() 进行比较。这应该足以让你开始。

于 2009-06-26T19:50:29.580 回答