1

我正在为 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");

//             
         }  
}
}

这是打印子矩阵,但不是正确的,请让我提供随机子矩阵的正确代码

4

1 回答 1

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");
    }  
}
}

此代码将所有可能的 8x8 子矩阵写入 32x32 矩阵。

旧帖:

对于这部分:

 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");
 }
 }
于 2014-02-01T07:23:45.837 回答