我的任务是获取一个 GeoTIFF,在其中进行一些图像分割,并将其保存到新的 GeoTIFF(具有现有坐标)。如果我理解正确,坐标将保存在 GeoTIFF 元数据中。所以我从原始文件中获取元数据:
File file = new File(inputFilePath);
ImageInputStream iis = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
IIOMetadata metadata=null;
ImageReader reader=null;
if (readers.hasNext()) {
// pick the first available ImageReader
reader = readers.next();
// attach source to the reader
reader.setInput(iis, true);
// read metadata of first image
metadata = reader.getImageMetadata(0);
}
当我这样做时
System.out.println("Metadata: "+metadata);
,我会看到正确的元标记 XML 树。所以我用图像做一些魔法
System.out.println("Starting segmentation");
BufferedImage image = UtilImageIO.loadImage(inputImage);
// Select input image type. Some algorithms behave different depending on image type
ImageType<MultiSpectral<ImageFloat32>> imageType = ImageType.ms(3, ImageFloat32.class);
ImageSuperpixels alg = FactoryImageSegmentation.fh04(new ConfigFh04(500, 30), imageType);
// Convert image into BoofCV format
ImageBase color = imageType.createImage(image.getWidth(), image.getHeight());
ConvertBufferedImage.convertFrom(image, color, true);
// Segment and display results
performSegmentation(alg, color);
System.out.println("Segmentation finished");
结果,我获得了成功进行图像分割的 BufferedImage(resultBufferedImage)。这里开始我的问题,我试图用旧的元数据保存这个 BufferedImage:
BufferedOutputStream out;
ImageWriter writer = ImageIO.getImageWriter(reader);
ImageOutputStream imgout = null;
FileOutputStream fos =null;
fos = new FileOutputStream(outputImage);
out = new BufferedOutputStream(fos);
imgout = ImageIO.createImageOutputStream(out);
writer.setOutput(imgout);
ImageWriteParam param = writer.getDefaultWriteParam();
IIOImage destIIOImage = new IIOImage(resultBufferedImage, null, metadata);
System.out.println("Before write");
writer.write(null, destIIOImage, null);
System.out.println("After write");
我打印了“写后”。但程序仍在运行,我试图等待,但没有结果。因此,当我终止进程时,即使使用地理数据,文件也会成功创建。如何确定编写完成并停止程序?ps 默认Ubuntu查看器中的图像看起来不错,但是当我在QGIS中打开它时,我有透明的字段,如何使灰色背景透明?