Wrapped OpenALPR library in "alpr" namespace. Resolves issue #60.

This commit is contained in:
Matt Hill
2014-10-27 20:12:57 -04:00
parent 83ed86c6b4
commit 85f52a6b8c
79 changed files with 7234 additions and 6968 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -29,58 +29,61 @@
#include "textdetection/textcontours.h"
#include "pipeline_data.h"
//const float MIN_BOX_WIDTH_PX = 4; // 4 pixels
const cv::Scalar COLOR_DEBUG_EDGE(0,0,255); // Red
const cv::Scalar COLOR_DEBUG_SPECKLES(0,0,255); // Red
const cv::Scalar COLOR_DEBUG_MIN_HEIGHT(255,0,0); // Blue
const cv::Scalar COLOR_DEBUG_MIN_AREA(255,0,0); // Blue
const cv::Scalar COLOR_DEBUG_FULLBOX(255,255,0); // Blue-green
const cv::Scalar COLOR_DEBUG_COLORFILTER(255,0,255); // Magenta
const cv::Scalar COLOR_DEBUG_EMPTYFILTER(0,255,255); // Yellow
class CharacterSegmenter
namespace alpr
{
public:
CharacterSegmenter(PipelineData* pipeline_data);
virtual ~CharacterSegmenter();
int confidence;
const cv::Scalar COLOR_DEBUG_EDGE(0,0,255); // Red
const cv::Scalar COLOR_DEBUG_SPECKLES(0,0,255); // Red
const cv::Scalar COLOR_DEBUG_MIN_HEIGHT(255,0,0); // Blue
const cv::Scalar COLOR_DEBUG_MIN_AREA(255,0,0); // Blue
const cv::Scalar COLOR_DEBUG_FULLBOX(255,255,0); // Blue-green
const cv::Scalar COLOR_DEBUG_COLORFILTER(255,0,255); // Magenta
const cv::Scalar COLOR_DEBUG_EMPTYFILTER(0,255,255); // Yellow
class CharacterSegmenter
{
public:
CharacterSegmenter(PipelineData* pipeline_data);
virtual ~CharacterSegmenter();
int confidence;
private:
Config* config;
PipelineData* pipeline_data;
private:
Config* config;
PipelineData* pipeline_data;
LineSegment top;
LineSegment bottom;
std::vector<cv::Mat> imgDbgGeneral;
std::vector<cv::Mat> imgDbgCleanStages;
LineSegment top;
LineSegment bottom;
cv::Mat getCharBoxMask(cv::Mat img_threshold, std::vector<cv::Rect> charBoxes);
std::vector<cv::Mat> imgDbgGeneral;
std::vector<cv::Mat> imgDbgCleanStages;
void removeSmallContours(std::vector<cv::Mat> thresholds, float avgCharHeight, TextLine textLine);
cv::Mat getCharBoxMask(cv::Mat img_threshold, std::vector<cv::Rect> charBoxes);
std::vector<cv::Rect> getHistogramBoxes(VerticalHistogram histogram, float avgCharWidth, float avgCharHeight, float* score);
std::vector<cv::Rect> getBestCharBoxes(cv::Mat img, std::vector<cv::Rect> charBoxes, float avgCharWidth);
std::vector<cv::Rect> combineCloseBoxes( std::vector<cv::Rect> charBoxes, float avgCharWidth);
void removeSmallContours(std::vector<cv::Mat> thresholds, float avgCharHeight, TextLine textLine);
std::vector<cv::Rect> get1DHits(cv::Mat img, int yOffset);
std::vector<cv::Rect> getHistogramBoxes(VerticalHistogram histogram, float avgCharWidth, float avgCharHeight, float* score);
std::vector<cv::Rect> getBestCharBoxes(cv::Mat img, std::vector<cv::Rect> charBoxes, float avgCharWidth);
std::vector<cv::Rect> combineCloseBoxes( std::vector<cv::Rect> charBoxes, float avgCharWidth);
void cleanCharRegions(std::vector<cv::Mat> thresholds, std::vector<cv::Rect> charRegions);
void cleanBasedOnColor(std::vector<cv::Mat> thresholds, cv::Mat colorMask, std::vector<cv::Rect> charRegions);
void cleanMostlyFullBoxes(std::vector<cv::Mat> thresholds, const std::vector<cv::Rect> charRegions);
std::vector<cv::Rect> filterMostlyEmptyBoxes(std::vector<cv::Mat> thresholds, const std::vector<cv::Rect> charRegions);
void filterEdgeBoxes(std::vector<cv::Mat> thresholds, const std::vector<cv::Rect> charRegions, float avgCharWidth, float avgCharHeight);
std::vector<cv::Rect> get1DHits(cv::Mat img, int yOffset);
int getLongestBlobLengthBetweenLines(cv::Mat img, int col);
void cleanCharRegions(std::vector<cv::Mat> thresholds, std::vector<cv::Rect> charRegions);
void cleanBasedOnColor(std::vector<cv::Mat> thresholds, cv::Mat colorMask, std::vector<cv::Rect> charRegions);
void cleanMostlyFullBoxes(std::vector<cv::Mat> thresholds, const std::vector<cv::Rect> charRegions);
std::vector<cv::Rect> filterMostlyEmptyBoxes(std::vector<cv::Mat> thresholds, const std::vector<cv::Rect> charRegions);
void filterEdgeBoxes(std::vector<cv::Mat> thresholds, const std::vector<cv::Rect> charRegions, float avgCharWidth, float avgCharHeight);
int isSkinnyLineInsideBox(cv::Mat threshold, cv::Rect box, std::vector<std::vector<cv::Point> > contours, std::vector<cv::Vec4i> hierarchy, float avgCharWidth, float avgCharHeight);
int getLongestBlobLengthBetweenLines(cv::Mat img, int col);
};
int isSkinnyLineInsideBox(cv::Mat threshold, cv::Rect box, std::vector<std::vector<cv::Point> > contours, std::vector<cv::Vec4i> hierarchy, float avgCharWidth, float avgCharHeight);
};
}
#endif // OPENALPR_CHARACTERSEGMENTER_H

