Java,将2D数组与另一个2D数组的每次迭代进行比较,形成OCR

时间:2016-03-24 02:38:43

标签: java

我正在写一个光学字符识别项目。我需要能够在一个非常非常原始的OCR中搜索大型数组(实际上可以是任何大小),除了一个零和一个零以找到它们的布局最准确反映的13x13数字模板。它需要在13x13块中移动较大阵列的每个可能的迭代,以查看它与数字阵列匹配的位置。然后给出得分越高,得分越高。我的问题是它只是重复相同的13x13块,而不会改变迭代并向侧面或向下移动。该方法需要比较所有数字的数组(下面显示的样本输入数组),并说明哪个输入数组得分最高。

这是我的代码:

public double compareMatrices(int[][] num,int[][] mat){
    double score=0;
    double highest=0;
    int n=0;
    for(;n+num.length<mat.length;n++){
        int[][] rows=new int[num.length][];
        int m=0;
        for(;m+num.length<mat[0].length;m++){
            for(int o=0;o<num.length;o++){
                rows[o]=Arrays.copyOfRange(mat[n+o],m,m+num.length);
            }
            int p=0;
            for(;p<rows.length;p++){
                int q=0;
                for(;q < rows[0].length;q++){
                    if((rows[p][q]==1)&&(num[p][q]==1)){
                        score+=1;
                    }
                    else if((num[p][q]==1)&&(rows[p][q]==0)){
                        score-=.25;
                    }
                    else if((num[p][q]==0)&&(rows[p][q]==0)){
                        score+=.25;
                    }
                }
            }
        }
        if(score>highest){
            highest=score;
            score=0;
        }
        else{
            score=0;
        }

    }
    return(highest);

这是一个示例输入数组:

0000001000000
0000011000000
0000011000000
0000101000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000001000000
0000111110000

我想搜索类似的内容

0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

3 个答案:

答案 0 :(得分:2)

我觉得你的问题是由for循环的条件引起的。特别是在:

for(;n+num.length<mat.length;n++)
//AND
for(;m+num.length<mat[0].length;m++)

1-为什么不在循环条件中声明nm?像这样:

for(int n = 0; n+num.length<mat.length; n++)
for(int m = 0; m+num.length<mat[0].length; m++)

2-为什么要将nm添加到num.length?不应该是这样的吗?

for(int n = 0; n<mat.length; n++)
for(int m = 0; m<mat[0].length; m++)

另外,请注意,在嵌套循环中,您没有使用n来分别获取每个“水平线”的长度,而是告诉它使用长度总是第一行。 mat[0].length

我对您提供的信息的最佳猜测是,两个循环可能都需要进行一些修订。

答案 1 :(得分:0)

您需要在最里面的循环(m,n)内移动分数的更新:

for(n) {
  for(m) {
       int p=0;
        for(;p<rows.length;p++){
            int q=0;
            for(;q < rows[0].length;q++){
                if((rows[p][q]==1)&&(num[p][q]==1)){
                    score+=1;
                }
                else if((num[p][q]==1)&&(rows[p][q]==0)){
                    score-=.25;
                }
                else if((num[p][q]==0)&&(rows[p][q]==0)){
                    score+=.25;
                }
            }
        }
        if(score>highest){
          highest=score;
          score=0;
        }
        else{
          score=0;
        }
    } // end m
} // end n

答案 2 :(得分:0)

你已经将一个相对简单的四边形嵌套循环变成了一个怪物。您希望避免创建新数组,只需将索引返回到传入的两个数组中。

public double compareMatrices(int[][] num,int[][] mat){
  //TODO: I'm going to assume both arrays are rectangular and of at least length 1
  //     you should check this
  int nNumRows = num.length;
  int nNumCols = num[0].length;
  int nMatRows = mat.length;
  int nMarCols = mat[0].length;

  double highest=0;

  for(int row=0; row<nMatRows-nNumRows+1; ++row) {
     for(int col=0; col<nMatCols-nNumCols+1; ++col) {
       double score = 0;
       for(int row_offset=0; row_offset<nNumRows; ++row_offset; ) {
         for(int col_offset=0; col_offset<nNumCols; ++col_offset;) {
           int matRowIndex = row + row_offset;
           int matColIndex = col + col_offset;
           int numV = num[row_offset][col_offset];
           int matV = mat[matRowIndex][matColIndex];

           if((matV==1)&&(numV==1)){
             score+=1;
           } else if((numV==1)&&(matV==0)){
             score-=.25;
           } else if((numV==0)&&(matV==0)){
             score+=.25;
           }
        }
     }

     if(score>highest) {
       highest=score;
     }
   }
   return highest
 }
相关问题