簡體   English   中英

如何在 Libgdx 或 Java 的像素圖中找到給定顏色的邊界矩形

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

我一直在嘗試使用 libgdx 在像素圖中找到給定顏色的邊界矩形。 我從線程How to find the bounding rectangle(s) of a given color, with a bitmap ? 並嘗試將代碼轉換為 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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM