Writing GeoTIFF metadata from one file to another

My task is to take one GeoTIFF, include part of the image segmentation and save it in a new GeoTIFF (with existing coordinates). If I understand correctly, coordinates are stored in GeoTIFF metadata. So I grab the metadata from the source file:

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

      

And when I do System.out.println("Metadata: "+metadata);

, I see the correct XML meta tags tree. So I do magic with a picture

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");

      

As a result, I get a BufferedImage (resultBufferedImage) with image segmentation successfully. And this is where my problems arise, I am trying to keep this BufferedImage with the old metadata:

  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");

      

I am typing "After recording". But the program still works, I tried to wait but no results. So when I kill the process, the file is created successfully, even with geodata. How to determine the end of writing and stopping a program? postscript The image in the default Ubuntu view seems nice, but when I opened it in QGIS I have transparent margins and how can I make the gray background transparent?enter image description here

+3


source to share


1 answer


Not a real answer, but here are two answers on how to make TIFF transparent:



+1


source







All Articles