[英]How to check runtime that is there any changes in image(16bit,gray-scale) in myApp using java?
[英]How to convert 16 bit, tiff, Gray-Scale image into 32 bit ,tiff, Gray-Scale Image in java?
我正在使用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位,無論輸入是多少。 記住要導入正確的罐子。
如果將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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.