那么您想对数组应用过滤器吗?这是一个幼稚的实现......
我认为关键是要明智地编码过滤器......我将通过将其表示为一系列浮点数来实现,这些浮点数代表我想要应用于输出值的原始值的百分比。对于过滤器来说,这是相当标准的。
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);
}
}