2

を使用して画像を CS_GRAY に変換できることを知っています

public static BufferedImage getGrayBufferedImage(BufferedImage image) {
    BufferedImageOp op = new ColorConvertOp(ColorSpace
            .getInstance(ColorSpace.CS_GRAY), null);
    BufferedImage sourceImgGray = op.filter(image, null);

    return sourceImgGray;
}

ただし、これは私のプログラム全体のチョークポイントです。800x600 ピクセルの画像でこれを頻繁に行う必要があり、この操作が完了するまでに平均で約 200 ~ 300 ミリ秒かかります。1 つの for ループを使用して画像データをループし、すぐに設定することで、これをはるかに高速に実行できることがわかっています。一方、上記のコードは、グレースケールの新しい 800x600 BufferedImage を構築します。むしろ、渡した画像を変換したいだけです。

for ループを使用してこれを行う方法を知っている人はいますか?画像が RGB 色空間であるとすれば?

4

2 に答える 2

3

ColorConvertOp.filter2つのパラメータを取ります。2番目のパラメーターもBufferedImage、宛先になります。メソッドに正しいものを渡すとBufferedImagefilter新しいを作成する手間が省けますBufferedImage

于 2010-05-10T17:08:21.013 に答える
1
private static int grayscale(int rgb) {
   int r = rgb >> 16 & 0xff;
   int g = rgb >> 8  & 0xff;
   int b = rgb       & 0xff;
   int cmax = Math.max(Math.max(r, g),b);
   return (rgb & 0xFF000000) | (cmax << 16) | (cmax << 8) | cmax;
}
public static BufferedImage grayscale(BufferedImage bi) {
    BufferedImage bout = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
    int[] rgbArray = new int[bi.getWidth() * bi.getHeight()];
    rgbArray = bi.getRGB(0, 0, bi.getWidth(), bi.getHeight(), rgbArray, 0, bi.getWidth());
    for (int i = 0, q = rgbArray.length; i < q; i++) {
        rgbArray[i] = grayscale(rgbArray[i]);
    }
    bout.setRGB(0, 0, bout.getWidth(), bout.getHeight(), rgbArray, 0, bout.getWidth());
    return bout;
}

Whatever you're doing you are likely doing something wrong. You shouldn't be regenerating a buffered image over and over again. But, rather figuring out a scheme to simply update the buffered image, or take the original pixels from the original and just using the grayscale which is the max of the RGB components, in each of the sections.

于 2014-06-09T23:05:17.153 に答える