mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-07 04:40:51 +08:00
Wrapped OpenALPR library in "alpr" namespace. Resolves issue #60.
This commit is contained in:
@@ -24,84 +24,89 @@
|
||||
|
||||
using namespace cv;
|
||||
|
||||
TextLine::TextLine(std::vector<cv::Point2f> textArea, std::vector<cv::Point2f> linePolygon) {
|
||||
std::vector<Point> textAreaInts, linePolygonInts;
|
||||
|
||||
for (uint i = 0; i < textArea.size(); i++)
|
||||
textAreaInts.push_back(Point(round(textArea[i].x), round(textArea[i].y)));
|
||||
for (uint i = 0; i < linePolygon.size(); i++)
|
||||
linePolygonInts.push_back(Point(round(linePolygon[i].x), round(linePolygon[i].y)));
|
||||
|
||||
initialize(textAreaInts, linePolygonInts);
|
||||
}
|
||||
namespace alpr
|
||||
{
|
||||
|
||||
TextLine::TextLine(std::vector<cv::Point> textArea, std::vector<cv::Point> linePolygon) {
|
||||
initialize(textArea, linePolygon);
|
||||
}
|
||||
TextLine::TextLine(std::vector<cv::Point2f> textArea, std::vector<cv::Point2f> linePolygon) {
|
||||
std::vector<Point> textAreaInts, linePolygonInts;
|
||||
|
||||
|
||||
TextLine::~TextLine() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
void TextLine::initialize(std::vector<cv::Point> textArea, std::vector<cv::Point> linePolygon) {
|
||||
if (textArea.size() > 0)
|
||||
{
|
||||
if (this->textArea.size() > 0)
|
||||
this->textArea.clear();
|
||||
if (this->linePolygon.size() > 0)
|
||||
this->linePolygon.clear();
|
||||
|
||||
for (uint i = 0; i < textArea.size(); i++)
|
||||
this->textArea.push_back(textArea[i]);
|
||||
|
||||
textAreaInts.push_back(Point(round(textArea[i].x), round(textArea[i].y)));
|
||||
for (uint i = 0; i < linePolygon.size(); i++)
|
||||
this->linePolygon.push_back(linePolygon[i]);
|
||||
|
||||
this->topLine = LineSegment(linePolygon[0].x, linePolygon[0].y, linePolygon[1].x, linePolygon[1].y);
|
||||
this->bottomLine = LineSegment(linePolygon[3].x, linePolygon[3].y, linePolygon[2].x, linePolygon[2].y);
|
||||
linePolygonInts.push_back(Point(round(linePolygon[i].x), round(linePolygon[i].y)));
|
||||
|
||||
this->charBoxTop = LineSegment(textArea[0].x, textArea[0].y, textArea[1].x, textArea[1].y);
|
||||
this->charBoxBottom = LineSegment(textArea[3].x, textArea[3].y, textArea[2].x, textArea[2].y);
|
||||
this->charBoxLeft = LineSegment(textArea[3].x, textArea[3].y, textArea[0].x, textArea[0].y);
|
||||
this->charBoxRight = LineSegment(textArea[2].x, textArea[2].y, textArea[1].x, textArea[1].y);
|
||||
|
||||
// Calculate line height
|
||||
float x = ((float) linePolygon[1].x) / 2;
|
||||
Point midpoint = Point(x, bottomLine.getPointAt(x));
|
||||
Point acrossFromMidpoint = topLine.closestPointOnSegmentTo(midpoint);
|
||||
this->lineHeight = distanceBetweenPoints(midpoint, acrossFromMidpoint);
|
||||
|
||||
// Subtract a pixel since the height is a little overestimated by the bounding box
|
||||
this->lineHeight = this->lineHeight - 1;
|
||||
|
||||
this->angle = (topLine.angle + bottomLine.angle) / 2;
|
||||
|
||||
initialize(textAreaInts, linePolygonInts);
|
||||
}
|
||||
|
||||
TextLine::TextLine(std::vector<cv::Point> textArea, std::vector<cv::Point> linePolygon) {
|
||||
initialize(textArea, linePolygon);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cv::Mat TextLine::drawDebugImage(cv::Mat baseImage) {
|
||||
cv::Mat debugImage(baseImage.size(), baseImage.type());
|
||||
|
||||
baseImage.copyTo(debugImage);
|
||||
|
||||
cv::cvtColor(debugImage, debugImage, CV_GRAY2BGR);
|
||||
|
||||
|
||||
fillConvexPoly(debugImage, linePolygon.data(), linePolygon.size(), Scalar(0,0,165));
|
||||
TextLine::~TextLine() {
|
||||
}
|
||||
|
||||
fillConvexPoly(debugImage, textArea.data(), textArea.size(), Scalar(125,255,0));
|
||||
|
||||
line(debugImage, topLine.p1, topLine.p2, Scalar(255,0,0), 1);
|
||||
line(debugImage, bottomLine.p1, bottomLine.p2, Scalar(255,0,0), 1);
|
||||
|
||||
line(debugImage, charBoxTop.p1, charBoxTop.p2, Scalar(0,125,125), 1);
|
||||
line(debugImage, charBoxLeft.p1, charBoxLeft.p2, Scalar(0,125,125), 1);
|
||||
line(debugImage, charBoxRight.p1, charBoxRight.p2, Scalar(0,125,125), 1);
|
||||
line(debugImage, charBoxBottom.p1, charBoxBottom.p2, Scalar(0,125,125), 1);
|
||||
|
||||
|
||||
return debugImage;
|
||||
}
|
||||
|
||||
|
||||
void TextLine::initialize(std::vector<cv::Point> textArea, std::vector<cv::Point> linePolygon) {
|
||||
if (textArea.size() > 0)
|
||||
{
|
||||
if (this->textArea.size() > 0)
|
||||
this->textArea.clear();
|
||||
if (this->linePolygon.size() > 0)
|
||||
this->linePolygon.clear();
|
||||
|
||||
for (uint i = 0; i < textArea.size(); i++)
|
||||
this->textArea.push_back(textArea[i]);
|
||||
|
||||
for (uint i = 0; i < linePolygon.size(); i++)
|
||||
this->linePolygon.push_back(linePolygon[i]);
|
||||
|
||||
this->topLine = LineSegment(linePolygon[0].x, linePolygon[0].y, linePolygon[1].x, linePolygon[1].y);
|
||||
this->bottomLine = LineSegment(linePolygon[3].x, linePolygon[3].y, linePolygon[2].x, linePolygon[2].y);
|
||||
|
||||
this->charBoxTop = LineSegment(textArea[0].x, textArea[0].y, textArea[1].x, textArea[1].y);
|
||||
this->charBoxBottom = LineSegment(textArea[3].x, textArea[3].y, textArea[2].x, textArea[2].y);
|
||||
this->charBoxLeft = LineSegment(textArea[3].x, textArea[3].y, textArea[0].x, textArea[0].y);
|
||||
this->charBoxRight = LineSegment(textArea[2].x, textArea[2].y, textArea[1].x, textArea[1].y);
|
||||
|
||||
// Calculate line height
|
||||
float x = ((float) linePolygon[1].x) / 2;
|
||||
Point midpoint = Point(x, bottomLine.getPointAt(x));
|
||||
Point acrossFromMidpoint = topLine.closestPointOnSegmentTo(midpoint);
|
||||
this->lineHeight = distanceBetweenPoints(midpoint, acrossFromMidpoint);
|
||||
|
||||
// Subtract a pixel since the height is a little overestimated by the bounding box
|
||||
this->lineHeight = this->lineHeight - 1;
|
||||
|
||||
this->angle = (topLine.angle + bottomLine.angle) / 2;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cv::Mat TextLine::drawDebugImage(cv::Mat baseImage) {
|
||||
cv::Mat debugImage(baseImage.size(), baseImage.type());
|
||||
|
||||
baseImage.copyTo(debugImage);
|
||||
|
||||
cv::cvtColor(debugImage, debugImage, CV_GRAY2BGR);
|
||||
|
||||
|
||||
fillConvexPoly(debugImage, linePolygon.data(), linePolygon.size(), Scalar(0,0,165));
|
||||
|
||||
fillConvexPoly(debugImage, textArea.data(), textArea.size(), Scalar(125,255,0));
|
||||
|
||||
line(debugImage, topLine.p1, topLine.p2, Scalar(255,0,0), 1);
|
||||
line(debugImage, bottomLine.p1, bottomLine.p2, Scalar(255,0,0), 1);
|
||||
|
||||
line(debugImage, charBoxTop.p1, charBoxTop.p2, Scalar(0,125,125), 1);
|
||||
line(debugImage, charBoxLeft.p1, charBoxLeft.p2, Scalar(0,125,125), 1);
|
||||
line(debugImage, charBoxRight.p1, charBoxRight.p2, Scalar(0,125,125), 1);
|
||||
line(debugImage, charBoxBottom.p1, charBoxBottom.p2, Scalar(0,125,125), 1);
|
||||
|
||||
|
||||
return debugImage;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user