Java,将二维数组与另一个二维数组的每次迭代进行比较以进行 OCR

标签 java

我正在编写一个光学字符识别项目。我需要能够在一个大数组(实际上可以是任何大小)中搜索除了 1 和 0 之外什么都没有的字段,以找到其布局在非常非常原始的 OCR 中最准确反射(reflect)的 13x13 数字模板。它需要遍历 13x13 block 中较大数组的每个可能迭代,以查看其与数字数组匹配的位置。然后它会给出一个分数,匹配越好,分数就越高。我的问题是,它只是重复相同的 13x13 block ,而没有更改迭代并移动到侧面或向下。该方法需要比较所有数字的数组(如下所示的示例输入数组)并显示哪个输入数组获得最高分数。

这是我的代码:

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

最佳答案

我感觉你的问题是由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- 为什么要在 num.length 中添加 nm ?不应该是这样吗?

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

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

根据您提供的信息,我的最佳猜测是两个循环可能都需要一些修改。

关于Java,将二维数组与另一个二维数组的每次迭代进行比较以进行 OCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36192329/

相关文章:

java - HTTPURLConnection Getoutputstream 挂起

java - JNI回调错误

java - 如何在 java 中为 Oracle Identity Management JSON 创建 JSON 对象

java - 这个条件在这个 while 循环中如何工作?

java - 当我失去互联网连接时,我似乎无法访问我的 sqlite 数据。安卓

java - JEdi​​torPane,文本中图像的对齐

java - Spring Boot、JPA/Hibernate : How to execute two raw SELECT queries at once?

java - 如何将 ROI 合并到 imageJ 插件中?

java - 属性模式匹配器需要很长时间

java - 尝试在 Hibernate 中使用 C3P0