Commit 3331fb37e47184ae4ab5878abd4afe41dce94576

Authored by Scott Klum
2 parents f7dddb1f 855da69b

Merge pull request #331 from biometrics/mask_updates

Mask updates
Showing 1 changed file with 75 additions and 2 deletions
openbr/plugins/mask.cpp
@@ -183,6 +183,76 @@ BR_REGISTER(Transform, LargestConvexAreaTransform) @@ -183,6 +183,76 @@ BR_REGISTER(Transform, LargestConvexAreaTransform)
183 183
184 /*! 184 /*!
185 * \ingroup transforms 185 * \ingroup transforms
  186 + * \brief Wraps OpenCV's adaptive thresholding.
  187 + * \author Scott Klum \cite sklum
  188 + */
  189 +class AdaptiveThresholdTransform : public UntrainableTransform
  190 +{
  191 + Q_OBJECT
  192 +
  193 + Q_ENUMS(Method)
  194 + Q_ENUMS(Type)
  195 + Q_PROPERTY(int maxValue READ get_maxValue WRITE set_maxValue RESET reset_maxValue STORED false)
  196 + Q_PROPERTY(Method method READ get_method WRITE set_method RESET reset_method STORED false)
  197 + Q_PROPERTY(Type type READ get_type WRITE set_type RESET reset_type STORED false)
  198 + Q_PROPERTY(int blockSize READ get_blockSize WRITE set_blockSize RESET reset_blockSize STORED false)
  199 + Q_PROPERTY(int C READ get_C WRITE set_C RESET reset_C STORED false)
  200 +
  201 + public:
  202 + enum Method { Mean = ADAPTIVE_THRESH_MEAN_C,
  203 + Gaussian = ADAPTIVE_THRESH_GAUSSIAN_C };
  204 +
  205 + enum Type { Binary = THRESH_BINARY,
  206 + Binary_Inv = THRESH_BINARY_INV };
  207 +
  208 + BR_PROPERTY(int, maxValue, 255)
  209 + BR_PROPERTY(Method, method, Mean)
  210 + BR_PROPERTY(Type, type, Binary)
  211 + BR_PROPERTY(int, blockSize, 3)
  212 + BR_PROPERTY(int, C, 0)
  213 +
  214 + void project(const Template &src, Template &dst) const
  215 + {
  216 + dst = src;
  217 +
  218 + Mat mask;
  219 + adaptiveThreshold(src, mask, maxValue, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, blockSize, C);
  220 +
  221 + dst.file.set("Mask",QVariant::fromValue(mask));
  222 + }
  223 +};
  224 +BR_REGISTER(Transform, AdaptiveThresholdTransform)
  225 +
  226 +/*!
  227 + * \ingroup transforms
  228 + * \brief Samples pixels from a mask.
  229 + * \author Scott Klum \cite sklum
  230 + */
  231 +class SampleFromMaskTransform : public UntrainableTransform
  232 +{
  233 + Q_OBJECT
  234 +
  235 + void project(const Template &src, Template &dst) const
  236 + {
  237 + Mat mask = src.file.get<Mat>("Mask");
  238 + const int count = countNonZero(mask);
  239 + dst.m() = Mat(1,count,src.m().type());
  240 +
  241 + Mat masked;
  242 + src.m().copyTo(masked, mask);
  243 +
  244 + Mat indices;
  245 + findNonZero(masked,indices);
  246 +
  247 + for (int j=0; j<indices.total(); j++)
  248 + dst.m().at<uchar>(0,j) = masked.at<uchar>(indices.at<Point>(j).y,indices.at<Point>(j).x);
  249 + }
  250 +};
  251 +
  252 +BR_REGISTER(Transform, SampleFromMaskTransform)
  253 +
  254 +/*!
  255 + * \ingroup transforms
186 * \brief Applies a mask from the metadata. 256 * \brief Applies a mask from the metadata.
187 * \author Austin Blanton \cite imaus10 257 * \author Austin Blanton \cite imaus10
188 */ 258 */
@@ -190,10 +260,13 @@ class ApplyMaskTransform : public UntrainableTransform @@ -190,10 +260,13 @@ class ApplyMaskTransform : public UntrainableTransform
190 { 260 {
191 Q_OBJECT 261 Q_OBJECT
192 262
  263 + Q_PROPERTY(QString key READ get_key WRITE set_key RESET reset_key STORED false)
  264 + BR_PROPERTY(QString, key, "Mask")
  265 +
193 void project(const Template &src, Template &dst) const 266 void project(const Template &src, Template &dst) const
194 { 267 {
195 - if (src.file.contains("Mask"))  
196 - src.m().copyTo(dst, src.file.get<Mat>("Mask")); 268 + if (src.file.contains(key))
  269 + src.m().copyTo(dst, src.file.get<Mat>(key));
197 else 270 else
198 dst = src; 271 dst = src;
199 } 272 }