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

如何在java中将16位tiff灰度图像转换为32位tiff灰度图像?,java,image,Java,Image,我正在用netbeans平台用java制作DesktopApp。在我的应用程序中,我使用了16位tiff灰度图像,并对该图像进行了处理。现在,我想使用16位tiff灰度图像或16位图像的数据制作32位tiff灰度图像。那么如何在java中将16位图像转换为32位图像呢 您需要做的是将其通过图像处理器对象,然后对其进行校准。可能是这样的: import java.awt.*; import java.awt.image.*; import ij.*; import ij.gui.*; import

我正在用netbeans平台用java制作DesktopApp。在我的应用程序中,我使用了16位tiff灰度图像,并对该图像进行了处理。现在,我想使用16位tiff灰度图像或16位图像的数据制作32位tiff灰度图像。那么如何在java中将16位图像转换为32位图像呢

您需要做的是将其通过图像处理器对象,然后对其进行校准。可能是这样的:

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位,无论输入是什么。但请记住导入正确的JAR。

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

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;
}

你是否意识到如果你这样做,你将不会得到任何新的数据?这是保留地conversion@NikolayKuznetsov感谢早日回复。您提供的链接是用于ARGB图像,但im图像是灰度图像,而不是ARGB或RGB。那个么我该如何进行转换呢?谢谢你们的回复。但我不想使用ImageJ API。我想使用java的API而不是第三方API。好的,那么,如果您正在寻找内置API,那么请寻找BuffereImage类。它所做的是,它将从一个图像中获取RGB值,并将这些值存储在另一个图像中。然后,BuffereImage类为您执行转换。