[英]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.