我一直在尝试使用libgdx在像素图中找到给定颜色的边界矩形。我找到了这个答案https://stackoverflow.com/a/62911971/14895996如何在位图中找到给定颜色的边界矩形?并尝试将代码转换为java。
到目前为止,我拥有的是:
public class Magik {
Array<Rectangle> bounds = new Array<>();
Array<Vector2> points = new Array<>();
Rectangle tempRect = new Rectangle();
Color compare = new Color();
Color color = new Color();
Pixmap pixmap = null;
public Rectangle getBoundingRectangle() {
int curX = (int) points.first().x;
int curY = (int) points.first().y + 1;
int maxX = (int) arrayMax( points ).x;
for( int y = curY; y < pixmap.getHeight(); y++ )
for( int x = curX; x <= maxX; x++ ) {
if ( color.equals(compare) )
points.add( new Vector2( x, y ) );
else
break;
}
Vector2 p1 = points.first();
Vector2 p2 = points.peek();
return new Rectangle( p1.x, p1.y, p2.x - p1.x, p2.y - p1.y );
}
public Array<Rectangle> getBounds( Pixmap pixmap, Color color ) {
this.color = color;
this.pixmap = pixmap;
for( int y = 0; y < pixmap.getHeight(); y++ ) {
for( int x = 0; x < pixmap.getWidth(); x++ ) {
compare = new Color( pixmap.getPixel( x, y) );
if ( color.equals( compare ) ) {
Vector2 p = new Vector2( x, y );
boolean found = false;
for ( Rectangle rect : bounds ) {
if ( tempRect.set( rect.x - 2, rect.y - 2, rect.width + 4, rect.height + 4 ).contains( p ) ) {
found = true;
break;
}
}
if ( !found ) {
points.add( p );
}
}
}
if( points.size > 0 ) {
tempRect = getBoundingRectangle();
bounds.add( new Rectangle( (int) tempRect.x, (int) tempRect.y, (int) tempRect.width, (int) tempRect.height ) );
points.clear();
}
}
System.out.println( "new" );
for (Rectangle corridorBounds : bounds) {
System.out.println( corridorBounds );
}
return bounds;
}
}
忘记在getBoundingRectangle((方法上设置像素颜色。这应该可以做到,而且现在运行良好:
public class Magik {
Array<Rectangle> bounds = new Array<>();
Array<Vector2> points = new Array<>();
Rectangle tempRect = new Rectangle();
Color compare = new Color();
Color color = new Color();
Pixmap pixmap = null;
public Rectangle getBoundingRectangle() {
int curX = (int) points.first().x;
int curY = (int) points.first().y + 1;
int maxX = (int) arrayMax( points ).x;
for( int y = curY; y < pixmap.getHeight(); y++ )
for( int x = curX; x <= maxX; x++ ) {
color = new Color( pixmap.getPixel( x, y ) );
if ( color.equals(compare) )
points.add( new Vector2( x, y ) );
else
break;
}
Vector2 p1 = points.first();
Vector2 p2 = points.peek();
return new Rectangle( p1.x, p1.y, p2.x - p1.x, p2.y - p1.y );
}
public Array<Rectangle> getBounds( Pixmap pixmap, Color color ) {
this.color = color;
this.pixmap = pixmap;
for( int y = 0; y < pixmap.getHeight(); y++ ) {
for( int x = 0; x < pixmap.getWidth(); x++ ) {
compare = new Color( pixmap.getPixel( x, y) );
if ( color.equals( compare ) ) {
Vector2 p = new Vector2( x, y );
boolean found = false;
for ( Rectangle rect : bounds ) {
if ( tempRect.set( rect.x - 2, rect.y - 2, rect.width + 4, rect.height + 4 ).contains( p ) ) {
found = true;
break;
}
}
if ( !found ) {
points.add( p );
}
}
}
if( points.size > 0 ) {
tempRect = getBoundingRectangle();
bounds.add( new Rectangle( (int) tempRect.x, (int) tempRect.y, (int) tempRect.width, (int) tempRect.height ) );
points.clear();
}
}
System.out.println( "new" );
for (Rectangle corridorBounds : bounds) {
System.out.println( corridorBounds );
}
return bounds;
}
}