ImageJ API: объединение изображений

Используя API ImageJ, я пытаюсь сохранить составное изображение, состоящее из нескольких изображений, расположенных рядом.

У меня есть код, который загружает объекты ImagePlus и сохраняет их. Но я не могу понять, как вставить изображение в другое изображение.


person Nicholas Marshall    schedule 03.09.2012    source источник
comment
У них одинаковые размеры?   -  person Codey McCodeface    schedule 04.09.2012


Ответы (2)


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

public ImagePlus composeImages(ArrayList<ImagePlus> imageList){
    int sumWidth = 0;
    int maxHeight = 0;

    for(ImagePlus imp : imageList){
        sumWidth = sumWidth +imp.getWidth();
        if(imp.getHeight() > maxHeight)
            maxHeight = imp.getWidth();

    }

    ImagePlus impComposite = new ImagePlus();

    ImageProcessor ipComposite = new ShortProcessor(sumWidth, maxHeight);

    for(int i=0; i<sumWidth; i++){
        for(int j=0; j<sumWidth; j++){

            ipComposite.putPixelValue(i, j, figureOutThis);

        }
    }

    impComposite.setProcessor(ipComposite);
    return impComposite;

}

Вам нужно написать алгоритм, чтобы найти значение пикселя (figureOutThis), чтобы поместить составное изображение в i, j. Это довольно тривиально, если все изображения имеют одинаковую ширину и немного больше работают в противном случае. Удачного кодирования

Редактировать: я должен добавить, что я предполагаю, что все они также короткие изображения (я работаю с медицинскими оттенками серого). Вы можете изменить это для других процессоров

person Codey McCodeface    schedule 04.09.2012

Этот код объединяет/сшивает сетку изображений:

Предполагается, что все изображения имеют одинаковые размеры.

  ImagePlus combine(List<List<ImagePlus>> imagesGrid) {
    checkArgument(
        !imagesGrid.isEmpty() && !imagesGrid.get(0).isEmpty(), "Expected grid to be non-empty");
    checkArgument(
        imagesGrid.stream().map(List::size).distinct().count() == 1,
        "Expected all rows in the grid to be of the same size");
    checkArgument(
        imagesGrid.stream().flatMap(List::stream).map(ImagePlus::getWidth).distinct().count() == 1,
        "Expected all images to have the same width");
    checkArgument(
        imagesGrid.stream().flatMap(List::stream).map(ImagePlus::getHeight).distinct().count() == 1,
        "Expected all images to have the same height");

    int rows = imagesGrid.size();
    int cols = imagesGrid.get(0).size();

    int singleWidth = imagesGrid.get(0).get(0).getWidth();
    int singleHeight = imagesGrid.get(0).get(0).getHeight();

    int combinedWidth = singleWidth * cols;
    int combinedHeight = singleHeight * rows;

    ImageProcessor processor = new ColorProcessor(combinedWidth, combinedHeight);

    for (int row = 0; row < rows; row++) {
      for (int col = 0; col < cols; col++) {
        ImagePlus image = imagesGrid.get(row).get(col);
        int offSetWidth = col * singleWidth;
        int offsetHeight = row * singleHeight;
        for (int w = 0; w < singleWidth; w++) {
          for (int h = 0; h < singleHeight; h++) {
            processor.putPixel(w + offSetWidth, h + offsetHeight, image.getPixel(w, h));
          }
        }
      }
    }

    ImagePlus combinedImage = new ImagePlus();
    combinedImage.setProcessor(processor);
    return combinedImage;
  }
person wilmol    schedule 28.05.2021