同じサイズの色付きのボールがいくつかある均一な灰色の背景を持つJPEG画像を読み込みたいです。この画像を取得して、各ボールの座標を記録できるプログラムが必要です。これを行う最善の方法は何ですか?
mono
質問する
4163 次
2 に答える
2
私はジェームズに同意します。次のプログラムを 1 回使用して、画像内の赤いボックスを見つけました (コミュニティによってほとんどの赤いボックスの色が変更される前に)。
/**
* @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
read() メソッドの 1 つを使用して、 ImageIOライブラリを使用してイメージを読み込むことができます。これにより、別々の色を分析できるBufferedImageが生成されます。getRGB() はおそらくこれを行うための最良の方法です。必要に応じて、これをColorオブジェクトの getRGB() と比較できます。それはあなたが始めるのに十分なはずです.
于 2009-06-26T19:50:29.580 に答える