3

My task is to take one GeoTIFF, make some image segmentation on in, and save it to new GeoTIFF(with existing coordinates). If I understand correctly, the coordinates are preserved in GeoTIFF metadata. So I grab metadata from the original 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 tree of metatags. So I'm do some magic with image

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

In result I obtain a BufferedImage(resultBufferedImage) with successfully image segmentation. And here starts my problems, I'm trying to save this BufferedImage with 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 get printed "After write". But program is still running, I tried to wait, but no results. So when I kill process the file is created successfully, even with geodata. How can I determine the finish of writing and stop program? p.s. Image in default Ubuntu viewer seems to be nice, but when I opened it in QGIS I have transparent fields, and how can I make gray background transparent?enter image description here

1 Answers1

1

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

Community
  • 1
  • 1
luca76
  • 813
  • 1
  • 10
  • 20