From a3050f2ed75437b06ccccdf6b14fa95bea4275cc Mon Sep 17 00:00:00 2001 From: Scott Klum Date: Mon, 9 Nov 2015 16:53:49 -0500 Subject: [PATCH] Added rotation code to OpenCVUtils --- openbr/core/opencvutils.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/core/opencvutils.h | 2 ++ openbr/plugins/imgproc/rndrotate.cpp | 16 +--------------- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/openbr/core/opencvutils.cpp b/openbr/core/opencvutils.cpp index 1583188..2816883 100644 --- a/openbr/core/opencvutils.cpp +++ b/openbr/core/opencvutils.cpp @@ -541,6 +541,67 @@ void OpenCVUtils::group(QList &rects, QList &confidences, float con } } +void OpenCVUtils::rotate(const br::Template &src, br::Template &dst, int degrees, bool rotateMat, bool rotatePoints, bool rotateRects) +{ + Mat rotMatrix = getRotationMatrix2D(Point2f(src.m().rows/2,src.m().cols/2),degrees,1.0); + if (rotateMat) { + warpAffine(src,dst,rotMatrix,Size(src.m().cols,src.m().rows),INTER_LINEAR,BORDER_REFLECT_101); + dst.file = src.file; + } 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 (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 = min(rotatedCorners[0].y(), rotatedCorners[1].y()); + float left = min(rotatedCorners[0].x(), rotatedCorners[2].x()); + float bottom = max(rotatedCorners[2].y(), rotatedCorners[3].y()); + float right = max(rotatedCorners[1].x(), rotatedCorners[3].x()); + + rotatedRects.append(QRectF(QPointF(left,top),QPointF(right,bottom))); + } + + dst.file.setRects(rotatedRects); + } +} + +void OpenCVUtils::rotate(const br::TemplateList &src, br::TemplateList &dst, int degrees, bool rotateMat, bool rotatePoints, bool rotateRects) +{ + for (int i=0; i &rects, QList &confidences, float confidenceThreshold, int minNeighbors, float epsilon); + void rotate(const br::Template &src, br::Template &dst, int degrees, bool rotateMat=true, bool rotatePoints=true, bool rotateRects=true); + void rotate(const br::TemplateList &src, br::TemplateList &dst, int degrees, bool rotateMat=true, bool rotatePoint=true, bool rotateRects=true); void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); void flip(const br::TemplateList &src, br::TemplateList &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); diff --git a/openbr/plugins/imgproc/rndrotate.cpp b/openbr/plugins/imgproc/rndrotate.cpp index 580607c..9df2722 100644 --- a/openbr/plugins/imgproc/rndrotate.cpp +++ b/openbr/plugins/imgproc/rndrotate.cpp @@ -40,21 +40,7 @@ class RndRotateTransform : public UntrainableTransform void project(const Template &src, Template &dst) const { int span = range.first() - range.last(); int angle = span == 0 ? range.first() : (rand() % span) + range.first(); - Mat rotMatrix = getRotationMatrix2D(center == -1 ? Point2f(src.m().rows/2,src.m().cols/2) : OpenCVUtils::toPoint(src.file.points()[center]),angle,1.0); - warpAffine(src,dst,rotMatrix,Size(src.m().cols,src.m().rows),INTER_LINEAR,BORDER_REFLECT_101); - - 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); + OpenCVUtils::rotate(src, dst, angle); } }; -- libgit2 0.21.4