tesseract  4.0.0-1-g2a2b
tesseract::IntGrid Class Reference

#include <bbgrid.h>

Inheritance diagram for tesseract::IntGrid:
tesseract::GridBase

Public Member Functions

 IntGrid ()
 
 IntGrid (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~IntGrid ()
 
void Init (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
void Clear ()
 
void Rotate (const FCOORD &rotation)
 
IntGridNeighbourhoodSum () const
 
int GridCellValue (int grid_x, int grid_y) const
 
void SetGridCell (int grid_x, int grid_y, int value)
 
bool RectMostlyOverThreshold (const TBOX &rect, int threshold) const
 
bool AnyZeroInRect (const TBOX &rect) const
 
Pix * ThresholdToPix (int threshold) const
 
- Public Member Functions inherited from tesseract::GridBase
 GridBase ()=default
 
 GridBase (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
virtual ~GridBase ()
 
void Init (int gridsize, const ICOORD &bleft, const ICOORD &tright)
 
int gridsize () const
 
int gridwidth () const
 
int gridheight () const
 
const ICOORDbleft () const
 
const ICOORDtright () const
 
void GridCoords (int x, int y, int *grid_x, int *grid_y) const
 
void ClipGridCoords (int *x, int *y) const
 

Additional Inherited Members

- Protected Attributes inherited from tesseract::GridBase
int gridsize_
 
int gridwidth_
 
int gridheight_
 
int gridbuckets_
 
ICOORD bleft_
 
ICOORD tright_
 

Detailed Description

Definition at line 98 of file bbgrid.h.

Constructor & Destructor Documentation

◆ IntGrid() [1/2]

tesseract::IntGrid::IntGrid ( )

Definition at line 65 of file bbgrid.cpp.

65  {
66  grid_ = nullptr;
67 }

◆ IntGrid() [2/2]

tesseract::IntGrid::IntGrid ( int  gridsize,
const ICOORD bleft,
const ICOORD tright 
)

Definition at line 69 of file bbgrid.cpp.

70  : grid_(nullptr) {
72 }
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:80
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
const ICOORD & tright() const
Definition: bbgrid.h:76

◆ ~IntGrid()

tesseract::IntGrid::~IntGrid ( )
virtual

Definition at line 74 of file bbgrid.cpp.

74  {
75  delete [] grid_;
76 }

Member Function Documentation

◆ AnyZeroInRect()

bool tesseract::IntGrid::AnyZeroInRect ( const TBOX rect) const

Definition at line 175 of file bbgrid.cpp.

175  {
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 }
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:53
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65

◆ Clear()

void tesseract::IntGrid::Clear ( )

Definition at line 88 of file bbgrid.cpp.

88  {
89  for (int i = 0; i < gridbuckets_; ++i) {
90  grid_[i] = 0;
91  }
92 }

◆ GridCellValue()

int tesseract::IntGrid::GridCellValue ( int  grid_x,
int  grid_y 
) const
inline

Definition at line 121 of file bbgrid.h.

121  {
122  ClipGridCoords(&grid_x, &grid_y);
123  return grid_[grid_y * gridwidth_ + grid_x];
124  }
void ClipGridCoords(int *x, int *y) const
Definition: bbgrid.cpp:60

◆ Init()

void tesseract::IntGrid::Init ( int  gridsize,
const ICOORD bleft,
const ICOORD tright 
)

Definition at line 80 of file bbgrid.cpp.

80  {
82  delete [] grid_;
83  grid_ = new int[gridbuckets_];
84  Clear();
85 }
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:41
const ICOORD & tright() const
Definition: bbgrid.h:76

◆ NeighbourhoodSum()

IntGrid * tesseract::IntGrid::NeighbourhoodSum ( ) const

Definition at line 133 of file bbgrid.cpp.

133  {
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 }
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
int gridwidth() const
Definition: bbgrid.h:67
void ClipGridCoords(int *x, int *y) const
Definition: bbgrid.cpp:60
int gridheight() const
Definition: bbgrid.h:70
const ICOORD & tright() const
Definition: bbgrid.h:76

◆ RectMostlyOverThreshold()

bool tesseract::IntGrid::RectMostlyOverThreshold ( const TBOX rect,
int  threshold 
) const

Definition at line 155 of file bbgrid.cpp.

155  {
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 }
Definition: rect.h:34
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
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
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65

◆ Rotate()

void tesseract::IntGrid::Rotate ( const FCOORD rotation)

Definition at line 100 of file bbgrid.cpp.

100  {
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 }
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:80
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
void rotate(const FCOORD vec)
Definition: points.h:764
Definition: rect.h:34
integer coordinate
Definition: points.h:32
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:53
Definition: points.h:189
float x() const
Definition: points.h:208
int gridwidth() const
Definition: bbgrid.h:67
int gridheight() const
Definition: bbgrid.h:70
const ICOORD & tright() const
Definition: bbgrid.h:76
float y() const
Definition: points.h:211
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ SetGridCell()

void tesseract::IntGrid::SetGridCell ( int  grid_x,
int  grid_y,
int  value 
)
inline

Definition at line 125 of file bbgrid.h.

125  {
126  ASSERT_HOST(grid_x >= 0 && grid_x < gridwidth());
127  ASSERT_HOST(grid_y >= 0 && grid_y < gridheight());
128  grid_[grid_y * gridwidth_ + grid_x] = value;
129  }
int gridwidth() const
Definition: bbgrid.h:67
int gridheight() const
Definition: bbgrid.h:70
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ ThresholdToPix()

Pix * tesseract::IntGrid::ThresholdToPix ( int  threshold) const

Definition at line 191 of file bbgrid.cpp.

191  {
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 }
int gridsize() const
Definition: bbgrid.h:64
const ICOORD & bleft() const
Definition: bbgrid.h:73
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
int gridwidth() const
Definition: bbgrid.h:67
int gridheight() const
Definition: bbgrid.h:70
const ICOORD & tright() const
Definition: bbgrid.h:76

The documentation for this class was generated from the following files: