我正在为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");
//
}
}
}
这是打印子矩阵,但不是正确的 请告诉我随机子矩阵的正确代码
答案 0 :(得分:0)
已编辑的帖子
public class Matrix {
public static void main(String[] args) {
int[][] a = new int[32][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j <32; j++) {
a[i][j]=i+j;
}
}
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println("The matrix is like this:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
for (int i = 0; i <= 24; i++) {
for (int j = 0; j <= 24; j++) {
print_submatrix(a, i, j);
}
System.out.print("\n");
}
}
public static void print_submatrix(int[][] b, int p, int q) {
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println("The sub matrix [" + p + "," + q + "] is like this:");
for (int i = p; i < p+8; i++) {
for (int j = q; j < q+8; j++) {
System.out.print(b[i][j]+"\t");
}
System.out.print("\n");
}
}
}
此代码在32x32矩阵中写入所有可能的8x8子矩阵。
旧帖子:
对于这部分:
System.out.println("the sub matrix is like 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");
}
}
为i和j选择一个新的起始编号,作为0到24之间的随机数,而不是始终为0。
Random generator = new Random();
int i = generator.nextInt(24);
然后使用8表示p和q作为子矩阵的维度。
Random generator = new Random();
int ii = generator.nextInt(24);
int jj = generator.nextInt(24);
System.out.println("the sub marix is lik this:");
for (int i = ii; i < 8; i++) {
for (int j = jj; j < 8; j++) {
System.out.print(b[i][j]+"\t");
//System.out.println();
}
System.out.print("\n");
}
}