diff --git a/src/openalpr/postprocess.cpp b/src/openalpr/postprocess.cpp index 7e28694..0e9e649 100644 --- a/src/openalpr/postprocess.cpp +++ b/src/openalpr/postprocess.cpp @@ -181,6 +181,18 @@ void PostProcess::analyze(string templateregion, int topn) } + // Prune the letters based on the topN value. + // If our topN value is 3, for example, we can get rid of a lot of low scoring letters + // because it would be impossible for them to be a part of our topN results. + vector maxDepth = getMaxDepth(topn); + + for (int i = 0; i < letters.size(); i++) + { + for (int k = letters[i].size() - 1; k > maxDepth[i]; k--) + { + letters[i].erase(letters[i].begin() + k); + } + } //getTopN(); vector tmp; @@ -325,6 +337,69 @@ float PostProcess::calculateMaxConfidenceScore() return totalScore / ((float) numScores); } +// Finds the minimum number of letters to include in the recursive sorting algorithm. +// For example, if I have letters +// A-200 B-100 C-100 +// X-99 Y-95 Z-90 +// Q-55 R-80 +// And my topN value was 3, this would return: +// 0, 1, 1 +// Which represents: +// A-200 B-100 C-100 +// Y-95 Z-90 +vector PostProcess::getMaxDepth(int topn) +{ + + vector depth; + for (int i = 0; i < letters.size(); i++) + depth.push_back(0); + + int nextLeastDropCharPos = getNextLeastDrop(depth); + while (nextLeastDropCharPos != -1) + { + depth[nextLeastDropCharPos] = depth[nextLeastDropCharPos] + 1; + if (getPermutationCount(depth) > topn) + break; + + nextLeastDropCharPos = getNextLeastDrop(depth); + } + + + return depth; +} + +int PostProcess::getPermutationCount(vector depth) +{ + int permutationCount = 1; + for (int i = 0; i < depth.size(); i++) + { + permutationCount *= (depth[i] + 1); + } + + return permutationCount; +} + +int PostProcess::getNextLeastDrop(vector depth) +{ + int nextLeastDropCharPos = -1; + float leastNextDrop = 99999999999; + + for (int i = 0; i < letters.size(); i++) + { + if (depth[i] + 1 >= letters[i].size()) + continue; + + float drop = letters[i][depth[i]].totalscore - letters[i][depth[i]+1].totalscore; + + if (drop < leastNextDrop) + { + nextLeastDropCharPos = i; + leastNextDrop = drop; + } + } + + return nextLeastDropCharPos; +} const vector PostProcess::getResults() { diff --git a/src/openalpr/postprocess.h b/src/openalpr/postprocess.h index f13d4e5..2f4438a 100644 --- a/src/openalpr/postprocess.h +++ b/src/openalpr/postprocess.h @@ -106,6 +106,11 @@ class PostProcess vector allPossibilities; + + // Functions used to prune the list of letters (based on topn) to improve performance + vector getMaxDepth(int topn); + int getPermutationCount(vector depth); + int getNextLeastDrop(vector depth); }; /*