如何将 ImageJ 用作单独 Java 应用程序的库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10676136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:03:26  来源:igfitidea点击:

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

javaimage-processingimagej

提问by eiowmqui

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 heresays:

在常规的 Java 应用程序中,我有一个 BufferedImage,我想用 ImageJ 操作它。我有一个宏,这正是我需要执行的。我怀疑第一步是创建一个 ImagePlus 对象,但我不确定如何从 Java 内部对 ImagePlus 对象运行宏。此处找到的 ImageJ 教程的第 7.3 节说:

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.

如果您决定使用 ImagePlus 作为您的内部图像格式,您还可以使用 ImageJ 发行版中的所有插件和宏以及所有其他 ImageJ 插件。

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.

但没有说明如何操作。如果有人可以解释如何做,或向我指出这样做的资源,我将不胜感激。

采纳答案by Hakan Serce

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

以下站点通过示例描述了 ImageJ API:http: //albert.rierol.net/imagej_programming_tutorials.html#ImageJ编程基础

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

示例包括读取图像、处理像素等。好吧,我想您还需要大量使用API 文档

回答by Meysam

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 :)

我希望它可以帮助您入门:)

回答by Mouli

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

这是一个带有 imagej 的开源项目实现,用于照片共享 Web 应用程序。

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

以此为参考,在你的应用中实现 imagej api

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

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