diff --git a/src/openalpr/transformation.cpp b/src/openalpr/transformation.cpp index ce9afbb..b3f32af 100644 --- a/src/openalpr/transformation.cpp +++ b/src/openalpr/transformation.cpp @@ -34,6 +34,17 @@ Transformation::~Transformation() { // Re-maps the coordinates from the smallImage to the coordinate space of the bigImage. vector Transformation::transformSmallPointsToBigImage(vector points) +{ + vector floatPoints; + for (unsigned int i = 0; i < points.size(); i++) + floatPoints.push_back(points[i]); + + return transformSmallPointsToBigImage(floatPoints); + +} + +// Re-maps the coordinates from the smallImage to the coordinate space of the bigImage. +vector Transformation::transformSmallPointsToBigImage(vector points) { vector bigPoints; for (uint i = 0; i < points.size(); i++) @@ -60,12 +71,19 @@ Mat Transformation::getTransformationMatrix(vector corners, Size output quad_pts.push_back(Point2f(outputImageSize.width, outputImageSize.height)); quad_pts.push_back(Point2f(0, outputImageSize.height)); + return getTransformationMatrix(corners, quad_pts); +} + +Mat Transformation::getTransformationMatrix(vector corners, vector outputCorners) +{ + // Get transformation matrix - Mat transmtx = getPerspectiveTransform(corners, quad_pts); + Mat transmtx = getPerspectiveTransform(corners, outputCorners); return transmtx; } + Mat Transformation::crop(Size outputImageSize, Mat transformationMatrix) { @@ -81,6 +99,15 @@ Mat Transformation::crop(Size outputImageSize, Mat transformationMatrix) return deskewed; } +vector Transformation::remapSmallPointstoCrop(vector smallPoints, cv::Mat transformationMatrix) +{ + vector floatPoints; + for (unsigned int i = 0; i < smallPoints.size(); i++) + floatPoints.push_back(smallPoints[i]); + + return remapSmallPointstoCrop(floatPoints, transformationMatrix); +} + vector Transformation::remapSmallPointstoCrop(vector smallPoints, cv::Mat transformationMatrix) { vector remappedPoints; @@ -89,3 +116,24 @@ vector Transformation::remapSmallPointstoCrop(vector smallPoin return remappedPoints; } +Size Transformation::getCropSize(vector areaCorners, Size targetSize) +{ + // 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 = targetSize.width; + int height = round(((float) width) / aspect); + if (height > targetSize.height) + { + height = targetSize.height; + width = round(((float) height) * aspect); + } + + return Size(width, height); +} \ No newline at end of file diff --git a/src/openalpr/transformation.h b/src/openalpr/transformation.h index 528f87a..67bbe97 100644 --- a/src/openalpr/transformation.h +++ b/src/openalpr/transformation.h @@ -21,6 +21,7 @@ #define OPENALPR_TRANSFORMATION_H #include "opencv2/imgproc/imgproc.hpp" +#include "utility.h" class Transformation { public: @@ -28,12 +29,16 @@ public: virtual ~Transformation(); std::vector transformSmallPointsToBigImage(std::vector points); + std::vector transformSmallPointsToBigImage(std::vector points); cv::Mat getTransformationMatrix(std::vector corners, cv::Size outputImageSize); + cv::Mat getTransformationMatrix(std::vector corners, std::vector outputCorners); cv::Mat crop(cv::Size outputImageSize, cv::Mat transformationMatrix); + std::vector remapSmallPointstoCrop(std::vector smallPoints, cv::Mat transformationMatrix); std::vector remapSmallPointstoCrop(std::vector smallPoints, cv::Mat transformationMatrix); + cv::Size getCropSize(std::vector areaCorners, cv::Size targetSize); private: cv::Mat bigImage;