tesseract  4.0.0-1-g2a2b
bbgrid.cpp
Go to the documentation of this file.
1 // File: bbgrid.cpp
3 // Description: Class to hold BLOBNBOXs in a grid for fast access
4 // to neighbours.
5 // Author: Ray Smith
6 // Created: Wed Jun 06 17:22:01 PDT 2007
7 //
8 // (C) Copyright 2007, Google Inc.
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
20 
21 #include "bbgrid.h"
22 #include "helpers.h"
23 #include "ocrblock.h"
24 
25 namespace tesseract {
26 
28 // BBGrid IMPLEMENTATION.
30 GridBase::GridBase(int gridsize, const ICOORD& bleft, const ICOORD& tright) {
32 }
33 
34 // Destructor.
35 // It is defined here, so the compiler can create a single vtable
36 // instead of weak vtables in every compilation unit.
37 GridBase::~GridBase() = default;
38 
39 // (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
40 // and bleft, tright are the bounding box of everything to go in it.
41 void GridBase::Init(int gridsize, const ICOORD& bleft, const ICOORD& tright) {
43  bleft_ = bleft;
44  tright_ = tright;
45  if (gridsize_ == 0)
46  gridsize_ = 1;
47  gridwidth_ = (tright.x() - bleft.x() + gridsize_ - 1) / gridsize_;
48  gridheight_ = (tright.y() - bleft.y() + gridsize_ - 1) / gridsize_;
50 }
51 
52 // Compute the given grid coordinates from image coords.
53 void GridBase::GridCoords(int x, int y, int* grid_x, int* grid_y) const {
54  *grid_x = (x - bleft_.x()) / gridsize_;
55  *grid_y = (y - bleft_.y()) / gridsize_;
56  ClipGridCoords(grid_x, grid_y);
57 }
58 
59 // Clip the given grid coordinates to fit within the grid.
60 void GridBase::ClipGridCoords(int* x, int* y) const {
61  *x = ClipToRange(*x, 0, gridwidth_ - 1);
62  *y = ClipToRange(*y, 0, gridheight_ - 1);
63 }
64 
66  grid_ = nullptr;
67 }
68 
69 IntGrid::IntGrid(int gridsize, const ICOORD& bleft, const ICOORD& tright)
70  : grid_(nullptr) {
72 }
73 
75  delete [] grid_;
76 }
77 
78 // (Re)Initialize the grid. The gridsize is the size in pixels of each cell,
79 // and bleft, tright are the bounding box of everything to go in it.
80 void IntGrid::Init(int gridsize, const ICOORD& bleft, const ICOORD& tright) {
82  delete [] grid_;
83  grid_ = new int[gridbuckets_];
84  Clear();
85 }
86 
87 // Clear all the ints in the grid to zero.
89  for (int i = 0; i < gridbuckets_; ++i) {
90  grid_[i] = 0;
91  }
92 }
93 
94 // Rotate the grid by rotation, keeping cell contents.
95 // rotation must be a multiple of 90 degrees.
96 // NOTE: due to partial cells, cell coverage in the rotated grid will be
97 // inexact. This is why there is no Rotate for the generic BBGrid.
98 // TODO(rays) investigate fixing this inaccuracy by moving the origin after
99 // rotation.
100 void IntGrid::Rotate(const FCOORD& rotation) {
101  ASSERT_HOST(rotation.x() == 0.0f || rotation.y() == 0.0f);
102  ICOORD old_bleft(bleft());
103  ICOORD old_tright(tright());
104  int old_width = gridwidth();
105  int old_height = gridheight();
106  TBOX box(bleft(), tright());
107  box.rotate(rotation);
108  int* old_grid = grid_;
109  grid_ = nullptr;
110  Init(gridsize(), box.botleft(), box.topright());
111  // Iterate over the old grid, copying data to the rotated position in the new.
112  int oldi = 0;
113  FCOORD x_step(rotation);
114  x_step *= gridsize();
115  for (int oldy = 0; oldy < old_height; ++oldy) {
116  FCOORD line_pos(old_bleft.x(), old_bleft.y() + gridsize() * oldy);
117  line_pos.rotate(rotation);
118  for (int oldx = 0; oldx < old_width; ++oldx, line_pos += x_step, ++oldi) {
119  int grid_x, grid_y;
120  GridCoords(static_cast<int>(line_pos.x() + 0.5),
121  static_cast<int>(line_pos.y() + 0.5),
122  &grid_x, &grid_y);
123  grid_[grid_y * gridwidth() + grid_x] = old_grid[oldi];
124  }
125  }
126  delete [] old_grid;
127 }
128 
129 // Returns a new IntGrid containing values equal to the sum of all the
130 // neighbouring cells. The returned grid must be deleted after use.
131 // For ease of implementation, edge cells are double counted, to make them
132 // have the same range as the non-edge cells.
134  IntGrid* sumgrid = new IntGrid(gridsize(), bleft(), tright());
135  for (int y = 0; y < gridheight(); ++y) {
136  for (int x = 0; x < gridwidth(); ++x) {
137  int cell_count = 0;
138  for (int yoffset = -1; yoffset <= 1; ++yoffset) {
139  for (int xoffset = -1; xoffset <= 1; ++xoffset) {
140  int grid_x = x + xoffset;
141  int grid_y = y + yoffset;
142  ClipGridCoords(&grid_x, &grid_y);
143  cell_count += GridCellValue(grid_x, grid_y);
144  }
145  }
146  if (GridCellValue(x, y) > 1)
147  sumgrid->SetGridCell(x, y, cell_count);
148  }
149  }
150  return sumgrid;
151 }
152 
153 // Returns true if more than half the area of the rect is covered by grid
154 // cells that are over the threshold.
155 bool IntGrid::RectMostlyOverThreshold(const TBOX& rect, int threshold) const {
156  int min_x, min_y, max_x, max_y;
157  GridCoords(rect.left(), rect.bottom(), &min_x, &min_y);
158  GridCoords(rect.right(), rect.top(), &max_x, &max_y);
159  int total_area = 0;
160  for (int y = min_y; y <= max_y; ++y) {
161  for (int x = min_x; x <= max_x; ++x) {
162  int value = GridCellValue(x, y);
163  if (value > threshold) {
164  TBOX cell_box(x * gridsize_, y * gridsize_,
165  (x + 1) * gridsize_, (y + 1) * gridsize_);
166  cell_box &= rect; // This is in-place box intersection.
167  total_area += cell_box.area();
168  }
169  }
170  }
171  return total_area * 2 > rect.area();
172 }
173 
174 // Returns true if any cell value in the given rectangle is zero.
175 bool IntGrid::AnyZeroInRect(const TBOX& rect) const {
176  int min_x, min_y, max_x, max_y;
177  GridCoords(rect.left(), rect.bottom(), &min_x, &min_y);
178  GridCoords(rect.right(), rect.top(), &max_x, &max_y);
179  for (int y = min_y; y <= max_y; ++y) {
180  for (int x = min_x; x <= max_x; ++x) {
181  if (GridCellValue(x, y) == 0)
182  return true;
183  }
184  }
185  return false;
186 }
187 
188 // Returns a full-resolution binary pix in which each cell over the given
189 // threshold is filled as a black square. pixDestroy after use.
190 // Edge cells, which have a zero 4-neighbour, are not marked.
191 Pix* IntGrid::ThresholdToPix(int threshold) const {
192  Pix* pix = pixCreate(tright().x() - bleft().x(),
193  tright().y() - bleft().y(), 1);
194  int cellsize = gridsize();
195  for (int y = 0; y < gridheight(); ++y) {
196  for (int x = 0; x < gridwidth(); ++x) {
197  if (GridCellValue(x, y) > threshold &&
198  GridCellValue(x - 1, y) > 0 && GridCellValue(x + 1, y) > 0 &&
199  GridCellValue(x, y - 1) > 0 && GridCellValue(x, y + 1) > 0) {
200  pixRasterop(pix, x * cellsize, tright().y() - ((y + 1) * cellsize),
201  cellsize, cellsize, PIX_SET, nullptr, 0, 0);
202  }
203  }
204  }
205  return pix;
206 }
207 
208 // Make a Pix of the correct scaled size for the TraceOutline functions.
209 static Pix* GridReducedPix(const TBOX& box, int gridsize,
210  ICOORD bleft, int* left, int* bottom) {
211  // Compute grid bounds of the outline and pad all round by 1.
212  int grid_left = (box.left() - bleft.x()) / gridsize - 1;
213  int grid_bottom = (box.bottom() - bleft.y()) / gridsize - 1;
214  int grid_right = (box.right() - bleft.x()) / gridsize + 1;
215  int grid_top = (box.top() - bleft.y()) / gridsize + 1;
216  *left = grid_left;
217  *bottom = grid_bottom;
218  return pixCreate(grid_right - grid_left + 1,
219  grid_top - grid_bottom + 1,
220  1);
221 }
222 
223 // Helper function to return a scaled Pix with one pixel per grid cell,
224 // set (black) where the given outline enters the corresponding grid cell,
225 // and clear where the outline does not touch the grid cell.
226 // Also returns the grid coords of the bottom-left of the Pix, in *left
227 // and *bottom, which corresponds to (0, 0) on the Pix.
228 // Note that the Pix is used upside-down, with (0, 0) being the bottom-left.
229 Pix* TraceOutlineOnReducedPix(C_OUTLINE* outline, int gridsize,
230  ICOORD bleft, int* left, int* bottom) {
231  const TBOX& box = outline->bounding_box();
232  Pix* pix = GridReducedPix(box, gridsize, bleft, left, bottom);
233  int wpl = pixGetWpl(pix);
234  l_uint32* data = pixGetData(pix);
235  int length = outline->pathlength();
236  ICOORD pos = outline->start_pos();
237  for (int i = 0; i < length; ++i) {
238  int grid_x = (pos.x() - bleft.x()) / gridsize - *left;
239  int grid_y = (pos.y() - bleft.y()) / gridsize - *bottom;
240  SET_DATA_BIT(data + grid_y * wpl, grid_x);
241  pos += outline->step(i);
242  }
243  return pix;
244 }
245 #if 0 // Example code shows how to use TraceOutlineOnReducedPix.
246  C_OUTLINE_IT ol_it(blob->cblob()->out_list());
247  int grid_left, grid_bottom;
248  Pix* pix = TraceOutlineOnReducedPix(ol_it.data(), gridsize_, bleft_,
249  &grid_left, &grid_bottom);
250  grid->InsertPixPtBBox(grid_left, grid_bottom, pix, blob);
251  pixDestroy(&pix);
252 #endif
253 
254 // As TraceOutlineOnReducedPix above, but on a BLOCK instead of a C_OUTLINE.
255 Pix* TraceBlockOnReducedPix(BLOCK* block, int gridsize,
256  ICOORD bleft, int* left, int* bottom) {
257  const TBOX& box = block->pdblk.bounding_box();
258  Pix* pix = GridReducedPix(box, gridsize, bleft, left, bottom);
259  int wpl = pixGetWpl(pix);
260  l_uint32* data = pixGetData(pix);
261  ICOORDELT_IT it(block->pdblk.poly_block()->points());
262  for (it.mark_cycle_pt(); !it.cycled_list();) {
263  ICOORD pos = *it.data();
264  it.forward();
265  ICOORD next_pos = *it.data();
266  ICOORD line_vector = next_pos - pos;
267  int major, minor;
268  ICOORD major_step, minor_step;
269  line_vector.setup_render(&major_step, &minor_step, &major, &minor);
270  int accumulator = major / 2;
271  while (pos != next_pos) {
272  int grid_x = (pos.x() - bleft.x()) / gridsize - *left;
273  int grid_y = (pos.y() - bleft.y()) / gridsize - *bottom;
274  SET_DATA_BIT(data + grid_y * wpl, grid_x);
275  pos += major_step;
276  accumulator += minor;
277  if (accumulator >= major) {
278  accumulator -= major;
279  pos += minor_step;
280  }
281  }
282  }
283  return pix;
284 }
285 
286 } // namespace tesseract.
const ICOORD & topright() const
Definition: rect.h:104
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:80
void rotate(const FCOORD &vec)
Definition: rect.h:197
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
void rotate(const FCOORD vec)
Definition: points.h:764
int16_t y() const
access_function
Definition: points.h:57
Definition: rect.h:34
Pix * TraceOutlineOnReducedPix(C_OUTLINE *outline, int gridsize, ICOORD bleft, int *left, int *bottom)
Definition: bbgrid.cpp:229
const ICOORD & start_pos() const
Definition: coutln.h:148
void Rotate(const FCOORD &rotation)
Definition: bbgrid.cpp:100
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
void setup_render(ICOORD *major_step, ICOORD *minor_step, int *major, int *minor) const
Definition: points.cpp:85
ICOORD tright_
Definition: bbgrid.h:92
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
bool AnyZeroInRect(const TBOX &rect) const
Definition: bbgrid.cpp:175
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
bool RectMostlyOverThreshold(const TBOX &rect, int threshold) const
Definition: bbgrid.cpp:155
POLY_BLOCK * poly_block() const
Definition: pdblock.h:56
int32_t pathlength() const
Definition: coutln.h:135
const TBOX & bounding_box() const
Definition: coutln.h:113
void SetGridCell(int grid_x, int grid_y, int value)
Definition: bbgrid.h:125
Definition: ocrblock.h:30
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:53
int32_t area() const
Definition: rect.h:122
Pix * TraceBlockOnReducedPix(BLOCK *block, int gridsize, ICOORD bleft, int *left, int *bottom)
Definition: bbgrid.cpp:255
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:41
const ICOORD & botleft() const
Definition: rect.h:92
IntGrid * NeighbourhoodSum() const
Definition: bbgrid.cpp:133
void bounding_box(ICOORD &bottom_left, ICOORD &top_right) const
get box
Definition: pdblock.h:60
Definition: points.h:189
int16_t right() const
Definition: rect.h:79
float x() const
Definition: points.h:208
ICOORDELT_LIST * points()
Definition: polyblk.h:39
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111
int gridwidth() const
Definition: bbgrid.h:67
void ClipGridCoords(int *x, int *y) const
Definition: bbgrid.cpp:60
int16_t bottom() const
Definition: rect.h:65
PDBLK pdblk
Definition: ocrblock.h:192
ICOORD step(int index) const
Definition: coutln.h:144
int gridheight() const
Definition: bbgrid.h:70
Pix * ThresholdToPix(int threshold) const
Definition: bbgrid.cpp:191
const ICOORD & tright() const
Definition: bbgrid.h:76
float y() const
Definition: points.h:211
virtual ~IntGrid()
Definition: bbgrid.cpp:74
#define ASSERT_HOST(x)
Definition: errcode.h:84