2015-10-27 115 views
0

我正在尝试通过遍历2D array来查找average遍历二维数组并获得其相邻单元格的平均值

middle中的值必须计算为4边的平均值(Top,Bottom,Left,Right and recalculated)。我必须找到当前指数的四面值,并用该数字替换当前的指数。任何想法,将不胜感激。

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(); 



} 
} 
+1

ConvolveOp有一个很难理解你的问题。即在索引[0] [0] b/c处发生了什么,不存在四条边?另外,重新计算在哪里开始和停止?请澄清。 –

+0

在[0] [0]我只是硬编码其两个相邻边的平均值,它们是(30 + 15/2) – Javazilla

+0

当网格总误差小于5.0时,重新计算必须停止 – Javazilla

回答

0

什么你所描述的仅仅是一个简单的真模糊操作中,box blur基本形式。任何对图像中的每个像素取N×N子矩阵并将其乘以另一个N×N矩阵的操作称为kernel convolution。 Rosetta Code在this page的Java中有一个很好的例子。

如果你可以使用AWT,Java标准库已经具备内置的。