From 320c4e7bb09b7d0c5715ddd1c990535862a786ed Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Tue, 11 Oct 2016 15:21:14 -0600 Subject: [PATCH] OpenCVUtils rotate cleanup and exposed new functions --- openbr/core/opencvutils.cpp | 67 ++++++++++++++++++++++++++++++------------------------------------- openbr/core/opencvutils.h | 2 ++ 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/openbr/core/opencvutils.cpp b/openbr/core/opencvutils.cpp index a2b9884..b844734 100644 --- a/openbr/core/opencvutils.cpp +++ b/openbr/core/opencvutils.cpp @@ -579,55 +579,48 @@ void OpenCVUtils::pad(const br::TemplateList &src, br::TemplateList &dst, bool p } } +QPointF OpenCVUtils::rotatePoint(const QPointF &point, const Mat &rotationMatrix) +{ + return QPointF(point.x() * rotationMatrix.at(0,0) + + point.y() * rotationMatrix.at(0,1) + + 1 * rotationMatrix.at(0,2), + point.x() * rotationMatrix.at(1,0) + + point.y() * rotationMatrix.at(1,1) + + 1 * rotationMatrix.at(1,2)); +} + +QList OpenCVUtils::rotatePoints(const QList &points, const Mat &rotationMatrix) +{ + QList rotatedPoints; + foreach (const QPointF &point, points) + rotatedPoints.append(rotatePoint(point, rotationMatrix)); + return rotatedPoints; +} + void OpenCVUtils::rotate(const br::Template &src, br::Template &dst, float degrees, bool rotateMat, bool rotatePoints, bool rotateRects, const QPointF ¢er) { - Point2f c = center.isNull() ? Point2f(src.m().rows/2,src.m().cols/2) : toPoint(center); - Mat rotMatrix = getRotationMatrix2D(c,degrees,1.0); + const Mat rotMatrix = getRotationMatrix2D(center.isNull() ? Point2f(src.m().rows/2,src.m().cols/2) : toPoint(center), + degrees, 1.0); if (rotateMat) { warpAffine(src,dst,rotMatrix,Size(src.m().cols,src.m().rows),INTER_AREA,BORDER_REPLICATE); dst.file = src.file; - } else + } else { dst = src; - - if (rotatePoints) { - QList points = src.file.points(); - QList rotatedPoints; - for (int i=0; i(0,0)+ - points.at(i).y()*rotMatrix.at(0,1)+ - rotMatrix.at(0,2), - points.at(i).x()*rotMatrix.at(1,0)+ - points.at(i).y()*rotMatrix.at(1,1)+ - rotMatrix.at(1,2))); - } - - dst.file.setPoints(rotatedPoints); } + if (rotatePoints) + dst.file.setPoints(OpenCVUtils::rotatePoints(src.file.points(), rotMatrix)); + if (rotateRects) { - QList rects = src.file.rects(); QList rotatedRects; - for (int i=0; i corners; - corners << rects[i].topLeft() << rects[i].topRight() << rects[i].bottomLeft() << rects[i].bottomRight(); - - QList rotatedCorners; - foreach (const QPointF &corner, corners) - rotatedCorners.append(QPointF(corner.x() * rotMatrix.at(0,0) + - corner.y() * rotMatrix.at(0,1) + - rotMatrix.at(0,2), - corner.x() * rotMatrix.at(1,0) + - corner.y() * rotMatrix.at(1,1) + - rotMatrix.at(1,2))); - - float top = Common::Min(QList() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y()); - float left = Common::Min(QList() << rotatedCorners[0].x() << rotatedCorners[2].x() << rotatedCorners[1].x() << rotatedCorners[3].x()); - float bottom = Common::Max(QList() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y()); - float right = Common::Max(QList() << rotatedCorners[0].x() << rotatedCorners[2].x() << rotatedCorners[1].x() << rotatedCorners[3].x()); - + foreach (const QRectF &rect, src.file.rects()) { + const QList rotatedCorners = OpenCVUtils::rotatePoints(QList() << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight(), rotMatrix); + const float top = Common::Min(QList() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y()); + const float left = Common::Min(QList() << rotatedCorners[0].x() << rotatedCorners[1].x() << rotatedCorners[2].x() << rotatedCorners[3].x()); + const float bottom = Common::Max(QList() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y()); + const float right = Common::Max(QList() << rotatedCorners[0].x() << rotatedCorners[1].x() << rotatedCorners[2].x() << rotatedCorners[3].x()); rotatedRects.append(QRectF(QPointF(left,top),QPointF(right,bottom))); } - dst.file.setRects(rotatedRects); } } diff --git a/openbr/core/opencvutils.h b/openbr/core/opencvutils.h index 888ec85..08abd6a 100644 --- a/openbr/core/opencvutils.h +++ b/openbr/core/opencvutils.h @@ -105,6 +105,8 @@ namespace OpenCVUtils void group(QList &rects, QList &confidences, float confidenceThreshold, int minNeighbors, float epsilon, bool useMax=false, QList *maxIndices=NULL); void pad(const br::Template &src, br::Template &dst, bool padMat, const QList &padding, bool padPoints, bool padRects, int border=0, int value=0); void pad(const br::TemplateList &src, br::TemplateList &dst, bool padMat, const QList &padding, bool padPoints, bool padRects, int border=0, int value=0); + QPointF rotatePoint(const QPointF &point, const cv::Mat &rotationMatrix); + QList rotatePoints(const QList &points, const cv::Mat &rotationMatrix); void rotate(const br::Template &src, br::Template &dst, float degrees, bool rotateMat=true, bool rotatePoints=true, bool rotateRects=true, const QPointF ¢er = QPointF()); void rotate(const br::TemplateList &src, br::TemplateList &dst, float degrees, bool rotateMat=true, bool rotatePoint=true, bool rotateRects=true, const QPointF ¢er = QPointF()); void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); -- libgit2 0.21.4