HashMap中的Java HashMap。存储像素的所有坐标

HashMap中的Java HashMap。存储像素的所有坐标,java,hashmap,coordinates,Java,Hashmap,Coordinates,我对图像的像素分析有问题 我试图分析每一个白色的像素(R=255,G=255,B=255) 问题在于这些数据的存储/读取 for (int i = 0; i <= Map.getHeight(); i++) { for (int j = 0; j <= Map.getWidth(); j++) { if (Map.getColor(j, i).getBlue() == 255 && Map.getColor(j,

我对图像的像素分析有问题

我试图分析每一个白色的像素(R=255,G=255,B=255)

问题在于这些数据的存储/读取

for (int i = 0; i <= Map.getHeight(); i++) {
            for (int j = 0; j <= Map.getWidth(); j++) {
                if (Map.getColor(j, i).getBlue() == 255 && Map.getColor(j, i).getRed() == 255
                        && Map.getColor(j, i).getGreen() == 255)
                {
//                  coordsX = new HashMap<>();
                    coordsX.put(j, new Rectangle(j, i, 5, 5));


                }

            }
            coordsY.put(i, coordsX);
        }
        System.out.println();
    }

for(int i=0;i您每次发现一个新的白色像素时都会创建一个新的
coordsX
。这可能不是您想要的。因此,对于每个
y
都会有一个映射
coordsX
,只有一个条目,以前的条目都会被丢弃

此外,我还建议创建一个表示二维坐标的类,我们称之为
坐标
,这样您的算法就更容易实现了。(或者可能已经有了这样的东西,例如
?)

类坐标{
private int x,y;//加上getter、setter等。
公共int hashCode(){
返回Objects.hash(x,y);
}
公共布尔等于(对象obj){
如果(obj==此)
返回true;
如果(!(obj实例坐标))
返回false;
坐标表示=(坐标)obj;
返回this.x==that.x&&this.y==that.y;
}
公共字符串toString(){
返回“(“+x+”、“+y+”)”;
}
}
// ...
Map coords=newhashmap();

因为(int y=0;y你所说的是有意义的,因为你的代码
coordsX.put(j,新矩形(j,i,5,5));
将x坐标
j
矩形关联,而你的代码
coordsY.put(i,coordsX);
将最终的y坐标
i
与coordsX
哈希映射关联

但是,由于只与x或y值关联,如果将其放置到相同的坐标(使用
j
i
次),则每个
put()
调用都将覆盖。如果您打算将x/y对类(例如
Point2D
)的单个哈希映射映射映射到矩形,则可能会更好

您可以通过使用有关矩形的一些知识来进一步优化它。如果您想为图像的某些颜色存储一些x/y对,您可以使用一维数组,知道您可以将每个坐标索引为
x+y*width
,或者在您的情况下
j+i*Map.getWidth()
。我将设计您的数组“hashmap”像这样:

//将值放入数组中。
矩形[]坐标=新矩形[Map.getWidth()*Map.getHeight()];

对于(int i=0;我哇,谢谢:)这非常好。刚刚创建了坐标类并使用了您的调整。我认为在这个阶段我觉得有点太复杂了。我真的很感谢您的帮助:)亲爱的jabu,我只是尝试只获取一些矩形条目(性能原因)对于保存的坐标。但不幸的是,每次它都返回一个“null”而不是一个矩形。即使我尝试获取坐标的所有矩形。对于(int y=0;y),我测试了哈希映射中的矩形。但是你无法通过上面提到的for循环读取矩形,因为你的
实现可能等于(Object)
坐标
类中的
hashCode()
不正确。有关更多详细信息,请参阅。非常感谢。我无法检查,但我不熟悉equals an hashCode builder,因此无法在我的代码中实现它。我快速测试了它。再次感谢您的努力:)。我稍后也会尝试你的代码。但我注意到它与我刚实现的代码非常相似。谢谢你的帮助:)我明天会尝试这个
for (Entry<Integer, HashMap<Integer, Rectangle>> e : coordsY.entrySet()) {
            // HashMap<Integer, Rectangle> coordsX = coordsY.get(y);
            HashMap<Integer, Rectangle> coordsX = e.getValue();
            if (coordsX != null) {
                for (Entry<Integer, Rectangle> entry : coordsX.entrySet()) {
                    Rectangle rect = entry.getValue();
                    g.setColor(Color.red);
                    g.draw(rect);
                    if (this.car2.intersects(rect)) {
                        intersectsTrack = true;
                    }

                }
            }
        }
coordsX = new HashMap<>();
class Coordinate {
    private int x, y;  // plus getter, setter, etc.

    public int hashCode() {
        return Objects.hash(x, y);
    }

    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        else if (!(obj instanceof Coordinate))
            return false;

        Coordinate that = (Coordinate) obj;
        return this.x == that.x && this.y == that.y;
    }

    public String toString() {
        return "(" + x + ", " + y + ")";
    }
}

// ...

Map<Coordinate, Rectangle> coords = new HashMap<>();
for (int y = 0; y <= Map.getHeight(); y++) {
    for (int x = 0; x <= Map.getWidth(); x++) {
        Color color = Map.getColor(x, y);
        if (color.getBlue() == 255 && color.getRed() == 255 && color.getGreen() == 255) {
            Coordinate coordinate = new Coordinate(x, y);
            Rectangle rectangle = new Rectangle(x, y, 5, 5);
            coords.put(coordinate, rectangle);
        }
    }
}

for (Rectangle rectangle : coords.values()) {
    g.setColor(Color.red);
    g.draw(rect);
}
// put values in the array.
Rectangle[] coords = new Rectangle[Map.getWidth() * Map.getHeight()];
for (int i = 0; i <= Map.getHeight(); i++) {
    for (int j = 0; j <= Map.getWidth(); j++) {
        int index = j + i * map.getWidth();

        if (/* i,j has the correct color */) {
        coords[index] = new Rectangle(j, i, 5, 5);
        }
    }
}

// read values from the array
for (int i = 0; i <= Map.getHeight(); i++) {
    for (int j = 0; j <= Map.getWidth(); j++) {

        Rectangle rect = coords[j + i * Map.getWidth()];
        if(rect == null) continue;

        //Your read logic here
        g.setColor(Color.red);
        g.draw(rect);
        if (this.car2.intersects(rect)) {
            intersectsTrack = true;
        }
    }
}