[英]How to open an image with ImageJ in java
I want to analyse images using the ImageJ framework in java. 我想使用Java中的ImageJ框架分析图像。 How can I open an image?
如何打开图像? I need to have an
ImageProcessor
object to be able to generate a histogram. 我需要有一个
ImageProcessor
对象才能生成直方图。 Here is the code I have so far: 这是我到目前为止的代码:
public void run(ImageProcessor ip) {
int[] H = new int[256]; // histogram array
int w = ip.getWidth();
int h = ip.getHeight();
for (int v = 0; v < h; v++) {
for (int u = 0; u < w; u++) {
int i = ip.getPixel(u, v);
H[i] = H[i] + 1;
}
}
// ... histogram H[] can now be used
}
I work with medical grayscale images where a ColorProcesser is not appropriate. 我使用不适用于ColorProcesser的医学灰度图像。 In that case I use
在这种情况下,我使用
Opener opener = new Opener();
String imageFilePath = "somePath";
ImagePlus imp = opener.openImage(imageFilePath);
ImageProcesser ip = imp.getProcessor(); // ImageProcessor from ImagePlus
I didn't see anything specifying how you wanted to open the image but Iv'e added some code below that will open the folder opener window and you can navigate to a folder of images (I think its mainly used for image stacks but you could just make the stack size 1 and it should work to open a single image). 我没有看到任何东西指定您要如何打开图像,但是我在下面添加了一些代码,这将打开文件夹打开器窗口,您可以导航到图像文件夹(我认为它主要用于图像堆栈,但是您可以只需将堆栈大小设置为1,即可打开单个图像)。
import ij.plugin.FolderOpener;
// Do this stuff in your run method
FolderOpener fo = new FolderOpener(); // create FolderOpener object
ImagePlus your_imgPlus; // create ImagePlus object
your_imgPlus = fo.open(null); // call FolderOpener.open()
The null argument in open() allows the folder selection window to open, you can also use a file path as an argument. open()中的null参数允许打开文件夹选择窗口,您也可以使用文件路径作为参数。
Create a java.awt.Image
first and then use it to construct a ColorProcessor
(subclass of ImageProcessor
) object. 首先创建一个
java.awt.Image
,然后使用它来构造一个ColorProcessor
( ImageProcessor
子类)对象。
Image myImage;
// instantiate myImage
ImageProcessor processor = new ColorProcessor(myImage);
File inputFile = new File("someImage.png");
Image someImage = null;
try {
someImage = ImageIO.read(inputFile);
} catch (IOException e) {
e.printStackTrace();
}
ImagePlus imagePlus = new ImagePlus();
imagePlus.setImage(someImage);
ColorProcessor processor = (ColorProcessor) imagePlus.getProcessor();
The answers are old so maybe there has been some changes, but I prefer using the static method IJ.openImage(java.lang.String path_to_file) http://rsb.info.nih.gov/ij/developer/api/ij/IJ.html . 答案很旧,所以可能有所更改,但是我更喜欢使用静态方法IJ.openImage(java.lang.String path_to_file) http://rsb.info.nih.gov/ij/developer/api/ij/ IJ.html 。 This method returns an ImagePlus object.
此方法返回一个ImagePlus对象。
I use absolute file paths myself but I suspect relative ones should work fine too. 我本人使用绝对文件路径,但我怀疑相对路径也可以正常工作。 If you want the ImageProcessor associated with the ImagePlus you call getProcessor().
如果要使ImageProcessor与ImagePlus关联,则调用getProcessor()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.