【问题标题】:JH Labs Quantize Usage to reduce image color depthJH Labs 量化使用以减少图像颜色深度
【发布时间】:2011-02-18 13:05:41
【问题描述】:

我正在尝试在

中使用 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。干杯

【问题讨论】:

    标签: java image image-processing quantization


    【解决方案1】:

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

    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 会自动创建它们。

    【讨论】:

      【解决方案2】:

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

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

      并将计数乘以 3。

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

      【讨论】:

        猜你喜欢
        • 2016-04-06
        • 2013-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 2011-09-06
        • 2016-03-12
        相关资源
        最近更新 更多