我正在为 Android 应用程序进行图像处理。我需要从 512x512 图像矩阵中选择随机的 8X8 子矩阵。我尝试的代码如下包子矩阵;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class matrix {
public static void main(String[] args) {
//float[][] a = new float[32][32];
//float[][] b = new float[8][8];
int[][] a = new int[32][32];
int[][] c =bigmatrix(a);
int[][] b = new int[8][8];
int[][] d = submatrix(c,b);
int m=a.length;
int n=a[0].length;
int p=b.length;
int q=b[0].length;
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println("M:"+m);
System.out.println("N:"+n);
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println("the marix is lik this:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(c[i][j]+"\t");
//System.out.println();
}
System.out.print("\n");
}
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println("the sub marix is lik this:");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
System.out.print(b[i][j]+"\t");
//System.out.println();
}
System.out.print("\n");
}
}
private static int[][] bigmatrix(int[][] a) {
// TODO Auto-generated method stub
int[][] c = new int[32][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j <32; j++) {
c[i][j]=i;
}
}
return c;
}
private static int[][] submatrix(int[][]c, int[][]b)
{
int m=0,n=0;
int N=8;
int count=0;
for (int i = 0; i < c.length ; i+=N) {
for (int j = 0; j < c[0].length ; j+=N) {
boolean submatrix = true; // at start we assume we have a submatrix
for (int k = i; k < i+N; k++) {
for (int l = j; l < j+N; l++) {
b[m][n]=c[k][l];
print_submatrix(b);
count++;
//System.out.print(" " +b[m][n] );
n++;
}m++; //System.out.println();
n=0;
}
m=0;
}
}
System.out.println("***Count ="+count+"***");
return b;
}
public static void print_submatrix(int[][] b) {
int p=b.length;
int q=b[0].length;
//System.out.println();
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println("the sub marix is lik this:");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
System.out.print(b[i][j]+"\t");
}
System.out.print("\n");
//
}
}
}
这是打印子矩阵,但不是正确的,请让我提供随机子矩阵的正确代码