Commit 320c4e7bb09b7d0c5715ddd1c990535862a786ed

Authored by Josh Klontz
1 parent 6443dfdf

OpenCVUtils rotate cleanup and exposed new functions

openbr/core/opencvutils.cpp
@@ -579,55 +579,48 @@ void OpenCVUtils::pad(const br::TemplateList &src, br::TemplateList &dst, bool p @@ -579,55 +579,48 @@ void OpenCVUtils::pad(const br::TemplateList &src, br::TemplateList &dst, bool p
579 } 579 }
580 } 580 }
581 581
  582 +QPointF OpenCVUtils::rotatePoint(const QPointF &point, const Mat &rotationMatrix)
  583 +{
  584 + return QPointF(point.x() * rotationMatrix.at<double>(0,0) +
  585 + point.y() * rotationMatrix.at<double>(0,1) +
  586 + 1 * rotationMatrix.at<double>(0,2),
  587 + point.x() * rotationMatrix.at<double>(1,0) +
  588 + point.y() * rotationMatrix.at<double>(1,1) +
  589 + 1 * rotationMatrix.at<double>(1,2));
  590 +}
  591 +
  592 +QList<QPointF> OpenCVUtils::rotatePoints(const QList<QPointF> &points, const Mat &rotationMatrix)
  593 +{
  594 + QList<QPointF> rotatedPoints;
  595 + foreach (const QPointF &point, points)
  596 + rotatedPoints.append(rotatePoint(point, rotationMatrix));
  597 + return rotatedPoints;
  598 +}
  599 +
