我有一个名为FindBestMatch
的方法,它调用另一个名为compareMatrices
的方法,该方法重复十次。 compareMatrices
基本上做了一些数学比较两个数组并返回一个数字,数字越大,两个数组匹配得越好。 FindBestMatch
需要返回编号最大的值。
这就是我所拥有的:
public String FindBestMatch() {
compareMatrices(numFiles.getMatrix("zero"), imgMatrix);
compareMatrices(numFiles.getMatrix("one"), imgMatrix);
compareMatrices(numFiles.getMatrix("two"), imgMatrix);
compareMatrices(numFiles.getMatrix("three"), imgMatrix);
compareMatrices(numFiles.getMatrix("four"), imgMatrix);
compareMatrices(numFiles.getMatrix("five"), imgMatrix);
compareMatrices(numFiles.getMatrix("six"), imgMatrix);
compareMatrices(numFiles.getMatrix("seven"), imgMatrix);
compareMatrices(numFiles.getMatrix("eight"), imgMatrix);
compareMatrices(numFiles.getMatrix("nine"), imgMatrix);
}
所以基本上FindBestMatch
为每个数字运行,从零到九,每次都返回一个数字。我不确定如何找出哪个compareMatrices
来电返回最高号码?
修改
这是`compareMatrices'。它将数字文件的2D数组和图像文件的2D数组作为输入。
public double compareMatrices(int[][] num, double[][] 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];
double 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;
}
它返回得分,基本上数字矩阵与图像矩阵匹配越好,得分越高。
答案 0 :(得分:0)
我建议您将字符串存储在数组String[]
中并使用for循环来查找最大值:
public String FindBestMatch() {
String[] m = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
double max = compareMatrices(numFiles.getMatrix(m[0]), imgMatrix);
double score = 0;
int maxIndex = 0;
for(int i = 1 ; i < m.length ; i++) {
score = compareMatrices(numFiles.getMatrix(m[i]), imgMatrix);
if(score > max) {
max = score;
maxIndex = i;
}
}
return m[i];
}
该方法将返回分数最高的字符串