简体   繁体   English

如何使用ImageJ中的线程打开文件夹? (JAVA)

[英]How would I open a folder using threads in ImageJ? (Java)

as part of a project in my ImageJ plugin, I need to open up to 3000 images as an ImageStack. 作为我的ImageJ插件中的项目的一部分,我需要打开多达3000张图像作为ImageStack。 The user selects a folder containing all the files with 用户选择一个包含所有文件的文件夹

DirectoryChooser dirChoos = new DirectoryChooser("Select Folder");
String filePath = dirChoos.getDirectory();

My current method of opening the files is with 我当前打开文件的方法是

FolderOpener opener = new FolderOpener();
ImagePlus imp = opener.openFolder(filePath);
ImageStack stack = imp.getImageStack();

This works, however for larger folders this adds 4 or 5 minutes on to plugins running time. 这可行,但是对于较大的文件夹,这会使插件的运行时间增加4或5分钟。 While I understand that opening large folders obviously takes more time, it would be nice if I could cut it down a bit. 虽然我知道打开大文件夹显然会花费更多时间,但是如果我可以减少一点的话,那将是很好的。

The method I am trying to implement is 我正在尝试实现的方法是

File folder = new File(filePath);
String[] listOfFiles = Folder.list();
Arrays.sort(listOfFiles); // to make sure the stack is in the right order

By splitting it listOfFiles using Arrays.copyOfRange (into 3 for argument's sake) and into parts and passing them to an object that extends Thread 通过使用Arrays.copyOfRange将listOfFiles拆分(为参数着想,分成3个),并将其传递给扩展Thread的对象

openFolder r1 = new openFolder(listOfFiles_part1);
openFolder r2 = new openFolder(listOfFiles_part2);
openFolder r3 = new openFolder(listOfFiles_part3);

new Thread(r1).start();
new Thread(r2).start();
new Thread(r3).start();

openFolder's method uses listOfFiles_part to form an array of ImagePlus instances from the files openFolder的方法使用listOfFiles_part从文件中形成ImagePlus实例的数组

int len = Array.getLength(listOfFiles_part)
ImagePlus[] impArray = new ImagePlus[len];
for (int a = 0; a < len; a++ ) {
     impArray[a] = new ImagePlus((listOfFiles_part[a]).getPath());
}

then, the impArrays are returned, and an ImageStack is formed from the arrays. 然后,返回impArrays,并从数组中形成一个ImageStack。

However, this does seem like a bit of an obtuse method and I haven't had too much luck implementing as of now. 但是,这似乎有点让人费解,到目前为止,我还没有实现太多的运气。

Are there any better ways to use threads to get an ImageStack from the folder? 有没有更好的方法来使用线程从文件夹中获取ImageStack? (Would using threads even necessarily speed up the process?) (使用线程是否还会加快进程?)

Any help would be appreciated. 任何帮助,将不胜感激。 Cheers 干杯

There is more one approach. 有一种以上的方法。 If you use java 8 the parallel approach is the simplier 如果您使用Java 8,则并行方法更简单

List<ImagePlus> impArray = new ArrayList<ImagePlus>();
listOfFiles_part.parallelStream()
.forEachOrdered(i -> impArray.add(new ImagePlus(i.getPath()));

Otherwise you may used threadpool because you don't know the optimized number of thread depending of the hardware. 否则,您可能会使用线程池,因为您不知道取决于硬件的优化线程数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM