Commit 7b0e83a18b1aa56634314188284b57c2600a4ee5
1 parent
13f4699b
Refactored SampleFromMask a bit
Showing
1 changed file
with
21 additions
and
6 deletions
openbr/plugins/imgproc/samplefrommask.cpp
| @@ -14,22 +14,37 @@ class SampleFromMaskTransform : public UntrainableTransform | @@ -14,22 +14,37 @@ class SampleFromMaskTransform : public UntrainableTransform | ||
| 14 | { | 14 | { |
| 15 | Q_OBJECT | 15 | Q_OBJECT |
| 16 | 16 | ||
| 17 | + Q_PROPERTY(int minIndices READ get_minIndices WRITE set_minIndices RESET reset_minIndices STORED false) | ||
| 18 | + BR_PROPERTY(int, minIndices, 0) | ||
| 19 | + | ||
| 17 | void project(const Template &src, Template &dst) const | 20 | void project(const Template &src, Template &dst) const |
| 18 | { | 21 | { |
| 22 | + dst = src; | ||
| 23 | + | ||
| 19 | Mat mask = src.file.get<Mat>("Mask"); | 24 | Mat mask = src.file.get<Mat>("Mask"); |
| 20 | Mat indices; | 25 | Mat indices; |
| 21 | findNonZero(mask,indices); | 26 | findNonZero(mask,indices); |
| 22 | 27 | ||
| 23 | - if (indices.total() > 0) { | ||
| 24 | - dst.m() = Mat(1,indices.total(),src.m().type()); | ||
| 25 | - | 28 | + if (indices.total() > (size_t)minIndices) { |
| 26 | Mat masked; | 29 | Mat masked; |
| 27 | src.m().copyTo(masked, mask); | 30 | src.m().copyTo(masked, mask); |
| 28 | - | ||
| 29 | - for (size_t j=0; j<indices.total(); j++) | ||
| 30 | - dst.m().at<uchar>(0,j) = masked.at<uchar>(indices.at<Point>(j).y,indices.at<Point>(j).x); | 31 | + if (src.m().channels() > 1) { |
| 32 | + dst.m() = Mat(3,indices.total(),CV_32FC1); | ||
| 33 | + for (size_t j=0; j<indices.total(); j++) { | ||
| 34 | + Vec3b v = masked.at<Vec3b>(indices.at<Point>(j).y,indices.at<Point>(j).x); | ||
| 35 | + dst.m().at<float>(0,j) = v[0]; | ||
| 36 | + dst.m().at<float>(1,j) = v[1]; | ||
| 37 | + dst.m().at<float>(2,j) = v[2]; | ||
| 38 | + } | ||
| 39 | + } else { | ||
| 40 | + dst.m() = Mat(1,indices.total(),src.m().type()); | ||
| 41 | + | ||
| 42 | + for (size_t j=0; j<indices.total(); j++) | ||
| 43 | + dst.m().at<uchar>(0,j) = masked.at<uchar>(indices.at<Point>(j).y,indices.at<Point>(j).x); | ||
| 44 | + } | ||
| 31 | } else { | 45 | } else { |
| 32 | dst.file.fte = true; | 46 | dst.file.fte = true; |
| 47 | + dst.file.remove("Mask"); | ||
| 33 | qWarning("No mask content for %s.",qPrintable(src.file.baseName())); | 48 | qWarning("No mask content for %s.",qPrintable(src.file.baseName())); |
| 34 | } | 49 | } |
| 35 | } | 50 | } |