FIJI/ImageJ宏未保存所有分析粒子的结果和汇总表

huangapple go评论112阅读模式
英文:

FIJI/ImageJ Macro not saving every analyze particles results and summary table

问题

我有一个包含多个Tif图像的文件夹,我希望使用"分析粒子"功能来分析这些粒子,并将它们保存到一个文件夹中。我需要对一个通道进行操作,并分析其所有z堆叠切片,然后将结果保存到一个文件夹中。我的问题是,只有每隔一个摘要和结果表被保存。因此,如果我的图像有10个z堆叠切片,它将保存切片1、3、5、7等。我可能只是遗漏了一些小细节!这是在ImageJ宏语言中的操作。

// 选择输入和输出文件夹
dir1 = getDirectory("选择源文件夹 ");
resultsDir = dir1 + "results/";
File.makeDirectory(resultsDir);
dir2 = getDirectory("选择目标文件夹 ");
list = getFileList(dir1);

processFolder(dir1);
function processFolder(dir1){
	list = getFileList(dir1);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(dir1 + File.separator + list[i]))
			processFolder(dir1 + File.separator + list[i]);
		if(endsWith(list[i], ".tif"))
			processFile(dir1, dir2, list[i]);
	}
}	 

function processFile(dir1, dir2, file){
	open(dir1 + File.separator + file);
	
	
// 分割通道并重命名	 
title = getTitle();
run("Split Channels");
selectWindow("C2-" + title);
rename("live");
selectWindow("C3-" + title);
rename("dead");
selectWindow("C4-" + title);
rename("total");



// 应用预处理滤镜并阈值化活细胞
selectWindow("live");
run("Duplicate...", "duplicate"); // 复制活细胞通道,以便在下一步中进行精确的阈值设置
rename("duplicate");
selectWindow("live");
run("Gaussian Blur...", "sigma=2 stack");
run("Threshold...");
waitForUser("调整阈值后,点击此弹出窗口上的“确定”。完成后请勿在阈值屏幕上按任何按钮,只需在“操作要求”屏幕上点击“确定”即可");
run("Make Binary", "method=Default background=Dark calculate black");
//run("Auto Local Threshold", "method=Phansalkar radius=8 parameter_1=0 parameter_2=0 white stack");
run("Fill Holes", "stack");
//run("Watershed", "stack");
run("Stack to Images"); // 将z切片转为单独的图像



// 对于每个二进制图像,重命名切片并分析粒子
for(z=0; z<nImages; z++){
			selectImage(z+1);
		if(is("binary")){
			name = getTitle();
			rename(name + "_" + title);
			run("Analyze Particles...", "size=0.50-Infinity show=[Overlay Masks] display clear summarize overlay add");
			selectWindow("Summary");
			saveAs("Results", dir2 + "Live_Summary_" + z + "_" + title + ".csv");
			selectWindow("Results");
			saveAs("Results", dir2  + name + "_" + z + "_" + title + ".csv"); 
			close();
		}
	}		
		close("live*");
		close("duplicate");
		run("Close");
		close();
		// 留下打印语句,直到一切都正常运行,然后删除它们。
	print("正在处理: " + dir1 + File.separator + file);
	print("保存到: " + dir2);
}

希望这能帮助您解决问题。

英文:

I have a folder of multiple Tif images, that I wish to analyze the particles, using analyze particles function. I need one channel, with all of its z-stack slices to be analysed and saved into a folder. My problem is that only every second summary and results table is being saved. So if my image has 10 z-stack slices, it saves slice 1, 3, 5, 7 etc. I must just be missing something small! This is in the ImageJ macro language.

