From 3675cd82abadceb3d1d032092b1f7698fd8e6712 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Fri, 25 Sep 2015 20:34:11 -0400 Subject: [PATCH] Calculating char gap by distance from midpoint (rather than edge). More accurate for characters like "1" that don't fill up the full area, but still have the same distance between overall characters --- .../segmentation/charactersegmenter.cpp | 21 +++++++++++++------ .../segmentation/charactersegmenter.h | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/openalpr/segmentation/charactersegmenter.cpp b/src/openalpr/segmentation/charactersegmenter.cpp index 144edac..f4d467c 100644 --- a/src/openalpr/segmentation/charactersegmenter.cpp +++ b/src/openalpr/segmentation/charactersegmenter.cpp @@ -403,6 +403,11 @@ namespace alpr } } } + int CharacterSegmenter::getCharGap(cv::Rect leftBox, cv::Rect rightBox) { + int right_midpoint = (rightBox.x + (rightBox.width / 2)); + int left_midpoint = (leftBox.x + (leftBox.width / 2)); + return right_midpoint - left_midpoint; + } vector CharacterSegmenter::combineCloseBoxes( vector charBoxes) { @@ -410,7 +415,7 @@ namespace alpr if (charBoxes.size() <= 2) return charBoxes; - // First find the median char gap (the space between characters) + // First find the median char gap (the space from midpoint to midpoint of chars) vector char_gaps; for (unsigned int i = 0; i < charBoxes.size(); i++) @@ -419,8 +424,7 @@ namespace alpr break; // the space between charbox i and i+1 - int char_gap = charBoxes[i+1].x - (charBoxes[i].x + charBoxes[i].width); - char_gaps.push_back(char_gap); + char_gaps.push_back(getCharGap(charBoxes[i], charBoxes[i+1])); } int median_char_gap = median(char_gaps.data(), char_gaps.size()); @@ -455,14 +459,14 @@ namespace alpr if (i == 0) left_gap = 999999999999; else - left_gap = charBoxes[i].x - (charBoxes[i-1].x + charBoxes[i-1].width); + left_gap = getCharGap(charBoxes[i-1], charBoxes[i]); - right_gap = charBoxes[i+1].x - (charBoxes[i].x + charBoxes[i].width); + right_gap = getCharGap(charBoxes[i], charBoxes[i+1]); int min_gap = (int) ((float)median_char_gap) * 0.75; int max_gap = (int) ((float)median_char_gap) * 1.25; - int max_width = (int) ((float)biggestCharWidth) * 1.1; + int max_width = (int) ((float)biggestCharWidth) * 1.2; bool has_good_gap = (left_gap >= min_gap && left_gap <= max_gap) || (right_gap >= min_gap && right_gap <= max_gap); @@ -488,6 +492,11 @@ namespace alpr else { newCharBoxes.push_back(charBoxes[i]); + if (this->config->debugCharSegmenter) + { + cout << "Not Merging 2 boxes -- " << i << " and " << i + 1 << " -- has_good_gap: " << has_good_gap << + " bigWidth (" << bigWidth << ") > max_width (" << max_width << ")" << endl; + } } } diff --git a/src/openalpr/segmentation/charactersegmenter.h b/src/openalpr/segmentation/charactersegmenter.h index 5bdb988..b6c9565 100644 --- a/src/openalpr/segmentation/charactersegmenter.h +++ b/src/openalpr/segmentation/charactersegmenter.h @@ -68,6 +68,8 @@ namespace alpr std::vector getHistogramBoxes(HistogramVertical histogram, float avgCharWidth, float avgCharHeight, float* score); std::vector getBestCharBoxes(cv::Mat img, std::vector charBoxes, float avgCharWidth); + + int getCharGap(cv::Rect leftBox, cv::Rect rightBox); std::vector combineCloseBoxes( std::vector charBoxes); std::vector get1DHits(cv::Mat img, int yOffset);