You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ImageJ中getHistogram函数输出与直方图List显示不一致问题排查

ImageJ getHistogram 输出与直方图窗口List结果不符的问题解决

问题出在活动窗口的切换:你执行run("Histogram")后,直方图窗口会成为当前活动窗口,此时调用getHistogram,函数会返回直方图窗口的分箱统计数据(而非你处理后的二值图像的像素计数),这就是数值异常的原因。

解决方法有两种:

方法1:移除不必要的直方图窗口调用

你不需要打开直方图窗口就能获取图像的直方图数据,直接在处理完图像后调用getHistogram即可:

compiledDataFile=File.open("C:/test/testfile.txt");

open("C:/Users/dnewman/OneDrive - Innovative Micro Technology/Pictures/white and black.jpg");
run("8-bit");
setAutoThreshold("Default dark no-reset");
run("Threshold...");
setThreshold(0, 150);
run("Convert to Mask");
// 移除run("Histogram"),直接获取图像直方图
getHistogram(values, counts, 256);

Array.print(counts)

方法2:重新激活原图像窗口(若需保留直方图窗口)

如果必须打开直方图窗口,在调用getHistogram前手动切换回原图像窗口(注意替换成你的图像实际标题):

compiledDataFile=File.open("C:/test/testfile.txt");

open("C:/Users/dnewman/OneDrive - Innovative Micro Technology/Pictures/white and black.jpg");
run("8-bit");
setAutoThreshold("Default dark no-reset");
run("Threshold...");
setThreshold(0, 150);
run("Convert to Mask");
run("Histogram");
// 重新激活原图像窗口
selectWindow("white and black.jpg");
getHistogram(values, counts, 256);

Array.print(counts)

修改后,counts数组会返回正确的像素计数:counts[0]=249600,counts[255]=400,与直方图窗口List按钮显示的结果一致。


内容的提问来源于stack exchange,提问作者David Newman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 20:33:28