简体   繁体   English

如何在 Libgdx 或 Java 的像素图中找到给定颜色的边界矩形

[英]How to find the bounding rectangle(s) of a given color, within a pixmap in Libgdx or Java

I've been trying to find the bounding rectangle(s) of a given color, within a pixmap using libgdx.我一直在尝试使用 libgdx 在像素图中找到给定颜色的边界矩形。 I've found this answer https://stackoverflow.com/a/62911971/14895996 from a thread How to find the bounding rectangle(s) of a given color, withing a bitmap?我从线程How to find the bounding rectangle(s) of a given color, with a bitmap ? and tried converting the code into java.并尝试将代码转换为 java。

Here's what I have so far:这是我到目前为止所拥有的:

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;
    }
}

Forgot to set Pixel Color on getBoundingRectangle() method.忘记在 getBoundingRectangle() 方法上设置像素颜色。 This should do it, and it's working fine now:这应该可以做到,现在工作正常:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM