0

我正在尝试使用 QuantizeFilter

http://www.jhlabs.com/ip/filters/index.html

以减少屏幕截图的颜色深度。

这是我非常非常简单的代码:

    Robot robo = new Robot();
    BufferedImage notQuantized = robo.createScreenCapture( new Rectangle ( 0, 0, 300, 300 ) );
    BufferedImage Quantized = new BufferedImage( 300, 300, BufferedImage.TYPE_INT_BGR);
    File nonquantized = new File ("C:\\nonquantized.png");
    File quantized = new File("C:\\quantized.png");
    nonquantized.createNewFile();
    quantized.createNewFile();
    QuantizeFilter bla = new QuantizeFilter();

    int [] outPixels = new int[300*300*3];
    int [] inPixels = new int[300*300*3];

    notQuantized.getRaster().getPixels( 0, 0, 300, 300, inPixels );
    bla.quantize( inPixels, outPixels, 300, 300,2, true, true );

    Quantized.getRaster().setPixels( 0, 0, 300, 300, outPixels );
    ImageIO.write( Quantized, "png", quantized );
    ImageIO.write( notQuantized, "png", nonquantized );

但是,我剩下的是:

原图:

在此处输入图像描述

量化的图像:

在此处输入图像描述

进一步分析问题表明,inPixels数组填充错误;它用原始图像的上三分之一填充了三次。

有什么指示我可以解决这个问题吗?

另外,Java中的任何链接都很好+快速量化算法?我搜索的是一种算法,它将采用 TYPE_INT_BGR 图像并生成新的 TYPE_INT_BGR 图像,但像素的实际差异较小,因此可以很容易地放气。

例如,如果我们在原始图像中有两个像素,其值为 255、255、234,另一个像素值为 255、255、236,则它们都应转换为 255,255,240。干杯

4

2 回答 2

2

以下示例将正确转换您的图像:

QuantizeFilter q = new QuantizeFilter();
int [] inPixels = new int[image.getWidth()*image.getHeight()*3];
int [] outPixels = new int[image.getWidth()*image.getHeight()*3];
image.getRaster().getPixels( 0, 0, image.getWidth(), image.getHeight(), inPixels );
q.quantize(inPixels, outPixels, image.getWidth(), image.getHeight(), 64, false, false);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0,0,width,height,outPixels);

同样没有理由隐式创建文件,因为 ImageIO.write 会自动创建它们。

于 2012-01-19T18:08:19.767 回答
1

我遇到了同样的问题,不是您发布的代码有问题,而是QuantizeFilter该类没有通过所有像素。你需要找到这个代码部分

 if (!dither) {
        for (int i = 0; i < count; i++)
            outPixels[i] = table[quantizer.getIndexForColor(inPixels[i])];

并将计数乘以 3。

请注意,如果最后 2 个参数设置为 false,这只是一个修复。

于 2012-05-13T00:54:17.987 回答