DM中有一个函数可以从数字表达式中获取中位数。例如:
// number expression
number x=3, y=5, z=10;
number num = median(x, pi(), y,z);
OKDialog( "num = " + num);
但是,此功能不适用于图像表达:
// image expression
image img := exprsize(5, icol);
number num = img.median();
OKDialog( "num = " + num );
有什么解决办法吗?谢谢。
您无疑希望有一个更快、更优雅的解决方案,但我认为以下方法有效:
// Get front image and some info about it
Image sourceImg := GetFrontImage();
Number srcW = sourceImg.ImageGetDimensionSize(0);
Number srcH = sourceImg.ImageGetDimensionSize(1);
Number srcMax = sourceImg.Max();
Number srcMin = sourceImg.Min();
// Form histogram of its values with binCount channels
Number binCount = 100;
Image histogram := RealImage("Histogram", 4, binCount);
Number binMin, binSize = (srcMax - srcMin) / binCount;
for (Number iBin = 0; iBin < binCount; iBin++)
{
OpenAndSetProgressWindow("Ch: " + iBin, "", "");
binMin = srcMin + iBin * binSize;
histogram[0, iBin, 1, iBin + 1] = Sum(sourceImg >= binMin && sourceImg < binMin + binSize);
}
CloseProgressWindow();
// Integrate the histogram and locate the median channel
Number minX, minY;
histogram += histogram.Index(icol - 1, 0);
histogram -= srcW * srcH / 2;
histogram.Abs().Min(minX, minY);
// Compute the (approximate) median value
Number median = srcMin + (minX + 0.5) * binSize;
OKDialog("Median: " + median);
也许其他人知道计算直方图的更快方法。这是最慢的部分。