迭代二维数组,得到相邻单元的平均值



我要做的是迭代2D array以找到average

middle中的数值必须计算为(Top,Bottom,Left,Right and recalculated) 4边的平均值。我必须找到当前索引的四个边的值并用那个数字替换当前索引。如有任何意见,不胜感激。

final public class Matrix {
private final int M;             // number of rows
private final int N;             // number of columns
private final double[][] data;   // M-by-N array
// create M-by-N matrix of 0's
public Matrix(int M, int N) {
    this.M = M;
    this.N = N;
    data = new double[M][N];
}
// create matrix based on 2d array
public Matrix(double[][] data) {
    M = data.length;
    N = data[0].length;
    this.data = new double[M][N];
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
                this.data[i][j] = data[i][j];
}
// copy constructor
private Matrix(Matrix A) { this(A.data); }

// print matrix to standard output
public void show() {
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) 
            System.out.printf("%.1f ", data[i][j]);
        System.out.println();
    }
}

// test client
public static void main(String[] args) {
    double[][] d = { { 22.5,30,30,30,30,30,30,30,30,51 }, 
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 15,0,0,0,0,0,0,0,0,72 },
                     { 45,75,75,75,75,75,75,75,75,73.5 }
    };

    Matrix D = new Matrix(d);
    D.show(); 
    System.out.println();

}
}

你所描述的只是一个非常简单的模糊操作,基本上是一种形式的框模糊。对图像中的每个像素取一个NxN子矩阵并将其乘以另一个NxN矩阵的任何操作都称为核卷积。Rosetta Code在这个页面上有一个很好的Java示例。

如果可以使用AWT,则Java标准库已经内置了ConvolveOp

相关内容

  • 没有找到相关文章

最新更新