Commit 2d62c94dc0fa9cc35629d30c0980bfffa8ed53d2
1 parent
fd9094e2
Added some checks for mask related transforms
Showing
2 changed files
with
50 additions
and
39 deletions
openbr/plugins/imgproc/cropfrommask.cpp
| @@ -24,43 +24,48 @@ private: | @@ -24,43 +24,48 @@ private: | ||
| 24 | 24 | ||
| 25 | Mat mask = dst.file.get<Mat>("Mask"); | 25 | Mat mask = dst.file.get<Mat>("Mask"); |
| 26 | 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; | 27 | + const int count = countNonZero(mask); |
| 28 | + | ||
| 29 | + if (count > 0) { | ||
| 30 | + int w = mask.rows; | ||
| 31 | + int h = mask.cols; | ||
| 32 | + int left = w; | ||
| 33 | + int right = 0; | ||
| 34 | + int top = h; | ||
| 35 | + int bottom = 0; | ||
| 36 | + for (int i = 0 ; i < w; i++) { | ||
| 37 | + for (int j = 0 ; j < h; j++) { | ||
| 38 | + if (mask.at<unsigned char>(i,j)) { | ||
| 39 | + if (i < left) | ||
| 40 | + left = i; | ||
| 41 | + if (i > right) | ||
| 42 | + right = i; | ||
| 43 | + if (j < top) | ||
| 44 | + top = j; | ||
| 45 | + if (j > bottom) | ||
| 46 | + bottom = j; | ||
| 47 | + } | ||
| 44 | } | 48 | } |
| 45 | } | 49 | } |
| 46 | - } | ||
| 47 | 50 | ||
| 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; | 51 | + if (fixedAspectRatio) { |
| 52 | + h = bottom - top + 1; | ||
| 53 | + w = right - left + 1; | ||
| 54 | + if (h > w) { | ||
| 55 | + int h2 = (h - w) / 2; | ||
| 56 | + right += h2; | ||
| 57 | + left -= h2; | ||
| 58 | + } else { | ||
| 59 | + int w2 = (w - h) / 2; | ||
| 60 | + bottom += w2; | ||
| 61 | + top -= w2; | ||
| 62 | + } | ||
| 59 | } | 63 | } |
| 60 | - } | ||
| 61 | - | ||
| 62 | - dst.m() = Mat(src.m(), Rect(top, left, bottom - top + 1, right - left + 1)); | ||
| 63 | 64 | ||
| 65 | + dst.m() = Mat(src.m(), Rect(top, left, bottom - top + 1, right - left + 1)); | ||
| 66 | + } else { | ||
| 67 | + dst.file.fte = true; | ||
| 68 | + } | ||
| 64 | } | 69 | } |
| 65 | }; | 70 | }; |
| 66 | 71 |
openbr/plugins/imgproc/samplefrommask.cpp
| @@ -18,16 +18,22 @@ class SampleFromMaskTransform : public UntrainableTransform | @@ -18,16 +18,22 @@ class SampleFromMaskTransform : public UntrainableTransform | ||
| 18 | { | 18 | { |
| 19 | Mat mask = src.file.get<Mat>("Mask"); | 19 | Mat mask = src.file.get<Mat>("Mask"); |
| 20 | const int count = countNonZero(mask); | 20 | const int count = countNonZero(mask); |
| 21 | - dst.m() = Mat(1,count,src.m().type()); | ||
| 22 | 21 | ||
| 23 | - Mat masked; | ||
| 24 | - src.m().copyTo(masked, mask); | 22 | + if (count > 0) { |
| 23 | + dst.m() = Mat(1,count,src.m().type()); | ||
| 25 | 24 | ||
| 26 | - Mat indices; | ||
| 27 | - findNonZero(masked,indices); | 25 | + Mat masked; |
| 26 | + src.m().copyTo(masked, mask); | ||
| 28 | 27 | ||
| 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); | 28 | + Mat indices; |
| 29 | + findNonZero(masked,indices); | ||
| 30 | + | ||
| 31 | + for (size_t j=0; j<indices.total(); j++) | ||
| 32 | + dst.m().at<uchar>(0,j) = masked.at<uchar>(indices.at<Point>(j).y,indices.at<Point>(j).x); | ||
| 33 | + } else { | ||
| 34 | + dst.file.fte = true; | ||
| 35 | + qWarning("No mask content for %s.",qPrintable(src.file.baseName())); | ||
| 36 | + } | ||
| 31 | } | 37 | } |
| 32 | }; | 38 | }; |
| 33 | 39 |