Commit 185908453c6b53219ba7e39a0d06610637c7db1d

Authored by Brendan K
2 parents 1f448ca7 f25266ec

Merge pull request #355 from biometrics/cropfrommask_tweaks

CropFromMask Tweaks
openbr/plugins/imgproc/cropfrommask.cpp
1 #include <openbr/plugins/openbr_internal.h> 1 #include <openbr/plugins/openbr_internal.h>
  2 +#include <openbr/core/common.h>
2 3
3 using namespace cv; 4 using namespace cv;
4 5
@@ -24,46 +25,45 @@ private: @@ -24,46 +25,45 @@ private:
24 25
25 Mat mask = dst.file.get<Mat>("Mask"); 26 Mat mask = dst.file.get<Mat>("Mask");
26 27
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 - }  
48 - } 28 + Mat indices;
  29 + findNonZero(mask,indices);
  30 +
  31 + if (indices.total() > 0) {
  32 + QList<int> x, y;
  33 + for (size_t i=0; i<indices.total(); i++) {
  34 + x.append(indices.at<Point>(i).x);
  35 + y.append(indices.at<Point>(i).y);
49 } 36 }
50 37
  38 + int t, b, l, r;
  39 + Common::MinMax(x,&l,&r);
  40 + Common::MinMax(y,&t,&b);
  41 +
51 if (fixedAspectRatio) { 42 if (fixedAspectRatio) {
52 - h = bottom - top + 1;  
53 - w = right - left + 1; 43 + int h, w;
  44 +
  45 + h = b - t + 1;
  46 + w = r - l + 1;
  47 +
54 if (h > w) { 48 if (h > w) {
55 int h2 = (h - w) / 2; 49 int h2 = (h - w) / 2;
56 - right += h2;  
57 - left -= h2; 50 + r += h2;
  51 + l -= h2;
58 } else { 52 } else {
59 int w2 = (w - h) / 2; 53 int w2 = (w - h) / 2;
60 - bottom += w2;  
61 - top -= w2; 54 + b += w2;
  55 + t -= w2;
62 } 56 }
63 } 57 }
64 58
65 - dst.m() = Mat(src.m(), Rect(top, left, bottom - top + 1, right - left + 1)); 59 + t = max(t,0);
  60 + b = min(b, src.m().rows-1);
  61 + l = max(l,0);
  62 + r = min(r, src.m().cols-1);
  63 +
  64 + dst.m() = Mat(src.m(), Rect(l, t, r - l + 1, b - t + 1));
66 } else { 65 } else {
  66 + // Avoid serializing mask
67 dst.file.remove("Mask"); 67 dst.file.remove("Mask");
68 dst.file.fte = true; 68 dst.file.fte = true;
69 } 69 }