所以你想对数组应用过滤器?这是一个幼稚的实现...我认为关键是要聪明地对过滤器进行编码...我将通过将其表示为一系列浮点数来表示我想应用于输出的原始值的百分比价值。当涉及到过滤器时,这是非常标准的。
public static int[] applyFilter( int[] from , float[] filter ) {
if (filter.length > from.lenth){
throw new IllegalArgumentException("Filter to large to apply to array!");
}
int [] to = new int[from.length + 1 - filter.length];
for ( int i = 0; i < from.length + 1 - filter.length; i++) {
float newValue = 0.0f;
for( int j = 0; j < filter.length; j++){
newValue += filter[j] * from[i+j];
}
to[i] = Math.round(newValue);
}
return to;
}
并调用过滤器,就像您在问题中指定的那样......
public static void main (String ... args){
float[] filter = new float[] { 0.66f, 0.66f };
int[] from = new int[] { 1, 2, 3, 4, 5, 6};
int[] to = applyFilter(from, filter);
for (int i : to){
System.out.println(i);
}
}
处理 from[1] 被缩放 1/2 的情况,可以通过预处理数组然后再应用过滤器来处理。像这样:
public static void main (String ... args){
float[] filter = new float[] { 0.66f, 0.66f };
int[] from = new int[] { 1, 2, 3, 4, 5, 6};
// Preprocess to apply static scalars to the source array.
int[] frompp = from.clone();
frompp[1] = Math.round((float) from[i] / 0.5f);
int[] to = applyFilter(from, filterpp);
for (int i : to){
System.out.println(i);
}
}