如何在java中将16位,tiff,灰度图像转换为32位,tiff,灰度图像?

时间:2012-12-17 08:28:31

标签: java image

我使用netbeans平台在java中制作DesktopApp。在我的应用程序中,我使用16位,tiff,灰度图像和处理该图像。现在,我想使用16位,tiff,灰度图像(或16位图像的数据)制作32位,tiff,灰度图像。那么如何在java中将16位图像转换为32位图像?

2 个答案:

答案 0 :(得分:0)

您需要做的是将其传递给图像处理器对象,然后进行校准。可能有这样的事情:

import java.awt.*;
import java.awt.image.*;
import ij.*;
import ij.gui.*;
import ij.measure.*;

/** converting  an ImagePlus object to a different type. */
public class ImageConverter {
    private ImagePlus imp;
    private int type;
    private static boolean doScaling = true;

    /** Construct an ImageConverter based on an ImagePlus object. */
    public ImageConverter(ImagePlus imp) {
        this.imp = imp;
        type = imp.getType();
    }



    /** Convert your ImagePlus to 32-bit grayscale. */
    public void convertToGray32() {
        if (type==ImagePlus.GRAY32)
            return;
        if (!(type==ImagePlus.GRAY8||type==ImagePlus.GRAY16||type==ImagePlus.COLOR_RGB))
            throw new IllegalArgumentException("Unsupported conversion");
        ImageProcessor ip = imp.getProcessor();
        imp.trimProcessor();
        Calibration cal = imp.getCalibration();
        imp.setProcessor(null, ip.convertToFloat());
        imp.setCalibration(cal); //update calibration
    }



    /** Set true to scale to 0-255 when converting short to byte or float
        to byte and to 0-65535 when converting float to short. */
    public static void setDoScaling(boolean scaleConversions) {
        doScaling = scaleConversions;
        IJ.register(ImageConverter.class); 
    }

    /** Returns true if scaling is enabled. */
    public static boolean getDoScaling() {
        return doScaling;
    }
}

这样,您校准的图像将被设置为32位,输入可能是什么。记得要导入正确的罐子。

答案 1 :(得分:0)

如果您的TIFF作为BufferedImage加载,您可以通过以下方式减少它:

BufferedImage convert(BufferedImage image) {

    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);

    ColorModel colorModel = new ComponentColorModel(
        colorSpace, false, false, Transparency.OPAQUE,
        DataBuffer.TYPE_USHORT);

    BufferedImageOp converter = new ColorConvertOp(colorSpace, null);
    BufferedImage newImage =
        converter.createCompatibleDestImage(image, colorModel);
    converter.filter(image, newImage);

    return newImage;
}