我试图在Java中将灰度图像的位深度更改为16位。这是我的源代码。虽然原始灰度图像被转换为16位,但在将其保存在PC中之后,所保存图像的位深度与原始图像的尺寸相同,而不是转换后的图像(16位)。我该如何解决这个问题。谢谢。
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;
this.type = imp.getType();
System.out.println(imp.getType());
}
/** Convert your ImagePlus to 16-bit grayscale. */
public ImagePlus convertToGray16() {
System.out.println(this.imp.getBitDepth());
if (this.type==ImagePlus.GRAY16)
return imp;
if (!(this.type==ImagePlus.GRAY8||this.type==ImagePlus.GRAY32||this.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.setProcessor(null, ip.convertToShort(doScaling));
imp.setCalibration(cal); //update calibration
return imp;
}
/** 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.
* @param scaleConversions */
public static void setDoScaling(boolean scaleConversions) {
doScaling = scaleConversions;
IJ.register(ImageConverter.class);
}
/** Returns true if scaling is enabled.
* @return */
public static boolean getDoScaling() {
return doScaling;
}
public static void main(String []args) throws IOException{
ImagePlus i=new ImagePlus("D:\\grayScaleImage.bmp");
ImageConverter ic=new ImageConverter(i);
i=ic.convertToGray16();
//BufferedImage imgBuffer=i.getProcessor().getBufferedImage();
//System.out.println(imgBuffer);
System.out.println(i.getBitDepth());
//***********************************************//
//problem here-though bit depth of converted image is 32, after saved in the PC, size becomes the original image size
FileSaver fs=new FileSaver(i);
boolean result=fs.saveAsBmp("D:\\grayScaleImage_32.bmp");
}
}
答案 0 :(得分:0)
ImageJ' BMP_Writer
类(由FileSaver
调用)似乎只支持8位和24位(RGB)图像。
保存任何灰度图像时,它应用当前的8位查找表(LUT)并始终保存8位。请参阅此处的源代码:
https://github.com/imagej/imagej1/blob/master/ij/plugin/BMP_Writer.java#L63-L81
您可能希望尝试使用SCIFIO或Bio-Formats进行保存(尽管我没有检查它们是否支持其他位深度)。
此外,您可以依靠copy-pasting库来代替ImageJ 1.x中的ImageJ Ops代码进行转换。这是一个简单的Groovy script(使用script parameters并且可以从script editor运行),说明了这一点:
#@ OpService ops
#@ Img inputImage
result = ops.run("convert.uint16", inputImage)