Java 读取JPEG图像并计算图像坐标

Java 读取JPEG图像并计算图像坐标,java,jpeg,Java,Jpeg,我想读一个JPEG图像,背景是均匀的灰色,上面有几个大小相同的彩色球。我想要一个程序,可以采取这一形象,并记录每个球的坐标。执行此操作的最佳方法是什么?您可以使用read()方法之一使用库读取图像。这将生成一个可以分析单独颜色的颜色。getRGB()可能是最好的方法。如果需要,可以将其与对象的getRGB()进行比较。这应该足够让你开始了。我同意詹姆斯的观点。我曾经使用以下程序在图像中查找红框(在社区重新收集大多数红框之前): /** *@author karnokd,2008.11.07。 *

我想读一个JPEG图像,背景是均匀的灰色,上面有几个大小相同的彩色球。我想要一个程序,可以采取这一形象,并记录每个球的坐标。执行此操作的最佳方法是什么?

您可以使用read()方法之一使用库读取图像。这将生成一个可以分析单独颜色的颜色。getRGB()可能是最好的方法。如果需要,可以将其与对象的getRGB()进行比较。这应该足够让你开始了。

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

/**
*@author karnokd,2008.11.07。
*@version$Revision 1.0$
*/
公共类RedLocator{
公共静态类Rect{
int x;
int-y;
int-x2;
int y2;
}
静态列表rects=newlinkedlist();
静态布尔checkRect(整数x,整数y){
for(Rect r:rects){
如果(x>=r.x&&x=r.y&&y>16;
绿色整数=(c&0x0000ff00)>>8;
int-blue=c&0x000000ff;
//检查红色度
如果(红色>180和绿色<30和蓝色<30){
if(!checkRect(x,y)){
int-tmpx=x;
int-tmpy=y;
而(红色>180和绿色<30和蓝色<30){
c=image.getRGB(tmpx++,tmpy);
红色=(c&0x00ff0000)>>16;
绿色=(c&0x0000ff00)>>8;
蓝色=c&0x000000ff;
}
tmpx-=2;
c=image.getRGB(tmpx,tmpy);
红色=(c&0x00ff0000)>>16;
绿色=(c&0x0000ff00)>>8;
蓝色=c&0x000000ff;
而(红色>180和绿色<30和蓝色<30){
c=image.getRGB(tmpx,tmpy++);
红色=(c&0x00ff0000)>>16;
绿色=(c&0x0000ff00)>>8;
蓝色=c&0x000000ff;
}
Rect r=新的Rect();
r、 x=x;
r、 y=y;
r、 x2=tmpx;
r、 y2=tmpy-2;
加法(r);
}
}
}
}
}
}

可能会给您一些提示。图像来源于。

读取JPEG图像和对其执行图像识别是两个不同层次上的完全不同的问题。同意。我建议您的问题使用不同的标题。
/**
 * @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);
                    }
                }
            }
        }
    }
}