Commit a3050f2ed75437b06ccccdf6b14fa95bea4275cc

Authored by Scott Klum
1 parent c2b1db42

Added rotation code to OpenCVUtils

openbr/core/opencvutils.cpp
... ... @@ -541,6 +541,67 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con
541 541 }
542 542 }
543 543  
  544 +void OpenCVUtils::rotate(const br::Template &src, br::Template &dst, int degrees, bool rotateMat, bool rotatePoints, bool rotateRects)
  545 +{
  546 + Mat rotMatrix = getRotationMatrix2D(Point2f(src.m().rows/2,src.m().cols/2),degrees,1.0);
  547 + if (rotateMat) {
  548 + warpAffine(src,dst,rotMatrix,Size(src.m().cols,src.m().rows),INTER_LINEAR,BORDER_REFLECT_101);
  549 + dst.file = src.file;
  550 + } else
  551 + dst = src;
  552 +
  553 + if (rotatePoints) {
  554 + QList<QPointF> points = src.file.points();
  555 + QList<QPointF> rotatedPoints;
  556 + for (int i=0; i<points.size(); i++) {
  557 + rotatedPoints.append(QPointF(points.at(i).x()*rotMatrix.at<double>(0,0)+
  558 + points.at(i).y()*rotMatrix.at<double>(0,1)+
  559 + rotMatrix.at<double>(0,2),
  560 + points.at(i).x()*rotMatrix.at<double>(1,0)+
  561 + points.at(i).y()*rotMatrix.at<double>(1,1)+
  562 + rotMatrix.at<double>(1,2)));
  563 + }
  564 +
  565 + dst.file.setPoints(rotatedPoints);
  566 + }
  567 +
  568 + if (rotateRects) {
  569 + QList<QRectF> rects = src.file.rects();
  570 + QList<QRectF> rotatedRects;
  571 + for (int i=0; i<rects.size(); i++) {
  572 + QList<QPointF> corners;
  573 + corners << rects[i].topLeft() << rects[i].topRight() << rects[i].bottomLeft() << rects[i].bottomRight();
  574 +
  575 + QList<QPointF> rotatedCorners;
  576 + foreach (const QPointF &corner, corners)
  577 + rotatedCorners.append(QPointF(corner.x() * rotMatrix.at<double>(0,0) +
  578 + corner.y() * rotMatrix.at<double>(0,1) +
  579 + rotMatrix.at<double>(0,2),
  580 + corner.x() * rotMatrix.at<double>(1,0) +
  581 + corner.y() * rotMatrix.at<double>(1,1) +
  582 + rotMatrix.at<double>(1,2)));
  583 +
  584 + float top = min(rotatedCorners[0].y(), rotatedCorners[1].y());
  585 + float left = min(rotatedCorners[0].x(), rotatedCorners[2].x());
  586 + float bottom = max(rotatedCorners[2].y(), rotatedCorners[3].y());
  587 + float right = max(rotatedCorners[1].x(), rotatedCorners[3].x());
  588 +
  589 + rotatedRects.append(QRectF(QPointF(left,top),QPointF(right,bottom)));
  590 + }
  591 +
  592 + dst.file.setRects(rotatedRects);
  593 + }
  594 +}
  595 +
  596 +void OpenCVUtils::rotate(const br::TemplateList &src, br::TemplateList &dst, int degrees, bool rotateMat, bool rotatePoints, bool rotateRects)
  597 +{
  598 + for (int i=0; i<src.size(); i++) {
  599 + br::Template t;
  600 + flip(src[i], t, degrees, rotateMat, rotatePoints, rotateRects);
  601 + dst.append(t);
  602 + }
  603 +}
  604 +
544 605 void OpenCVUtils::flip(const br::Template &src, br::Template &dst, int axis, bool flipMat, bool flipPoints, bool flipRects)
545 606 {
546 607 if (flipMat) {
... ...
openbr/core/opencvutils.h
... ... @@ -103,6 +103,8 @@ namespace OpenCVUtils
103 103  
104 104 // Misc
105 105 void group(QList<cv::Rect> &rects, QList<float> &confidences, float confidenceThreshold, int minNeighbors, float epsilon);
  106 + void rotate(const br::Template &src, br::Template &dst, int degrees, bool rotateMat=true, bool rotatePoints=true, bool rotateRects=true);
  107 + void rotate(const br::TemplateList &src, br::TemplateList &dst, int degrees, bool rotateMat=true, bool rotatePoint=true, bool rotateRects=true);
106 108 void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true);
107 109 void flip(const br::TemplateList &src, br::TemplateList &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true);
108 110  
... ...
openbr/plugins/imgproc/rndrotate.cpp
... ... @@ -40,21 +40,7 @@ class RndRotateTransform : public UntrainableTransform
40 40 void project(const Template &src, Template &dst) const {
41 41 int span = range.first() - range.last();
42 42 int angle = span == 0 ? range.first() : (rand() % span) + range.first();
43   - Mat rotMatrix = getRotationMatrix2D(center == -1 ? Point2f(src.m().rows/2,src.m().cols/2) : OpenCVUtils::toPoint(src.file.points()[center]),angle,1.0);
44   - warpAffine(src,dst,rotMatrix,Size(src.m().cols,src.m().rows),INTER_LINEAR,BORDER_REFLECT_101);
45   -
46   - QList<QPointF> points = src.file.points();
47   - QList<QPointF> rotatedPoints;
48   - for (int i=0; i<points.size(); i++) {
49   - rotatedPoints.append(QPointF(points.at(i).x()*rotMatrix.at<double>(0,0)+
50   - points.at(i).y()*rotMatrix.at<double>(0,1)+
51   - rotMatrix.at<double>(0,2),
52   - points.at(i).x()*rotMatrix.at<double>(1,0)+
53   - points.at(i).y()*rotMatrix.at<double>(1,1)+
54   - rotMatrix.at<double>(1,2)));
55   - }
56   -
57   - dst.file.setPoints(rotatedPoints);
  43 + OpenCVUtils::rotate(src, dst, angle);
58 44 }
59 45 };
60 46  
... ...