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