【问题标题】:Java Grayscale ImagesJava 灰度图像
【发布时间】:2016-01-12 07:07:33
【问题描述】:

一段时间以来,我一直致力于在 java 中对图像进行灰度化。我使用的是colorConvertOp,但似乎在处理了大量图像之后,JVM 最终会在操作中以锁定状态挂起。

现在我已经开始使用了:

BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_BYTE_GRAY);  
Graphics g = image.getGraphics();  
g.drawImage(img, 0, 0, null);  
g.dispose(); 

但是,我发现 CPU 出现了大幅峰值,以前低于 20%,现在已达到 120%。它似乎也导致了我的内存泄漏,并最终导致 OOMs。

有没有一种更简单、更快捷的方式在 Java 中进行灰度化,而无需使用尽可能多的 CPU/消除挂起的 JVM 错误?

【问题讨论】:

  • 需要多少时间?
  • 也许...您说的是一个错误,实际上是其中两个。你能给我们展示一个完整的崩溃示例吗?
  • Java 中的内存泄漏。使用垃圾收集器是不可能的......
  • 图片大小是多少,原始格式是什么,...如果数据不可用,结果很难重现。

标签: java performance grayscale


【解决方案1】:

我写了一个java程序将RGB图像转换为GrayScaleImage。希望这会有所帮助

public class GrayScale {

    BufferedImage image;
    int width;
    int height;

    public GrayScale() {

        try {
            File input = new File("input path of the image");
            image = ImageIO.read(input);
            width = image.getWidth();
            height = image.getHeight();

            for (int i = 0; i < height; i++) {

                for (int j = 0; j < width; j++) {

                    Color c = new Color(image.getRGB(j, i));
                    int red = (int) (c.getRed() * 0.299);
                    int green = (int) (c.getGreen() * 0.587);
                    int blue = (int) (c.getBlue() * 0.114);
                    Color newColor = new Color(red + green + blue,

                    red + green + blue, red + green + blue);

                    image.setRGB(j, i, newColor.getRGB());
                }
            }

            File ouptut = new File("output path of the image");
            ImageIO.write(image, "jpg", ouptut);

        } catch (Exception e) {
        }
    }

    static public void main(String args[]) throws Exception {
        GrayScale obj = new GrayScale();
    }

}

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多