582 void OpenCVUtils::rotate(const br::Template &src, br::Template &dst, float degrees, bool rotateMat, bool rotatePoints, bool rotateRects, const QPointF &center) 600 void OpenCVUtils::rotate(const br::Template &src, br::Template &dst, float degrees, bool rotateMat, bool rotatePoints, bool rotateRects, const QPointF &center)
583 { 601 {
584 - Point2f c = center.isNull() ? Point2f(src.m().rows/2,src.m().cols/2) : toPoint(center);  
585 - Mat rotMatrix = getRotationMatrix2D(c,degrees,1.0); 602 + const Mat rotMatrix = getRotationMatrix2D(center.isNull() ? Point2f(src.m().rows/2,src.m().cols/2) : toPoint(center),
  603 + degrees, 1.0);
586 if (rotateMat) { 604 if (rotateMat) {
587 warpAffine(src,dst,rotMatrix,Size(src.m().cols,src.m().rows),INTER_AREA,BORDER_REPLICATE); 605 warpAffine(src,dst,rotMatrix,Size(src.m().cols,src.m().rows),INTER_AREA,BORDER_REPLICATE);
588 dst.file = src.file; 606 dst.file = src.file;
589 - } else 607 + } else {
590 dst = src; 608 dst = src;
591 -  
592 - if (rotatePoints) {  
593 - QList<QPointF> points = src.file.points();  
594 - QList<QPointF> rotatedPoints;  
595 - for (int i=0; i<points.size(); i++) {  
596 - rotatedPoints.append(QPointF(points.at(i).x()*rotMatrix.at<double>(0,0)+  
597 - points.at(i).y()*rotMatrix.at<double>(0,1)+  
598 - rotMatrix.at<double>(0,2),  
599 - points.at(i).x()*rotMatrix.at<double>(1,0)+  
600 - points.at(i).y()*rotMatrix.at<double>(1,1)+  
601 - rotMatrix.at<double>(1,2)));  
602 - }  
603 -  
604 - dst.file.setPoints(rotatedPoints);  
605 } 609 }
606 610
  611 + if (rotatePoints)
  612 + dst.file.setPoints(OpenCVUtils::rotatePoints(src.file.points(), rotMatrix));
  613 +
607 if (rotateRects) { 614 if (rotateRects) {
608 - QList<QRectF> rects = src.file.rects();  
609 QList<QRectF> rotatedRects; 615 QList<QRectF> rotatedRects;
610 - for (int i=0; i<rects.size(); i++) {  
611 - QList<QPointF> corners;  
612 - corners << rects[i].topLeft() << rects[i].topRight() << rects[i].bottomLeft() << rects[i].bottomRight();  
613 -  
614 - QList<QPointF> rotatedCorners;  
615 - foreach (const QPointF &corner, corners)  
616 - rotatedCorners.append(QPointF(corner.x() * rotMatrix.at<double>(0,0) +  
617 - corner.y() * rotMatrix.at<double>(0,1) +  
618 - rotMatrix.at<double>(0,2),  
619 - corner.x() * rotMatrix.at<double>(1,0) +  
620 - corner.y() * rotMatrix.at<double>(1,1) +  
621 - rotMatrix.at<double>(1,2)));  
622 -  
623 - float top = Common::Min(QList<float>() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y());  
624 - float left = Common::Min(QList<float>() << rotatedCorners[0].x() << rotatedCorners[2].x() << rotatedCorners[1].x() << rotatedCorners[3].x());  
625 - float bottom = Common::Max(QList<float>() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y());  
626 - float right = Common::Max(QList<float>() << rotatedCorners[0].x() << rotatedCorners[2].x() << rotatedCorners[1].x() << rotatedCorners[3].x());  
627 - 616 + foreach (const QRectF &rect, src.file.rects()) {
  617 + const QList<QPointF> rotatedCorners = OpenCVUtils::rotatePoints(QList<QPointF>() << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight(), rotMatrix);
  618 + const float top = Common::Min(QList<float>() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y());
  619 + const float left = Common::Min(QList<float>() << rotatedCorners[0].x() << rotatedCorners[1].x() << rotatedCorners[2].x() << rotatedCorners[3].x());
  620 + const float bottom = Common::Max(QList<float>() << rotatedCorners[0].y() << rotatedCorners[1].y() << rotatedCorners[2].y() << rotatedCorners[3].y());
  621 + const float right = Common::Max(QList<float>() << rotatedCorners[0].x() << rotatedCorners[1].x() << rotatedCorners[2].x() << rotatedCorners[3].x());
628 rotatedRects.append(QRectF(QPointF(left,top),QPointF(right,bottom))); 622 rotatedRects.append(QRectF(QPointF(left,top),QPointF(right,bottom)));
629 } 623 }
630 -  
631 dst.file.setRects(rotatedRects); 624 dst.file.setRects(rotatedRects);
632 } 625 }
633 } 626 }
openbr/core/opencvutils.h
@@ -105,6 +105,8 @@ namespace OpenCVUtils @@ -105,6 +105,8 @@ namespace OpenCVUtils
105 void group(QList<cv::Rect> &rects, QList<float> &confidences, float confidenceThreshold, int minNeighbors, float epsilon, bool useMax=false, QList<int> *maxIndices=NULL); 105 void group(QList<cv::Rect> &rects, QList<float> &confidences, float confidenceThreshold, int minNeighbors, float epsilon, bool useMax=false, QList<int> *maxIndices=NULL);
106 void pad(const br::Template &src, br::Template &dst, bool padMat, const QList<int> &padding, bool padPoints, bool padRects, int border=0, int value=0); 106 void pad(const br::Template &src, br::Template &dst, bool padMat, const QList<int> &padding, bool padPoints, bool padRects, int border=0, int value=0);
107 void pad(const br::TemplateList &src, br::TemplateList &dst, bool padMat, const QList<int> &padding, bool padPoints, bool padRects, int border=0, int value=0); 107 void pad(const br::TemplateList &src, br::TemplateList &dst, bool padMat, const QList<int> &padding, bool padPoints, bool padRects, int border=0, int value=0);
  108 + QPointF rotatePoint(const QPointF &point, const cv::Mat &rotationMatrix);
  109 + QList<QPointF> rotatePoints(const QList<QPointF> &points, const cv::Mat &rotationMatrix);
108 void rotate(const br::Template &src, br::Template &dst, float degrees, bool rotateMat=true, bool rotatePoints=true, bool rotateRects=true, const QPointF &center = QPointF()); 110 void rotate(const br::Template &src, br::Template &dst, float degrees, bool rotateMat=true, bool rotatePoints=true, bool rotateRects=true, const QPointF &center = QPointF());
109 void rotate(const br::TemplateList &src, br::TemplateList &dst, float degrees, bool rotateMat=true, bool rotatePoint=true, bool rotateRects=true, const QPointF &center = QPointF()); 111 void rotate(const br::TemplateList &src, br::TemplateList &dst, float degrees, bool rotateMat=true, bool rotatePoint=true, bool rotateRects=true, const QPointF &center = QPointF());
110 void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); 112 void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true);