繁体   English   中英

比较OCR中的两个阵列

[英]Comparing two arrays in an OCR

我正在做一个原始的OCR项目。 我写了一种方法,将图像矩阵与数字矩阵进行比较,然后对它们进行“评分”,数字矩阵最高的是最佳匹配。 这是我的方法:

public double compareMatrices(int[][] num, int[][] img) {
    int nNumRows = num.length;
    int nNumCols = num[0].length;
    int nImgRows = img.length;
    int nImgCols = img[0].length;

    double highest = 0;

    for (int row = 0; row < nImgRows - nNumRows + 1; row++) {
        for (int col = 0; col < nImgCols - 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 imgRowIndex = row + row_offset;
                    int imgColIndex = col + col_offset;
                    int numV = num[row_offset][col_offset];
                    int imgV = img[imgRowIndex][imgColIndex];

                    if ((imgV == 1) && (numV == 1)) {
                        score +=1;
                    } else if ((numV == 1) & (imgV == 0)) {
                        score -= 0.25;
                    } else if ((numV == 0) && (imgV == 0)) {
                        score += 0.25;
                    }
                }
            }

            if (score > highest) {
                highest = score;
            }
        }
    }
    return highest;
}

现在,我正在尝试编写一种方法,通过比较compareMatrices方法的得分来确定哪个矩阵最匹配。

我的imgd0是, imgd0是测试图像(或者我什至需要指定一个图像?我可以说要比较一个图像矩阵吗?):

public String FindBestMatch() { 
    numFiles.getMatrix("zero");
    compareMatrices(int[][] zero, imgd0);
    numFiles.getMatrix("one");
    compareMatrices(int[][] one, imgd0);
    numFiles.getMatrix("two");
    compareMatrices(int[][] two, imgd0);
    numFiles.getMatrix("three");
    compareMatrices(int[][] three, imgd0);
    numFiles.getMatrix("four");
    compareMatrices(int[][] four, imgd0);
    numFiles.getMatrix("five");
    compareMatrices(int[][] five, imgd0);
    numFiles.getMatrix("six");
    compareMatrices(int[][] six, imgd0);
    numFiles.getMatrix("seven");
    compareMatrices(int[][] seven, imgd0);
    numFiles.getMatrix("eight");
    compareMatrices(int[][] eight, imgd0);
    numFiles.getMatrix("nine");
    compareMatrices(int[][] nine, imgd0);

其中numFiles.getMatrix是一种来自以下方法:

public class NumFiles {

    private int[][] one = makeMatrix("one.txt");
    private int[][] two= makeMatrix("two.txt");
    private int[][] three= makeMatrix("three.txt");
    private int[][] four= makeMatrix("four.txt");
    private int[][] five= makeMatrix("five.txt");
    private int[][] six= makeMatrix("six.txt");
    private int[][] seven= makeMatrix("seven.txt");
    private int[][] eight= makeMatrix("eight.txt");
    private int[][] nine= makeMatrix("nine.txt");
    private int[][] zero= makeMatrix("zero.txt");

    public NumFiles() {
    }

    public int[][] getMatrix(String num){
        if (num == "one") {
            return one;
        }
        else if (num == "two"){
            return two;
        }
        else if (num == "three"){
            return three;
        }
        else if (num == "four") {
            return four;
        }
        else if (num == "five") {
            return five;
        }
        else if (num == "six") {
            return six;
        }
        else if (num == "seven"){
            return seven;
        }
        else if (num == "eight"){
            return eight;
        }
        else if (num == "nine") {
            return nine;
        }
        else if (num == "zero") {
            return zero;
        }
        else {
            int [][] k = {{-1},{-1}};
            return k;
        }
    }

因此,我要问的是我的FindBestMatch方法,有没有比遍历每个数字矩阵,对单个测试图像每次运行compareMatrices更好的写方法呢? 谢谢!

我不明白你的问题; 如果您要问是否需要全部输入,那么答案是否定的。 您创建一个描述您的图像的类

class Image {String name; int[][] pixels;}

然后,您保留一张图像列表

List<Image> images = new ArrayList<>();

您可以从文件或目录中加载它们(省略代码),并且可以使用流和reduce()以非常优雅的方式遍历列表,但是我很困,无法真正键入它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM