Updated character segmenter to break blocks into smaller pieces.

This commit is contained in:
Matt Hill
2014-01-29 15:54:30 -06:00
parent 7808cce33a
commit 8a9b4e1bae
3 changed files with 78 additions and 4 deletions

View File

@@ -292,9 +292,36 @@ vector<Rect> CharacterSegmenter::getHistogramBoxes(VerticalHistogram histogram,
{
charBoxes.push_back(allBoxes[i]);
}
else if (allBoxes[i].width > MAX_SEGMENT_WIDTH && allBoxes[i].height > MIN_HISTOGRAM_HEIGHT)
else if (allBoxes[i].width > avgCharWidth * 2 && allBoxes[i].width < MAX_SEGMENT_WIDTH * 2 && allBoxes[i].height > MIN_HISTOGRAM_HEIGHT)
{
// rectangle(histogram.histoImg, allBoxes[i], Scalar(255, 0, 0) );
// drawAndWait(&histogram.histoImg);
// Try to split up doubles into two good char regions, check for a break between 40% and 60%
int leftEdge = allBoxes[i].x + (int) (((float) allBoxes[i].width) * 0.4f);
int rightEdge = allBoxes[i].x + (int) (((float) allBoxes[i].width) * 0.6f);
int minX = histogram.getLocalMinimum(leftEdge, rightEdge);
int maxXChar1 = histogram.getLocalMaximum(allBoxes[i].x, minX);
int maxXChar2 = histogram.getLocalMaximum(minX, allBoxes[i].x + allBoxes[i].width);
int minHeight = histogram.getHeightAt(minX);
int maxHeightChar1 = histogram.getHeightAt(maxXChar1);
int maxHeightChar2 = histogram.getHeightAt(maxXChar2);
if (maxHeightChar1 > MIN_HISTOGRAM_HEIGHT && minHeight < (0.25 * ((float) maxHeightChar1)))
{
cout << "GOODY GOODY LEFT" << endl;
// Add a box for Char1
Point botRight = Point(minX - 1, allBoxes[i].y + allBoxes[i].height);
charBoxes.push_back(Rect(allBoxes[i].tl(), botRight) );
}
if (maxHeightChar2 > MIN_HISTOGRAM_HEIGHT && minHeight < (0.25 * ((float) maxHeightChar2)))
{
cout << "GOODY GOODY RIGHT" << endl;
// Add a box for Char2
Point topLeft = Point(minX + 1, allBoxes[i].y);
charBoxes.push_back(Rect(topLeft, allBoxes[i].br()) );
}
}
}

View File

@@ -56,19 +56,60 @@ void VerticalHistogram::analyzeImage(Mat inputImage, Mat mask)
}
for (; columnCount > 0; columnCount--)
histoImg.at<uchar>(inputImage.rows - columnCount, col) = 255;
this->colHeights.push_back(columnCount);
if (columnCount < lowestValley)
lowestValley = columnCount;
if (columnCount > highestPeak)
highestPeak = columnCount;
for (; columnCount > 0; columnCount--)
histoImg.at<uchar>(inputImage.rows - columnCount, col) = 255;
}
}
int VerticalHistogram::getLocalMinimum(int leftX, int rightX)
{
int minimum = histoImg.rows + 1;
int lowestX = leftX;
for (int i = leftX; i <= rightX; i++)
{
if (colHeights[i] < minimum)
{
lowestX = i;
minimum = colHeights[i];
}
}
return lowestX;
}
int VerticalHistogram::getLocalMaximum(int leftX, int rightX)
{
int maximum = -1;
int highestX = leftX;
for (int i = leftX; i <= rightX; i++)
{
if (colHeights[i] > maximum)
{
highestX = i;
maximum = colHeights[i];
}
}
return highestX;
}
int VerticalHistogram::getHeightAt(int x)
{
return colHeights[x];
}
void VerticalHistogram::findValleys()
{
int MINIMUM_PEAK_HEIGHT = (int) (((float) highestPeak) * 0.75);

View File

@@ -46,6 +46,12 @@ class VerticalHistogram
Mat histoImg;
// Returns the lowest X position between two points.
int getLocalMinimum(int leftX, int rightX);
// Returns the highest X position between two points.
int getLocalMaximum(int leftX, int rightX);
int getHeightAt(int x);
private:
vector<int> colHeights;