Commit 603f5cc1ed064ef4c8cd3534c1cd82acd73e03e9

Authored by Scott Klum
1 parent 6f2dd0bf

Refactored cropfrommask

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