View File

@@ -19,32 +19,36 @@
#include "segment.h"
Segment::Segment(cv::Rect newSegment)
{
this->segment = newSegment;
}
Segment::~Segment()
namespace alpr
{
}
Segment::Segment(cv::Rect newSegment)
{
this->segment = newSegment;
}
bool Segment::matches(cv::Rect newSegment)
{
// Compare the two segments with a given leniency
const float WIDTH_LENIENCY_MIN = 0.25;
const float WIDTH_LENIENCY_MAX = 0.20;
float left_min = segment.x - (((float)segment.width) * WIDTH_LENIENCY_MIN);
float left_max = segment.x + (((float)segment.width) * WIDTH_LENIENCY_MAX);
float right_min = (segment.x + segment.width) - (((float)segment.width) * WIDTH_LENIENCY_MIN);
float right_max = (segment.x + segment.width) + (((float)segment.width) * WIDTH_LENIENCY_MAX);
int newSegRight = newSegment.x + newSegment.width;
if (newSegment.x >= left_min && newSegment.x <= left_max &&
newSegRight >= right_min && newSegRight <= right_max)
return true;
return false;
}
Segment::~Segment()
{
}
bool Segment::matches(cv::Rect newSegment)
{
// Compare the two segments with a given leniency
const float WIDTH_LENIENCY_MIN = 0.25;
const float WIDTH_LENIENCY_MAX = 0.20;
float left_min = segment.x - (((float)segment.width) * WIDTH_LENIENCY_MIN);
float left_max = segment.x + (((float)segment.width) * WIDTH_LENIENCY_MAX);
float right_min = (segment.x + segment.width) - (((float)segment.width) * WIDTH_LENIENCY_MIN);
float right_max = (segment.x + segment.width) + (((float)segment.width) * WIDTH_LENIENCY_MAX);
int newSegRight = newSegment.x + newSegment.width;
if (newSegment.x >= left_min && newSegment.x <= left_max &&
newSegRight >= right_min && newSegRight <= right_max)
return true;
return false;
}
}

View File

@@ -25,17 +25,22 @@
#include "opencv2/imgproc/imgproc.hpp"
class Segment
{
namespace alpr
{
public:
Segment(cv::Rect newSegment);
virtual ~Segment();
class Segment
{
cv::Rect segment;
bool matches(cv::Rect newSegment);
};
public:
Segment(cv::Rect newSegment);
virtual ~Segment();
cv::Rect segment;
bool matches(cv::Rect newSegment);
};
}
#endif // OPENALPR_SEGMENTATIONGROUP_H

View File

@@ -19,31 +19,36 @@
#include "segmentationgroup.h"
SegmentationGroup::SegmentationGroup()
{
}
SegmentationGroup::~SegmentationGroup()
namespace alpr
{
}
void SegmentationGroup::add(int segmentID)
{
this->segmentIDs.push_back(segmentID);
}
bool SegmentationGroup::equals(SegmentationGroup otherGroup)
{
if (segmentIDs.size() != otherGroup.segmentIDs.size())
return false;
for (int i = 0; i < segmentIDs.size(); i++)
SegmentationGroup::SegmentationGroup()
{
if (otherGroup.segmentIDs[i] != segmentIDs[i])
}
SegmentationGroup::~SegmentationGroup()
{
}
void SegmentationGroup::add(int segmentID)
{
this->segmentIDs.push_back(segmentID);
}
bool SegmentationGroup::equals(SegmentationGroup otherGroup)
{
if (segmentIDs.size() != otherGroup.segmentIDs.size())
return false;
for (int i = 0; i < segmentIDs.size(); i++)
{
if (otherGroup.segmentIDs[i] != segmentIDs[i])
return false;
}
return true;
}
return true;
}

