Commit c722968d9e176953c72a4d6216fe6631e105796b

Authored by Brendan K
2 parents 5a31eb19 f2d54abc

Merge pull request #342 from biometrics/pills_crop

Crop from mask
openbr/plugins/imgproc/cropfrommask.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 image based on mask metadata
  11 + * \author Brendan Klare \cite bklare
  12 + */
  13 +class CropFromMaskTransform : public UntrainableTransform
  14 +{
  15 + Q_OBJECT
  16 + Q_PROPERTY(bool fixedAspectRatio READ get_fixedAspectRatio WRITE set_fixedAspectRatio RESET reset_fixedAspectRatio STORED false)
  17 + BR_PROPERTY(bool, fixedAspectRatio, true)
  18 +
  19 +private:
  20 +
  21 + void project(const Template &src, Template &dst) const
  22 + {
  23 + dst = src;
  24 +
  25 + Mat mask = dst.file.get<Mat>("Mask");
  26 +
  27 + int w = mask.rows;
  28 + int h = mask.cols;
  29 + int left = w;
  30 + int right = 0;
  31 + int top = h;
  32 + int bottom = 0;
  33 + for (int i = 0 ; i < w; i++) {
  34 + for (int j = 0 ; j < h; j++) {
  35 + if (mask.at<unsigned char>(i,j)) {
  36 + if (i < left)
  37 + left = i;
  38 + if (i > right)
  39 + right = i;
  40 + if (j < top)
  41 + top = j;
  42 + if (j > bottom)
  43 + bottom = j;
  44 + }
  45 + }
  46 + }
  47 +
  48 + if (fixedAspectRatio) {
  49 + h = bottom - top + 1;
  50 + w = right - left + 1;
  51 + if (h > w) {
  52 + int h2 = (h - w) / 2;
  53 + right += h2;
  54 + left -= h2;
  55 + } else {
  56 + int w2 = (w - h) / 2;
  57 + bottom += w2;
  58 + top -= w2;
  59 + }
  60 + }
  61 +
  62 + dst.m() = Mat(src.m(), Rect(top, left, bottom - top + 1, right - left + 1));
  63 +
  64 + }
  65 +};
  66 +
  67 +BR_REGISTER(Transform, CropFromMaskTransform)
  68 +
  69 +} // namespace br
  70 +
  71 +#include "imgproc/cropfrommask.moc"
... ...