0

可以說我有1000個數組只包含值0,1,2,3。我想要做的是去除其他值的海洋中的奇數值,例如。 0,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; 
} 

感謝,

回答

0

如果你不侷限於使用ImageJ的宏語言,但開放使用任何支持scripting languages的,你可以從Apache的公地數學使用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) 

希望有所幫助。

+0

謝謝。我一直在考慮將整個宏變爲腳本或完整的祈願插件,儘管這需要相當多的學習時間,而我現在還沒有真正的時間。 – hadroque

相關問題