What I am trying to do is iterate through the 2D array
to find an average
.
The values in the middle
must be calculated as an average of 4 sides (Top,Bottom,Left,Right and recalculated)
. I have to find the values of the four sides of the current index and replace the current index with that number. Any ideas would be appreciated.
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();
}
}
What you're describing is just a really simplistic blur operation, basically a form of box blur . Any operation where you take an NxN submatrix for every pixel in an image and multiply it by another NxN matrix is called a kernel convolution . Rosetta Code has a good example in Java on this page .
If you can use AWT, the Java standard library already has ConvolveOp
built in.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.