Как можно объединить несколько буферизованных изображений, полученных из ограничительной рамки, в один файл изображения рядом? - PullRequest
0 голосов
/ 06 октября 2018

Я читаю TIFF-файл и извлекаю изображения ограничивающего прямоугольника, основываясь на значении достоверности каждого символа. Я хочу разделить несколько изображений и объединить их в один файл изображения для учебного набора данных.

1 Ответ

0 голосов
/ 10 октября 2018
BufferedImage[] input = new BufferedImage[count-1];
//Load each input image.
for(int index=0; index<input.length; index++) {
try {
     File f = new File("folder_path\\"+img_"+ 
(index+1)+".tiff");
     input[index] = ImageIO.read(f);
     }
catch(Exception e) {
          e.printStackTrace();
      }
  }

  int offset  = 5, width=0, height=input[0].getHeight();
  for(int index=0; index<input.length; index++) {
        width+=input[index].getWidth();
        if(height<input[index].getHeight())
        {
            height=input[index].getHeight();
        }
  }
  width+=count*offset;
  height+=offset;

  //Create Output image
  BufferedImage output = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = output.createGraphics();
  Color oldColor = g2.getColor();

//fill background
  g2.setPaint(Color.WHITE);
  g2.fillRect(0, 0, width, height);

//draw image
  g2.setColor(oldColor);
  int xcordinate=0;
  for(int index=0; index<input.length; index++) {
      g2.drawImage(input[index], null, xcordinate, 0);
      xcordinate+=input[index].getWidth()+offset;
  }
  g2.dispose();
  File merged = new File("folder_path\\"+merged_2_new.tiff");
  try {
      ImageIO.write(output, "tiff", merged);
  }
  catch(Exception e) {
      e.printStackTrace();
  }
...