How can I use ImageJ as a library for a separate Java application?
Asked Answered
B

3

15

In a regular Java application, I have a BufferedImage that I would like to manipulate with ImageJ. I have a macro that is exactly what I need to execute. I suspect that the first step is to make an ImagePlus object, but I am not sure how to then run a macro on the ImagePlus object from within Java. Section 7.3 of the ImageJ tutorial found here says:

If you decide to use ImagePlus as your internal image format you can also use all plugins and macros from the ImageJ distribution as well as all other ImageJ plugins.

But does not indicate how to do so. If someone could explain how, or point me towards a resource that does, I would very much appreciate it.

Broadway answered 20/5, 2012 at 18:43 Comment(0)
R
12

The following site describes ImageJ API with examples: http://albert.rierol.net/imagej_programming_tutorials.html#ImageJ programming basics

The examples include reading images, processing pixels etc. Well, I guess you will also need to use the API documentation a lot.

Reba answered 20/5, 2012 at 22:29 Comment(1)
This is perfect! IJ.run is exactly what I am looking for. Thank you very much.Broadway
G
6

Here is a sample code that opens an image, inverts it and saves it back:

import ij.ImagePlus;
import ij.io.FileSaver;
import ij.process.ImageProcessor;

ImagePlus imgPlus = new ImagePlus("path-to-sample.jpg");
ImageProcessor imgProcessor = imgPlus.getProcessor();
imgProcessor.invert();
FileSaver fs = new FileSaver(imgPlus);
fs.saveAsJpeg("path-to-inverted.jpg");

And here's a sample code that shows how to manipulate an image to make it grayscale:

BufferedImage bufferedImage = imgProcessor.getBufferedImage();
for(int y=0;y<bufferedImage.getHeight();y++)
{
    for(int x=0;x<bufferedImage.getWidth();x++)
    {
        Color color = new Color(bufferedImage.getRGB(x, y));
        int grayLevel = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
        int r = grayLevel;
        int g = grayLevel;
        int b = grayLevel;
        int rgb = (r<<16)  | (g<<8)  | b;
        bufferedImage.setRGB(x, y, rgb);
    }
}
ImagePlus grayImg = new ImagePlus("gray", bufferedImage);
fs = new FileSaver(grayImg);
fs.saveAsJpeg("path-to-gray.jpg");

I hope it helps you get started :)

Gnosticism answered 16/10, 2015 at 18:15 Comment(2)
Would this be applicable for PNG also? or other image formats?Underbid
I found these examples while looking for grayscale conversion. This grayscale conversion could also be done directly using the ImageConverter and replacing the two ImageProcessor lines from the first example with ImageConverter converter = new ImageConverter(imgPlus) and then converter.convertToGray8();Empiricism
H
2

Here is an opensource project implementation with imagej for photo sharing web app.

Use this as a reference to implement imagej apis in your application

http://www.gingercart.com/Home/java-snippets/create-image-thumbnail-in-java-using-imagej-api

Historiated answered 22/8, 2013 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.