假设我有一个只由值0,1,2,3组成的1000数组。我想要做的是删除其他值的海洋中的奇数值,例如。0,0,0,0,1,0,0,0,0 --> 0,0,0,0,0,0,0,0,0,0。一个简单的移动平均值并不是真的有效,因为我总是要返回0,1,2,3的值,所以在0,3,0 --> 1之间求平均值是错误的。我想出了这个似乎可以完成这项工作的方法,但我想知道是否有更有效和更好的方法。这是针对ImageJ宏的。
r = 7; //window length
for(j=r; j<lengthOf(armsPosition)-r;j++){
count0 = 0; count1 = 0; count2=0;count3 = 0;
for(m = j - r/2; m <= j + r/2; m++){
if(armsPosition[m] == 0)
count0++;
else if(armsPosition[m] == 1)
count1++;
else if(armsPosition[m] == 2)
count2++;
else
count3++;
}
if(count0 >= count1 && count0 >= count2 && count0 >= count3)
armsPositionA[j]=0;
else if(count1 > count0 && count1 > count2 && count1 > count3)
armsPositionA[j]=1;
else if(count2 > count0 && count2 > count1 && count2 > count3)
armsPositionA[j]=2;
else
armsPositionA[j]=3;
}谢谢,
发布于 2017-05-30 15:39:55
如果您不局限于ImageJ的宏语言,而是可以使用任何支持的数学,那么可以使用Apache commons- scripting languages中的StatUtils.mode(double[] sample, int begin, int length)方法。
下面的Groovy脚本说明了这一点:
import org.apache.commons.math3.stat.StatUtils
a = [1,3,4,21,3,2,21,21,21,21,21,21,4,3,5,2,2,1,1,1,1]
println StatUtils.mode((double[])a, 0, 7)希望这能有所帮助。
https://stackoverflow.com/questions/44196994
复制相似问题