// Choose input and output folders
dir1 = getDirectory(&quot;Choose Source Directory &quot;);
resultsDir = dir1+&quot;results/&quot;;
File.makeDirectory(resultsDir);
dir2 = getDirectory(&quot;Choose Destination Directory &quot;);
list = getFileList(dir1);
processFolder(dir1);
function processFolder(dir1){
list = getFileList(dir1);
list = Array.sort(list);
for (i = 0; i &lt; list.length; i++) {
if(File.isDirectory(dir1 + File.separator + list[i]))
processFolder(dir1 + File.separator + list[i]);
if(endsWith(list[i], &quot;.tif&quot;))
processFile(dir1, dir2, list[i]);
}
}	 
function processFile(dir1, dir2, file){
open(dir1 + File.separator + file);
// Split channels and rename	 
title = getTitle();
run(&quot;Split Channels&quot;);
selectWindow(&quot;C2-&quot; + title);
rename(&quot;live&quot;);
selectWindow(&quot;C3-&quot; + title);
rename(&quot;dead&quot;);
selectWindow(&quot;C4-&quot; + title);
rename(&quot;total&quot;);
//Apply pre-processing filters and threshold live cells
selectWindow(&quot;live&quot;);
run(&quot;Duplicate...&quot;, &quot;duplicate&quot;); // Duplicates live channel so accurate thresholding can be done in the following step
rename(&quot;duplicate&quot;);
selectWindow(&quot;live&quot;);
run(&quot;Gaussian Blur...&quot;, &quot;sigma=2 stack&quot;);
run(&quot;Threshold...&quot;);
waitForUser(&quot;Adjust threshold, press ok on this pop-up when the threshold has been set. Do not press anything on the threshold screen when finished. Just press ok on action required screen&quot;);
run(&quot;Make Binary&quot;, &quot;method=Default background=Dark calculate black&quot;);
//run(&quot;Auto Local Threshold&quot;, &quot;method=Phansalkar radius=8 parameter_1=0 parameter_2=0 white stack&quot;);
run(&quot;Fill Holes&quot;, &quot;stack&quot;);
//run(&quot;Watershed&quot;, &quot;stack&quot;);
run(&quot;Stack to Images&quot;); //Makes z-slices individual images
//For every image that is binary, rename the slice and analyze particles
for(z=0; z&lt;nImages; z++){
selectImage(z+1);
if(is(&quot;binary&quot;)){
name = getTitle();
rename(name + &quot;_&quot; + title);
run(&quot;Analyze Particles...&quot;, &quot;size=0.50-Infinity show=[Overlay Masks] display clear summarize overlay add&quot;);
selectWindow(&quot;Summary&quot;);
saveAs(&quot;Results&quot;, dir2 + &quot;Live_Summary_&quot; + z + &quot;_&quot; + title + &quot;.csv&quot;);
selectWindow(&quot;Results&quot;);
saveAs(&quot;Results&quot;, dir2  + name +&quot;_&quot; + z + &quot;_&quot; + title + &quot;.csv&quot;); 
close();
}
}		
close(&quot;live*&quot;);
close(&quot;duplicate&quot;);
run(&quot;Close&quot;);
close();
// Leave the print statements until things work, then remove them.
print(&quot;Processing: &quot; + dir1 + File.separator + file);
print(&quot;Saving to: &quot; + dir2);
}

答案1

得分: 1

问题很可能是你在循环内运行 close();,然后在循环条件中评估 nImages

示例: 如果你有3张图片,第一张是二进制的。循环开始,处理并删除第一张图片。第二次循环时,i 增加,现在评估窗口列表中的第二张图片,但这将是你的第三张图片,因为第一张图片已被删除,所以第二张图片被跳过。

你要么需要在循环外关闭图片,要么使用 for (i = nImages; i &gt; 0; i--) 逐渐遍历图像列表并在遍历时删除。

英文:

The problem is likely to be that you run close(); inside the loop and then evaluate nImages in the loop condition.

Example: If you had 3 images, and 1st is binary. The loop starts, 1st image is processed and removed. 2nd time through, i is incremented and second image from the window list is now evaluated, however, this will be your 3rd image, because 1st image was removed, so 2nd image has been skipped.

You would either need to close the image outside the loop, or descend the list of images using for (i = nImages; i &gt; 0; i--) removing as you go.

huangapple
  • 本文由 发表于 2023年3月7日 11:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定