Commit 571cabff54cb54b7fd23932eeddbaedd45837269

Authored by bklare
1 parent a890d39b

Cropped image based on landmark indices

openbr/plugins/imgproc/cropfromlandmarks.cpp 0 → 100644
  1 +#include <openbr/plugins/openbr_internal.h>
  2 +
  3 +using namespace cv;
  4 +
  5 +namespace br
  6 +{
  7 +
  8 +/*!
  9 + * \ingroup transforms
  10 + * \brief Crops around the landmarks numbers provided.
  11 + * \author Brendan Klare \cite bklare
  12 + * \param padding Percentage of height and width to pad the image.
  13 + */
  14 +class CropFromLandmarksTransform : public UntrainableTransform
  15 +{
  16 + Q_OBJECT
  17 +
  18 + Q_PROPERTY(QList<int> indices READ get_indices WRITE set_indices RESET reset_indices STORED false)
  19 + Q_PROPERTY(float paddingHorizontal READ get_paddingHorizontal WRITE set_paddingHorizontal RESET reset_paddingHorizontal STORED false)
  20 + Q_PROPERTY(float paddingVertical READ get_paddingVertical WRITE set_paddingVertical RESET reset_paddingVertical STORED false)
  21 + BR_PROPERTY(QList<int>, indices, QList<int>())
  22 + BR_PROPERTY(float, paddingHorizontal, .1)
  23 + BR_PROPERTY(float, paddingVertical, .1)
  24 +
  25 + void project(const Template &src, Template &dst) const
  26 + {
  27 + int minX = src.m().cols - 1,
  28 + maxX = 1,
  29 + minY = src.m().rows - 1,
  30 + maxY = 1;
  31 +
  32 + for (int i = 0; i <indices.size(); i++) {
  33 + if (minX > src.file.points()[indices[i]].x())
  34 + minX = src.file.points()[indices[i]].x();
  35 + if (minY > src.file.points()[indices[i]].y())
  36 + minY = src.file.points()[indices[i]].y();
  37 + if (maxX < src.file.points()[indices[i]].x())
  38 + maxX = src.file.points()[indices[i]].x();
  39 + if (maxY < src.file.points()[indices[i]].y())
  40 + maxY = src.file.points()[indices[i]].y();
  41 + }
  42 +
  43 + int padW = qRound((maxX - minX) * (paddingHorizontal / 2));
  44 + int padH = qRound((maxY - minY) * (paddingVertical / 2));
  45 +
  46 + dst = Mat(src, Rect(minX - padW, minY - padH, (maxX - minX + 1) + padW * 2, (maxY - minY + 1) + padH * 2));
  47 + }
  48 +};
  49 +
  50 +BR_REGISTER(Transform, CropFromLandmarksTransform)
  51 +
  52 +} // namespace br
  53 +
  54 +#include "imgproc/cropfromlandmarks.moc"
... ...