2012-12-17 204 views
1

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

+0

你知道,如果你这样做,你不会得到任何新的数据? –

+1

http://stackoverflow.com/questions/1177059/converting-a-32bpp-image-to-a-16bpp-image-in-java这是储备转换 –

+0

@NikolayKuznetsov感谢您的回复early.Linked给你是为ARGB图像,但是im图像是灰度图像而不是ARGB或RGB。那么我怎么做转换? – Jay

回答

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位,有史以来输入可能是什么。记得要输入正确的罐子。

+0

感谢回复me.But我不想使用ImageJ API。我想使用Java的API而不是第三方API。 – Jay

+0

好吧,如果您正在寻找内置的API,那么请查找BufferedImage类。它所做的是,它将从一个图像获取RGB值并将值存储在另一个图像中。 BufferedImage类然后为你做转换。 –

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