Using the ImageJ api, I’m trying to save an composite image, made up of several images laid out side by side.
I’ve got code that loads ImagePlus objs, and saves them. But I can’t figure how to paste an image into another image.
Advertisement
Answer
I interpret the problem as taking multiple images and stitching them together side by side to form a large one where the images may have different dimensions. The following incomplete code is one way of doing it and should get you started.
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; }
You need to write an algorithm to find the pixel value (figureOutThis
) to put in the composite image at i
,j
. That is pretty trivial if all images have the same width and a little bit more work otherwise. Happy coding
Edit: I should add that I am assuming they are also all short images (I work with medical grayscale). You can modify this for other processors