View File

@@ -27,24 +27,28 @@
#include "segment.h"
class SegmentationGroup
namespace alpr
{
public:
SegmentationGroup();
virtual ~SegmentationGroup();
class SegmentationGroup
{
void add(int segmentID);
std::vector<int> segmentIDs;
bool equals(SegmentationGroup otherGroup);
public:
SegmentationGroup();
virtual ~SegmentationGroup();
private:
float strength; // Debuggin purposes -- how many threshold segmentations match this one perfectly
};
void add(int segmentID);
std::vector<int> segmentIDs;
bool equals(SegmentationGroup otherGroup);
private:
float strength; // Debuggin purposes -- how many threshold segmentations match this one perfectly
};
}
#endif // OPENALPR_SEGMENTATIONGROUP_H

View File

@@ -22,160 +22,165 @@
using namespace cv;
using namespace std;
VerticalHistogram::VerticalHistogram(Mat inputImage, Mat mask)
namespace alpr
{
analyzeImage(inputImage, mask);
}
VerticalHistogram::~VerticalHistogram()
{
histoImg.release();
colHeights.clear();
}
void VerticalHistogram::analyzeImage(Mat inputImage, Mat mask)
{
highestPeak = 0;
lowestValley = inputImage.rows;
histoImg = Mat::zeros(inputImage.size(), CV_8U);
int columnCount;
for (int col = 0; col < inputImage.cols; col++)
VerticalHistogram::VerticalHistogram(Mat inputImage, Mat mask)
{
columnCount = 0;
for (int row = 0; row < inputImage.rows; row++)
{
if (inputImage.at<uchar>(row, col) > 0 && mask.at<uchar>(row, col) > 0)
columnCount++;
}
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;
analyzeImage(inputImage, mask);
}
}
int VerticalHistogram::getLocalMinimum(int leftX, int rightX)
{
int minimum = histoImg.rows + 1;
int lowestX = leftX;
for (int i = leftX; i <= rightX; i++)
VerticalHistogram::~VerticalHistogram()
{
if (colHeights[i] < minimum)
histoImg.release();
colHeights.clear();
}
void VerticalHistogram::analyzeImage(Mat inputImage, Mat mask)
{
highestPeak = 0;
lowestValley = inputImage.rows;
histoImg = Mat::zeros(inputImage.size(), CV_8U);
int columnCount;
for (int col = 0; col < inputImage.cols; col++)
{
lowestX = i;
minimum = colHeights[i];
columnCount = 0;
for (int row = 0; row < inputImage.rows; row++)
{
if (inputImage.at<uchar>(row, col) > 0 && mask.at<uchar>(row, col) > 0)
columnCount++;
}
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;
}
}
return lowestX;
}
int VerticalHistogram::getLocalMaximum(int leftX, int rightX)
{
int maximum = -1;
int highestX = leftX;
for (int i = leftX; i <= rightX; i++)
int VerticalHistogram::getLocalMinimum(int leftX, int rightX)
{
if (colHeights[i] > maximum)
int minimum = histoImg.rows + 1;
int lowestX = leftX;
for (int i = leftX; i <= rightX; i++)
{
highestX = i;
maximum = colHeights[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);
int totalWidth = colHeights.size();
int midpoint = ((highestPeak - lowestValley) / 2) + lowestValley;
HistogramDirection prevDirection = FALLING;
int relativePeakHeight = 0;
//int valleyStart = 0;
for (int i = 0; i < totalWidth; i++)
{
bool aboveMidpoint = (colHeights[i] >= midpoint);
if (aboveMidpoint)
{
if (colHeights[i] > relativePeakHeight)
relativePeakHeight = colHeights[i];
prevDirection = FLAT;
}
else
{
relativePeakHeight = 0;
HistogramDirection direction = getHistogramDirection(i);
if ((prevDirection == FALLING || prevDirection == FLAT) && direction == RISING)
{
}
else if ((prevDirection == FALLING || prevDirection == FLAT) && direction == RISING)
{
}
}
}
}
return highestX;
}
int VerticalHistogram::getHeightAt(int x)
{
return colHeights[x];
}
void VerticalHistogram::findValleys()
{
//int MINIMUM_PEAK_HEIGHT = (int) (((float) highestPeak) * 0.75);
int totalWidth = colHeights.size();
int midpoint = ((highestPeak - lowestValley) / 2) + lowestValley;
HistogramDirection prevDirection = FALLING;
int relativePeakHeight = 0;
//int valleyStart = 0;
for (int i = 0; i < totalWidth; i++)
HistogramDirection VerticalHistogram::getHistogramDirection(uint index)
{
bool aboveMidpoint = (colHeights[i] >= midpoint);
int EXTRA_WIDTH_TO_AVERAGE = 2;
if (aboveMidpoint)
float trailingAverage = 0;
float forwardAverage = 0;
int trailStartIndex = index - EXTRA_WIDTH_TO_AVERAGE;
if (trailStartIndex < 0)
trailStartIndex = 0;
uint forwardEndIndex = index + EXTRA_WIDTH_TO_AVERAGE;
if (forwardEndIndex >= colHeights.size())
forwardEndIndex = colHeights.size() - 1;
for (int i = index; i >= trailStartIndex; i--)
{
if (colHeights[i] > relativePeakHeight)
relativePeakHeight = colHeights[i];
prevDirection = FLAT;
trailingAverage += colHeights[i];
}
trailingAverage = trailingAverage / ((float) (1 + index - trailStartIndex));
for (uint i = index; i <= forwardEndIndex; i++)
{
forwardAverage += colHeights[i];
}
forwardAverage = forwardAverage / ((float) (1 + forwardEndIndex - index));
float diff = forwardAverage - trailingAverage;
float minDiff = ((float) (highestPeak - lowestValley)) * 0.10;
if (diff > minDiff)
return RISING;
else if (diff < minDiff)
return FALLING;
else
{
relativePeakHeight = 0;
HistogramDirection direction = getHistogramDirection(i);
if ((prevDirection == FALLING || prevDirection == FLAT) && direction == RISING)
{
}
else if ((prevDirection == FALLING || prevDirection == FLAT) && direction == RISING)
{
}
}
return FLAT;
}
}
HistogramDirection VerticalHistogram::getHistogramDirection(uint index)
{
int EXTRA_WIDTH_TO_AVERAGE = 2;
float trailingAverage = 0;
float forwardAverage = 0;
int trailStartIndex = index - EXTRA_WIDTH_TO_AVERAGE;
if (trailStartIndex < 0)
trailStartIndex = 0;
uint forwardEndIndex = index + EXTRA_WIDTH_TO_AVERAGE;
if (forwardEndIndex >= colHeights.size())
forwardEndIndex = colHeights.size() - 1;
for (int i = index; i >= trailStartIndex; i--)
{
trailingAverage += colHeights[i];
}
trailingAverage = trailingAverage / ((float) (1 + index - trailStartIndex));
for (uint i = index; i <= forwardEndIndex; i++)
{
forwardAverage += colHeights[i];
}
forwardAverage = forwardAverage / ((float) (1 + forwardEndIndex - index));
float diff = forwardAverage - trailingAverage;
float minDiff = ((float) (highestPeak - lowestValley)) * 0.10;
if (diff > minDiff)
return RISING;
else if (diff < minDiff)
return FALLING;
else
return FLAT;
}
}

