【问题标题】:java - How do I find the extents of an object within a BufferedImagejava - 如何在 BufferedImage 中找到对象的范围
【发布时间】:2020-10-02 05:46:54
【问题描述】:

所以我试图在 png 图像中找到对象的上、下、左和右范围。图像具有透明背景和中间的黑色圆圈。如何找到圆的上、下、左和右范围(全黑),从而将圆与透明背景隔离?

到目前为止我尝试了什么:

    public static BufferedImage trimImage(BufferedImage image) {
    int left = 0, right = 0, up = 0, down = 0;
    
    for(int i = 0; i<image.getHeight(); i++) {
        for(int j = 0; j<image.getWidth(); j++) {
            if(image.getRGB(j, i)==0) {
                up = i;
                break;
            }
        }
    }
    
    for(int i = 0; i<image.getWidth(); i++) {
        for(int j = 0; j<image.getHeight(); j++) {
            if(image.getRGB(i, j)==0) {
                left = i;
                break;
            }
        }
    }
    
    for(int i = 0; i<image.getHeight(); i++) {
        for(int j = 0; j<image.getWidth(); j++) {
            if(image.getRGB(j, i)==0&&i>down) {
                down = i;
            }
        }
    }
    
    for(int i = 0; i<image.getWidth(); i++) {
        for(int j = 0; j<image.getHeight(); j++) {
            if(image.getRGB(i, j)==0&&i>down) {
                right = i;
            }
        }
    }
    
    System.out.println(up+","+down+","+left+","+right);
    BufferedImage temp = image.getSubimage(left, up, right-left, down-up);
    return temp;
}

你知道更简单的方法吗?这给了我以下错误:

Exception in thread "main" java.awt.image.RasterFormatException: (x + width) is outside raster

感谢任何帮助。谢谢!

编辑:这是完整的 stackTrace():

Exception in thread "main" java.awt.image.RasterFormatException: (x + width) is outside raster
at sun.awt.image.IntegerInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at main.ImageHandler.trimLetter(ImageHandler.java:114)
at main.HandwritingReader.<init>(HandwritingReader.java:48)
at main.HandwritingReader.main(HandwritingReader.java:55)

【问题讨论】:

  • 异常从何而来?您能否添加完整的堆栈跟踪?我会在 x 轴上用 x 代替 i,在 y 轴上用 y 代替 j。 :)
  • 是的,我确实尝试改变 x 和 y 的位置,反之亦然,交换两者,以及 getWidth() 和 getHeight()。不工作。这是堆栈跟踪:线程“main”中的异常 java.awt.image.RasterFormatException: (x + width) is outside raster at sun.awt.image.IntegerInterleavedRaster.createWritableChild(Unknown Source) at java.awt.image.BufferedImage。 getSubimage(Unknown Source) at main.ImageHandler.trimLetter(ImageHandler.java:114) at main.HandwritingReader.(HandwritingReader.java:48) at main.HandwritingReader.main(HandwritingReader.java:55)
  • 您的堆栈跟踪确实属于您未提及的其他类。
  • 我在其他类中所做的唯一一件事就是为图像调用此函数。这是对该函数的唯一引用: for(int i = 0; i
  • 你可以用一个嵌套循环来做到这一点。看起来您的前两个嵌套循环“正常”,但后两个在逻辑上存在错误。

标签: java bufferedimage


【解决方案1】:

考虑左是最低的x坐标,上是最低的y坐标,右是最高的x坐标,下是最高的y坐标。

int left = Integer.MAX_VALUE;
int right = 0;
int top = Integer.MAX_VALUE;
int bottom = 0;

for(int i = 0; i<image.getWidth(); i++) {
    for(int j = 0; j<image.getHeight(); j++) {
        if(image.getRGB(i, j)==0) {
            left = Math.min(left, i);
            right = Math.max(right, i);
            top = Math.min(top, j);
            bottom = Math.max(bottom, j);
        }
    }
}

这应该会在循环完成时为您提供界限。

我制作了一个示例图像并尝试了它。

        BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.BLACK);
        g.fillOval(25, 25, 32, 32);
        g.dispose();

在这种情况下,我必须检查 image.getRGB() != 0 是否因为透明像素为 0,而黑色像素的 alpha 值为 255。

【讨论】:

    猜你喜欢
    • 2011-04-25
    • 2014-02-12
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 2017-06-01
    相关资源
    最近更新 更多