Commit 68b3bef7f3f287674ebde4500cc08fb52df03947

Authored by bklare
1 parent ae78f5a2

Randomly shift an image

openbr/plugins/imgproc/rndtranslate.cpp 0 → 100644
  1 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2 + * Copyright 2015 Rank One Computing
  3 + * *
  4 + * Licensed under the Apache License, Version 2.0 (the "License"); *
  5 + * you may not use this file except in compliance with the License. *
  6 + * You may obtain a copy of the License at *
  7 + * *
  8 + * http://www.apache.org/licenses/LICENSE-2.0 *
  9 + * *
  10 + * Unless required by applicable law or agreed to in writing, software *
  11 + * distributed under the License is distributed on an "AS IS" BASIS, *
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  13 + * See the License for the specific language governing permissions and *
  14 + * limitations under the License. *
  15 + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  16 +
  17 +#include <opencv2/imgproc/imgproc.hpp>
  18 +#include <openbr/plugins/openbr_internal.h>
  19 +#include <openbr/core/opencvutils.h>
  20 +
  21 +using namespace cv;
  22 +
  23 +namespace br
  24 +{
  25 +
  26 +/*!
  27 + * \ingroup transforms
  28 + * \brief Randomly translate an image based on the height of a face contained within
  29 + * \br_param maxHeight The max percentage of face height to shift the image
  30 + * \author Brendan Klare \cite bklare
  31 + */
  32 +class RndTranslateTransform : public UntrainableMetaTransform
  33 +{
  34 + Q_OBJECT
  35 +
  36 + Q_PROPERTY(float maxHeight READ get_maxHeight WRITE set_maxHeight RESET reset_maxHeight STORED false)
  37 + BR_PROPERTY(float, maxHeight, .1)
  38 + Q_PROPERTY(int nStages READ get_nStages WRITE set_nStages RESET reset_nStages STORED false)
  39 + BR_PROPERTY(int, nStages, 3)
  40 +
  41 + void project(const Template &src, Template &dst) const
  42 + {
  43 + qFatal("Shoult not be here (RndTranslate)");
  44 + }
  45 +
  46 + void project(const TemplateList &srcList, TemplateList &dstList) const
  47 + {
  48 + foreach (const Template &src, srcList) {
  49 + for (int stage = 0; stage < nStages; stage++) {
  50 +
  51 + Template dst = src;
  52 +
  53 + QPointF rightEye = src.file.get<QPointF>("RightEye");
  54 + QPointF leftEye = src.file.get<QPointF>("LeftEye");
  55 + QPointF chin = src.file.get<QPointF>("Chin");
  56 + QPointF eyeCenter = (rightEye + leftEye) / 2;
  57 + const float length = sqrt(pow(eyeCenter.x() - chin.x(), 2.0) +
  58 + pow(eyeCenter.y() - chin.y(), 2.0));
  59 +
  60 + int max = qRound(length * maxHeight);
  61 + int shiftX = (rand() % (max * 2 + 1)) - max;
  62 + int shiftY = (rand() % (max * 2 + 1)) - max;
  63 + //Mat out(src.m().rows, src.m().cols, src.m().type());
  64 + Mat out;
  65 + Mat M = Mat::zeros(2, 3, CV_32F);
  66 + M.at<float>(0,0) = 1;
  67 + M.at<float>(1,1) = 1;
  68 + M.at<float>(0,2) = shiftX;
  69 + M.at<float>(1,2) = shiftY;
  70 + warpAffine(src.m(), out, M, Size(src.m().rows, src.m().cols));
  71 +
  72 + dst.m() = out;
  73 + dstList += dst;
  74 + }
  75 + }
  76 + }
  77 +};
  78 +
  79 +BR_REGISTER(Transform, RndTranslateTransform)
  80 +
  81 +} // namespace br
  82 +
  83 +#include "imgproc/rndtranslate.moc"