Fawkes API  Fawkes Development Version
shrinker.cpp
1 
2 /***************************************************************************
3  * shrinker.cpp - Implementation of Shrinker
4  *
5  * Generated: Wed Aug 31 2005 21:52:28
6  * Copyright 2005 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvclassifiers/shrinker.h>
25 #include <fvutils/color/colorspaces.h>
26 #include <fvutils/base/roi.h>
27 
28 #include <cstddef>
29 
30 namespace firevision {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class Shrinker <fvclassifiers/shrinker.h>
36  * Shrinker class to shrink ROIs.
37  * This shrinker shrinks a given ROI. This is done to cope with several
38  * special problems that arise in different setups. For example if playing
39  * downstairs in the lobby without a carpet we always have a problem with
40  * reflections on the floor.
41  *
42  * This shrinker works like this:
43  * - if ROI is vertically rectangular, we cut off the bottom part
44  * because it is likely to contain reflection
45  * - if ball is not close (roi->width <= 100), we tighten the ROI, such that
46  * it only contains the ball. This helps against reflection.
47  * (If ball is close, this does not work, because it takes away too many edge pixels.)
48  */
49 
50 /** Constructor. */
52 {
53  src = NULL;
54 }
55 
56 
57 /** Destructor. */
59 {
60 }
61 
62 
63 /** Set the filtered buffer.
64  * The buffer is assumed to being YUV422_PLANAR mode and the desired filter
65  * combination has been run.
66  * @param yuv422planar_buffer YUV422 planar buffer
67  */
68 void
69 Shrinker::setFilteredBuffer(unsigned char *yuv422planar_buffer) {
70  src = yuv422planar_buffer;
71 }
72 
73 
74 /** Shrink!
75  * Do the actual shrinking. See above for used method.
76  * @param roi ROI to srhink
77  */
78 void
80 {
81 
82  unsigned int x;
83  unsigned int y;
84 
85  /* if ROI is vertically rectangular, we cut off the bottom part
86  because it is likely to contain reflection */
87  if (roi->height > roi->width) {
88  roi->height = roi->width;
89  }
90 
91  if ( roi->width <= 100 ) {
92  /* Ball is not close. Tighten ROI, such that it only contains the ball.
93  This helps against reflection.
94  (If ball is close, this does not work, because it takes away too many edge pixels.) */
95 
96  unsigned char *bufferTmp = roi->get_roi_buffer_start( src );
97  unsigned char *line_startTmp = bufferTmp;
98  fawkes::upoint_t leftmostPixel = {roi->width, 0};
99  fawkes::upoint_t topmostPixel = {0, roi->height};
100 
101  // find leftmost hint-pixel
102  bool pixelFound = false;
103  for (x = 0; !pixelFound && (x < roi->width / 2); ++x) {
104  for (y = 0; y < roi->height/2; ++y) {
105  if (*bufferTmp > 230) { // if pixel is white or almost white = edge
106  leftmostPixel.x = x;
107  leftmostPixel.y = y;
108  pixelFound = true;
109  }
110  ++bufferTmp;
111  } // inner for
112  line_startTmp += roi->line_step;
113  bufferTmp = line_startTmp;
114  } // outer for
115 
116  bufferTmp = roi->get_roi_buffer_start( src );
117  line_startTmp = bufferTmp;
118 
119  // find topmost hint-pixel
120  pixelFound = false;
121  for (y = 0; !pixelFound && (y < roi->height/2); ++y) {
122  for (x = 0; x < roi->width; ++x) {
123  if (*bufferTmp > 230) {
124  topmostPixel.x = x;
125  topmostPixel.y = y;
126  pixelFound = true;
127  }
128  ++bufferTmp;
129  } // inner for
130  /*
131  if (pixelFound) {
132  // try to improve x-coordinate (too small)
133  unsigned int x2 = topmostPixel.x;
134  for (unsigned int a = topmostPixel.x + 1; a < roi->width; ++a) {
135  if (TEST_IF_IS_A_PIXEL(*bufferTmp)) {
136  x2 = a;
137  }
138  ++bufferTmp;
139  }
140  topmostPixel.x = (topmostPixel.x + x2) / 2;
141  }
142  */
143  line_startTmp += roi->line_step;
144  bufferTmp = line_startTmp;
145  } // outer for
146 
147  bufferTmp = roi->get_roi_buffer_start( src );
148  line_startTmp = bufferTmp;
149 
150  // tighten ROI if it makes sense
151  if ( (leftmostPixel.x >= topmostPixel.x) ||
152  (topmostPixel.y >= leftmostPixel.y) ||
153  (2 * (topmostPixel.x - leftmostPixel.x) >= roi->width) ||
154  (2 * (leftmostPixel.y - topmostPixel.y) >= roi->height) ) {
155  // bad pixels found
156  } else {
157  // tighten ROI
158  // roi->start.x += leftmostPixel.x;
159  // roi->start.y += topmostPixel.y;
160  // roi->height -= topmostPixel.y;
161  //roi->width = 2 * (topmostPixel.x - leftmostPixel.x);
162  roi->height = 2 * (leftmostPixel.y - topmostPixel.y);
163  /*
164  if ( roi->width < roi->height ) {
165  // further shrinking
166  roi->height = roi->width;
167  }
168  */
169  //cout << "Shrinker: Shrank region!" << endl;
170  }
171  } // else do nothing
172 
173 }
174 
175 } // end namespace firevision
unsigned int y
y coordinate
Definition: types.h:36
unsigned int x
x coordinate
Definition: types.h:35
unsigned int width
ROI width.
Definition: roi.h:121
Region of interest.
Definition: roi.h:58
unsigned char * src
Source image buffer.
Definition: shrinker.h:46
virtual void setFilteredBuffer(unsigned char *yuv422planar_buffer)
Set the filtered buffer.
Definition: shrinker.cpp:69
virtual ~Shrinker()
Destructor.
Definition: shrinker.cpp:58
unsigned char * get_roi_buffer_start(unsigned char *buffer) const
Get ROI buffer start.
Definition: roi.cpp:556
Point with cartesian coordinates as unsigned integers.
Definition: types.h:34
Shrinker()
Constructor.
Definition: shrinker.cpp:51
unsigned int height
ROI height.
Definition: roi.h:123
unsigned int line_step
line step
Definition: roi.h:129
virtual void shrink(ROI *roi)
Shrink! Do the actual shrinking.
Definition: shrinker.cpp:79