mirror of
https://github.com/kerberos-io/openalpr-base.git
synced 2025-10-06 11:56:52 +08:00
Got edgefinder working
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "edgefinder.h"
|
#include "edgefinder.h"
|
||||||
|
#include "textlinecollection.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
@@ -27,6 +28,7 @@ EdgeFinder::EdgeFinder(PipelineData* pipeline_data) {
|
|||||||
this->pipeline_data = pipeline_data;
|
this->pipeline_data = pipeline_data;
|
||||||
|
|
||||||
// First re-crop the area from the original picture knowing the text position
|
// First re-crop the area from the original picture knowing the text position
|
||||||
|
this->confidence = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,4 +36,146 @@ EdgeFinder::EdgeFinder(PipelineData* pipeline_data) {
|
|||||||
EdgeFinder::~EdgeFinder() {
|
EdgeFinder::~EdgeFinder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<cv::Point2f> EdgeFinder::findEdgeCorners() {
|
||||||
|
|
||||||
|
TextLineCollection tlc(pipeline_data->textLines);
|
||||||
|
|
||||||
|
vector<Point> corners;
|
||||||
|
|
||||||
|
// If the character segment is especially small, just expand the existing box
|
||||||
|
// If it's a nice, long segment, then guess the correct box based on character height/position
|
||||||
|
if (tlc.longerSegment.length > tlc.charHeight * 3)
|
||||||
|
{
|
||||||
|
float charHeightToPlateWidthRatio = pipeline_data->config->plateWidthMM / pipeline_data->config->charHeightMM;
|
||||||
|
float idealPixelWidth = tlc.charHeight * (charHeightToPlateWidthRatio * 1.03); // Add 3% so we don't clip any characters
|
||||||
|
|
||||||
|
float charHeightToPlateHeightRatio = pipeline_data->config->plateHeightMM / pipeline_data->config->charHeightMM;
|
||||||
|
float idealPixelHeight = tlc.charHeight * charHeightToPlateHeightRatio;
|
||||||
|
|
||||||
|
|
||||||
|
float verticalOffset = (idealPixelHeight * 1.5 / 2);
|
||||||
|
float horizontalOffset = (idealPixelWidth * 1.25 / 2);
|
||||||
|
LineSegment topLine = tlc.centerHorizontalLine.getParallelLine(verticalOffset);
|
||||||
|
LineSegment bottomLine = tlc.centerHorizontalLine.getParallelLine(-1 * verticalOffset);
|
||||||
|
|
||||||
|
LineSegment leftLine = tlc.centerVerticalLine.getParallelLine(-1 * horizontalOffset);
|
||||||
|
LineSegment rightLine = tlc.centerVerticalLine.getParallelLine(horizontalOffset);
|
||||||
|
|
||||||
|
Point topLeft = topLine.intersection(leftLine);
|
||||||
|
Point topRight = topLine.intersection(rightLine);
|
||||||
|
Point botRight = bottomLine.intersection(rightLine);
|
||||||
|
Point botLeft = bottomLine.intersection(leftLine);
|
||||||
|
|
||||||
|
corners.push_back(topLeft);
|
||||||
|
corners.push_back(topRight);
|
||||||
|
corners.push_back(botRight);
|
||||||
|
corners.push_back(botLeft);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
//cout << "HEYOOO!" << endl;
|
||||||
|
int expandX = (int) ((float) pipeline_data->crop_gray.cols) * 0.15f;
|
||||||
|
int expandY = (int) ((float) pipeline_data->crop_gray.rows) * 0.15f;
|
||||||
|
int w = pipeline_data->crop_gray.cols;
|
||||||
|
int h = pipeline_data->crop_gray.rows;
|
||||||
|
|
||||||
|
corners.push_back(Point(-1 * expandX, -1 * expandY));
|
||||||
|
corners.push_back(Point(expandX + w, -1 * expandY));
|
||||||
|
corners.push_back(Point(expandX + w, expandY + h));
|
||||||
|
corners.push_back(Point(-1 * expandX, expandY + h));
|
||||||
|
|
||||||
|
// for (int i = 0; i < 4; i++)
|
||||||
|
// {
|
||||||
|
// std::cout << "CORNER: " << corners[i].x << " - " << corners[i].y << std::endl;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Transformation imgTransform(pipeline_data->grayImg, pipeline_data->crop_gray, pipeline_data->regionOfInterest);
|
||||||
|
vector<Point2f> remappedCorners = imgTransform.transformSmallPointsToBigImage(corners);
|
||||||
|
|
||||||
|
// cout << "topLeft: " << remappedCorners[0] << endl;
|
||||||
|
// cout << "topRight: " << remappedCorners[1] << endl;
|
||||||
|
// cout << "botRight: " << remappedCorners[2] << endl;
|
||||||
|
// cout << "botLeft: " << remappedCorners[3] << endl;
|
||||||
|
|
||||||
|
Size cropSize = imgTransform.getCropSize(remappedCorners,
|
||||||
|
Size(pipeline_data->config->templateWidthPx, pipeline_data->config->templateHeightPx));
|
||||||
|
|
||||||
|
Mat transmtx = imgTransform.getTransformationMatrix(remappedCorners, cropSize);
|
||||||
|
Mat newCrop = imgTransform.crop(cropSize, transmtx);
|
||||||
|
|
||||||
|
// drawAndWait(&newCrop);
|
||||||
|
|
||||||
|
vector<TextLine> newLines;
|
||||||
|
for (uint i = 0; i < pipeline_data->textLines.size(); i++)
|
||||||
|
{
|
||||||
|
vector<Point2f> textArea = imgTransform.transformSmallPointsToBigImage(pipeline_data->textLines[i].textArea);
|
||||||
|
vector<Point2f> linePolygon = imgTransform.transformSmallPointsToBigImage(pipeline_data->textLines[i].linePolygon);
|
||||||
|
|
||||||
|
vector<Point2f> textAreaRemapped;
|
||||||
|
vector<Point2f> linePolygonRemapped;
|
||||||
|
|
||||||
|
textAreaRemapped = imgTransform.remapSmallPointstoCrop(textArea, transmtx);
|
||||||
|
linePolygonRemapped = imgTransform.remapSmallPointstoCrop(linePolygon, transmtx);
|
||||||
|
|
||||||
|
newLines.push_back(TextLine(textAreaRemapped, linePolygonRemapped));
|
||||||
|
}
|
||||||
|
|
||||||
|
PlateLines plateLines(pipeline_data);
|
||||||
|
plateLines.processImage(newCrop, newLines, 1.2);
|
||||||
|
|
||||||
|
|
||||||
|
PlateCorners cornerFinder(newCrop, &plateLines, pipeline_data, newLines);
|
||||||
|
vector<Point> smallPlateCorners = cornerFinder.findPlateCorners();
|
||||||
|
|
||||||
|
confidence = cornerFinder.confidence;
|
||||||
|
|
||||||
|
// cout << "topLeft: " << smallPlateCorners[0] << endl;
|
||||||
|
// cout << "topRight: " << smallPlateCorners[1] << endl;
|
||||||
|
// cout << "botRight: " << smallPlateCorners[2] << endl;
|
||||||
|
// cout << "botLeft: " << smallPlateCorners[3] << endl;
|
||||||
|
|
||||||
|
// cvtColor(newCrop, newCrop, CV_GRAY2BGR);
|
||||||
|
// for (int i = 0; i < 4; i++)
|
||||||
|
// circle(newCrop, smallPlateCorners[i], 4, Scalar(0,255,0), -1);
|
||||||
|
//
|
||||||
|
// drawAndWait(&newCrop);
|
||||||
|
|
||||||
|
// Transformation reTrans(pipeline_data->crop_gray, newCrop, cv::Rect(0,0,pipeline_data->crop_gray.cols, pipeline_data->crop_gray.rows));
|
||||||
|
// vector<Point2f> cornersInOriginalCrop = reTrans.transformSmallPointsToBigImage(smallPlateCorners);
|
||||||
|
//
|
||||||
|
// for (int i = 0; i < 4; i++)
|
||||||
|
// circle(pipeline_data->crop_gray, cornersInOriginalCrop[i], 4, Scalar(255,255,255), -1);
|
||||||
|
//
|
||||||
|
// drawAndWait(&pipeline_data->crop_gray);
|
||||||
|
|
||||||
|
std::vector<Point2f> imgArea;
|
||||||
|
imgArea.push_back(Point2f(0, 0));
|
||||||
|
imgArea.push_back(Point2f(newCrop.cols, 0));
|
||||||
|
imgArea.push_back(Point2f(newCrop.cols, newCrop.rows));
|
||||||
|
imgArea.push_back(Point2f(0, newCrop.rows));
|
||||||
|
Mat newCropTransmtx = imgTransform.getTransformationMatrix(imgArea, remappedCorners);
|
||||||
|
|
||||||
|
vector<Point2f> cornersInOriginalImg = imgTransform.remapSmallPointstoCrop(smallPlateCorners, newCropTransmtx);
|
||||||
|
|
||||||
|
// cout << "topLeft: " << cornersInOriginalImg[0] << endl;
|
||||||
|
// cout << "topRight: " << cornersInOriginalImg[1] << endl;
|
||||||
|
// cout << "botRight: " << cornersInOriginalImg[2] << endl;
|
||||||
|
// cout << "botLeft: " << cornersInOriginalImg[3] << endl;
|
||||||
|
|
||||||
|
// Mat debugimg;
|
||||||
|
// cvtColor(pipeline_data->crop_gray, debugimg, CV_GRAY2BGR);
|
||||||
|
// for (int i = 0; i < 4; i++)
|
||||||
|
// circle(debugimg, cornersInOriginalImg[i], 4, Scalar(0,255,0), -1);
|
||||||
|
// drawAndWait(&debugimg);
|
||||||
|
|
||||||
|
|
||||||
|
// for (int i = 0; i < 4; i++)
|
||||||
|
// circle(pipeline_data->colorImg, cornersInOriginalImg[i], 4, Scalar(0,255,0), -1);
|
||||||
|
|
||||||
|
return cornersInOriginalImg;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -22,12 +22,19 @@
|
|||||||
|
|
||||||
#include "opencv2/imgproc/imgproc.hpp"
|
#include "opencv2/imgproc/imgproc.hpp"
|
||||||
#include "pipeline_data.h"
|
#include "pipeline_data.h"
|
||||||
|
#include "transformation.h"
|
||||||
|
#include "platelines.h"
|
||||||
|
#include "platecorners.h"
|
||||||
|
|
||||||
class EdgeFinder {
|
class EdgeFinder {
|
||||||
public:
|
public:
|
||||||
EdgeFinder(PipelineData* pipeline_data);
|
EdgeFinder(PipelineData* pipeline_data);
|
||||||
virtual ~EdgeFinder();
|
virtual ~EdgeFinder();
|
||||||
|
|
||||||
|
std::vector<cv::Point2f> findEdgeCorners();
|
||||||
|
|
||||||
|
float confidence;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PipelineData* pipeline_data;
|
PipelineData* pipeline_data;
|
||||||
|
|
||||||
|
@@ -22,8 +22,8 @@
|
|||||||
using namespace cv;
|
using namespace cv;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
PlateCorners::PlateCorners(Mat inputImage, PlateLines* plateLines, PipelineData* pipelineData) :
|
PlateCorners::PlateCorners(Mat inputImage, PlateLines* plateLines, PipelineData* pipelineData, vector<TextLine> textLines) :
|
||||||
tlc(pipelineData->textLines)
|
tlc(textLines)
|
||||||
{
|
{
|
||||||
this->pipelineData = pipelineData;
|
this->pipelineData = pipelineData;
|
||||||
|
|
||||||
@@ -32,6 +32,7 @@ PlateCorners::PlateCorners(Mat inputImage, PlateLines* plateLines, PipelineData*
|
|||||||
|
|
||||||
this->inputImage = inputImage;
|
this->inputImage = inputImage;
|
||||||
this->plateLines = plateLines;
|
this->plateLines = plateLines;
|
||||||
|
this->textLines = textLines;
|
||||||
|
|
||||||
this->bestHorizontalScore = 9999999999999;
|
this->bestHorizontalScore = 9999999999999;
|
||||||
this->bestVerticalScore = 9999999999999;
|
this->bestVerticalScore = 9999999999999;
|
||||||
@@ -83,10 +84,10 @@ vector<Point> PlateCorners::findPlateCorners()
|
|||||||
Mat imgCorners = Mat(inputImage.size(), inputImage.type());
|
Mat imgCorners = Mat(inputImage.size(), inputImage.type());
|
||||||
inputImage.copyTo(imgCorners);
|
inputImage.copyTo(imgCorners);
|
||||||
|
|
||||||
for (uint linenum = 0; linenum < pipelineData->textLines.size(); linenum++)
|
for (uint linenum = 0; linenum < textLines.size(); linenum++)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
circle(imgCorners, pipelineData->textLines[linenum].textArea[i], 2, Scalar(0, 0, 0));
|
circle(imgCorners, textLines[linenum].textArea[i], 2, Scalar(0, 0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
line(imgCorners, this->bestTop.p1, this->bestTop.p2, Scalar(255, 0, 0), 1, CV_AA);
|
line(imgCorners, this->bestTop.p1, this->bestTop.p2, Scalar(255, 0, 0), 1, CV_AA);
|
||||||
|
@@ -28,8 +28,8 @@
|
|||||||
|
|
||||||
#define NO_LINE -1
|
#define NO_LINE -1
|
||||||
|
|
||||||
#define SCORING_MISSING_SEGMENT_PENALTY_VERTICAL 10
|
#define SCORING_MISSING_SEGMENT_PENALTY_VERTICAL 20
|
||||||
#define SCORING_MISSING_SEGMENT_PENALTY_HORIZONTAL 1
|
#define SCORING_MISSING_SEGMENT_PENALTY_HORIZONTAL 10
|
||||||
|
|
||||||
#define SCORING_BOXINESS_WEIGHT 0.8
|
#define SCORING_BOXINESS_WEIGHT 0.8
|
||||||
#define SCORING_PLATEHEIGHT_WEIGHT 2.2
|
#define SCORING_PLATEHEIGHT_WEIGHT 2.2
|
||||||
@@ -47,7 +47,7 @@ class PlateCorners
|
|||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PlateCorners(cv::Mat inputImage, PlateLines* plateLines, PipelineData* pipelineData) ;
|
PlateCorners(cv::Mat inputImage, PlateLines* plateLines, PipelineData* pipelineData, std::vector<TextLine> textLines) ;
|
||||||
|
|
||||||
virtual ~PlateCorners();
|
virtual ~PlateCorners();
|
||||||
|
|
||||||
@@ -60,6 +60,7 @@ class PlateCorners
|
|||||||
PipelineData* pipelineData;
|
PipelineData* pipelineData;
|
||||||
cv::Mat inputImage;
|
cv::Mat inputImage;
|
||||||
|
|
||||||
|
std::vector<TextLine> textLines;
|
||||||
TextLineCollection tlc;
|
TextLineCollection tlc;
|
||||||
|
|
||||||
float bestHorizontalScore;
|
float bestHorizontalScore;
|
||||||
|
@@ -39,7 +39,7 @@ PlateLines::~PlateLines()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlateLines::processImage(Mat inputImage, float sensitivity)
|
void PlateLines::processImage(Mat inputImage, vector<TextLine> textLines, float sensitivity)
|
||||||
{
|
{
|
||||||
if (this->debug)
|
if (this->debug)
|
||||||
cout << "PlateLines findLines" << endl;
|
cout << "PlateLines findLines" << endl;
|
||||||
@@ -73,10 +73,10 @@ void PlateLines::processImage(Mat inputImage, float sensitivity)
|
|||||||
|
|
||||||
Mat mask = Mat::zeros(inputImage.size(), CV_8U);
|
Mat mask = Mat::zeros(inputImage.size(), CV_8U);
|
||||||
|
|
||||||
for (uint i = 0; i < pipelineData->textLines.size(); i++)
|
for (uint i = 0; i < textLines.size(); i++)
|
||||||
{
|
{
|
||||||
vector<vector<Point> > polygons;
|
vector<vector<Point> > polygons;
|
||||||
polygons.push_back(pipelineData->textLines[i].textArea);
|
polygons.push_back(textLines[i].textArea);
|
||||||
fillPoly(mask, polygons, Scalar(255,255,255));
|
fillPoly(mask, polygons, Scalar(255,255,255));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -40,7 +40,7 @@ class PlateLines
|
|||||||
PlateLines(PipelineData* pipelineData);
|
PlateLines(PipelineData* pipelineData);
|
||||||
virtual ~PlateLines();
|
virtual ~PlateLines();
|
||||||
|
|
||||||
void processImage(cv::Mat img, float sensitivity=1.0);
|
void processImage(cv::Mat img, std::vector<TextLine> textLines, float sensitivity=1.0);
|
||||||
|
|
||||||
std::vector<PlateLine> horizontalLines;
|
std::vector<PlateLine> horizontalLines;
|
||||||
std::vector<PlateLine> verticalLines;
|
std::vector<PlateLine> verticalLines;
|
||||||
|
@@ -57,19 +57,12 @@ void LicensePlateCandidate::recognize()
|
|||||||
|
|
||||||
if (textAnalysis.confidence > 10)
|
if (textAnalysis.confidence > 10)
|
||||||
{
|
{
|
||||||
PlateLines plateLines(pipeline_data);
|
|
||||||
|
|
||||||
if (pipeline_data->hasPlateBorder)
|
|
||||||
plateLines.processImage(pipeline_data->plateBorderMask, 1.10);
|
|
||||||
|
|
||||||
plateLines.processImage(pipeline_data->crop_gray, 0.9);
|
|
||||||
|
|
||||||
PlateCorners cornerFinder(pipeline_data->crop_gray, &plateLines, pipeline_data);
|
|
||||||
vector<Point> smallPlateCorners = cornerFinder.findPlateCorners();
|
|
||||||
|
|
||||||
EdgeFinder edgeFinder(pipeline_data);
|
EdgeFinder edgeFinder(pipeline_data);
|
||||||
|
|
||||||
if (cornerFinder.confidence > 0)
|
pipeline_data->plate_corners = edgeFinder.findEdgeCorners();
|
||||||
|
|
||||||
|
if (edgeFinder.confidence > 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
timespec startTime;
|
timespec startTime;
|
||||||
@@ -79,9 +72,9 @@ void LicensePlateCandidate::recognize()
|
|||||||
Mat originalCrop = pipeline_data->crop_gray;
|
Mat originalCrop = pipeline_data->crop_gray;
|
||||||
|
|
||||||
Transformation imgTransform(this->pipeline_data->grayImg, pipeline_data->crop_gray, expandedRegion);
|
Transformation imgTransform(this->pipeline_data->grayImg, pipeline_data->crop_gray, expandedRegion);
|
||||||
pipeline_data->plate_corners = imgTransform.transformSmallPointsToBigImage(smallPlateCorners);
|
|
||||||
|
|
||||||
Size cropSize = getCropSize(pipeline_data->plate_corners);
|
Size cropSize = imgTransform.getCropSize(pipeline_data->plate_corners,
|
||||||
|
Size(pipeline_data->config->ocrImageWidthPx, pipeline_data->config->ocrImageHeightPx));
|
||||||
Mat transmtx = imgTransform.getTransformationMatrix(pipeline_data->plate_corners, cropSize);
|
Mat transmtx = imgTransform.getTransformationMatrix(pipeline_data->plate_corners, cropSize);
|
||||||
pipeline_data->crop_gray = imgTransform.crop(cropSize, transmtx);
|
pipeline_data->crop_gray = imgTransform.crop(cropSize, transmtx);
|
||||||
|
|
||||||
@@ -130,25 +123,4 @@ void LicensePlateCandidate::recognize()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Size LicensePlateCandidate::getCropSize(vector<Point2f> areaCorners)
|
|
||||||
{
|
|
||||||
// Figure out the approximate width/height of the license plate region, so we can maintain the aspect ratio.
|
|
||||||
LineSegment leftEdge(round(areaCorners[3].x), round(areaCorners[3].y), round(areaCorners[0].x), round(areaCorners[0].y));
|
|
||||||
LineSegment rightEdge(round(areaCorners[2].x), round(areaCorners[2].y), round(areaCorners[1].x), round(areaCorners[1].y));
|
|
||||||
LineSegment topEdge(round(areaCorners[0].x), round(areaCorners[0].y), round(areaCorners[1].x), round(areaCorners[1].y));
|
|
||||||
LineSegment bottomEdge(round(areaCorners[3].x), round(areaCorners[3].y), round(areaCorners[2].x), round(areaCorners[2].y));
|
|
||||||
|
|
||||||
float w = distanceBetweenPoints(leftEdge.midpoint(), rightEdge.midpoint());
|
|
||||||
float h = distanceBetweenPoints(bottomEdge.midpoint(), topEdge.midpoint());
|
|
||||||
float aspect = w/h;
|
|
||||||
int width = config->ocrImageWidthPx;
|
|
||||||
int height = round(((float) width) / aspect);
|
|
||||||
if (height > config->ocrImageHeightPx)
|
|
||||||
{
|
|
||||||
height = config->ocrImageHeightPx;
|
|
||||||
width = round(((float) height) * aspect);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Size(width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user