View File

@@ -22,43 +22,47 @@
#include "opencv2/imgproc/imgproc.hpp"
struct Valley
{
int startIndex;
int endIndex;
int width;
int pixelsWithin;
};
enum HistogramDirection { RISING, FALLING, FLAT };
class VerticalHistogram
namespace alpr
{
public:
VerticalHistogram(cv::Mat inputImage, cv::Mat mask);
virtual ~VerticalHistogram();
struct Valley
{
int startIndex;
int endIndex;
int width;
int pixelsWithin;
};
cv::Mat histoImg;
enum HistogramDirection { RISING, FALLING, FLAT };
// 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);
class VerticalHistogram
{
int getHeightAt(int x);
public:
VerticalHistogram(cv::Mat inputImage, cv::Mat mask);
virtual ~VerticalHistogram();
private:
std::vector<int> colHeights;
int highestPeak;
int lowestValley;
std::vector<Valley> valleys;
cv::Mat histoImg;
void analyzeImage(cv::Mat inputImage, cv::Mat mask);
void findValleys();
// 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);
HistogramDirection getHistogramDirection(uint index);
};
int getHeightAt(int x);
private:
std::vector<int> colHeights;
int highestPeak;
int lowestValley;
std::vector<Valley> valleys;
void analyzeImage(cv::Mat inputImage, cv::Mat mask);
void findValleys();
HistogramDirection getHistogramDirection(uint index);
};
}
#endif // OPENALPR_VERTICALHISTOGRAM_H