tesseract  5.0.0-alpha-619-ge9db
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)
 
 ~IntGrid () override
 
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 97 of file bbgrid.h.

Constructor & Destructor Documentation

◆ IntGrid() [1/2]

tesseract::IntGrid::IntGrid ( )

Definition at line 64 of file bbgrid.cpp.

64  {
65  grid_ = nullptr;
66 }

◆ IntGrid() [2/2]

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

Definition at line 68 of file bbgrid.cpp.

69  : grid_(nullptr) {
71 }

◆ ~IntGrid()

tesseract::IntGrid::~IntGrid ( )
override

Definition at line 73 of file bbgrid.cpp.

73  {
74  delete [] grid_;
75 }

Member Function Documentation

◆ AnyZeroInRect()

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

Definition at line 174 of file bbgrid.cpp.

174  {
175  int min_x, min_y, max_x, max_y;
176  GridCoords(rect.left(), rect.bottom(), &min_x, &min_y);
177  GridCoords(rect.right(), rect.top(), &max_x, &max_y);
178  for (int y = min_y; y <= max_y; ++y) {
179  for (int x = min_x; x <= max_x; ++x) {
180  if (GridCellValue(x, y) == 0)
181  return true;
182  }
183  }
184  return false;
185 }

◆ Clear()

void tesseract::IntGrid::Clear ( )

Definition at line 87 of file bbgrid.cpp.

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

◆ GridCellValue()

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

Definition at line 120 of file bbgrid.h.

120  {
121  ClipGridCoords(&grid_x, &grid_y);
122  return grid_[grid_y * gridwidth_ + grid_x];
123  }

◆ Init()

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

Definition at line 79 of file bbgrid.cpp.

79  {
81  delete [] grid_;
82  grid_ = new int[gridbuckets_];
83  Clear();
84 }

◆ NeighbourhoodSum()

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

Definition at line 132 of file bbgrid.cpp.

132  {
133  auto* sumgrid = new IntGrid(gridsize(), bleft(), tright());
134  for (int y = 0; y < gridheight(); ++y) {
135  for (int x = 0; x < gridwidth(); ++x) {
136  int cell_count = 0;
137  for (int yoffset = -1; yoffset <= 1; ++yoffset) {
138  for (int xoffset = -1; xoffset <= 1; ++xoffset) {
139  int grid_x = x + xoffset;
140  int grid_y = y + yoffset;
141  ClipGridCoords(&grid_x, &grid_y);
142  cell_count += GridCellValue(grid_x, grid_y);
143  }
144  }
145  if (GridCellValue(x, y) > 1)
146  sumgrid->SetGridCell(x, y, cell_count);
147  }
148  }
149  return sumgrid;
150 }

◆ RectMostlyOverThreshold()

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

Definition at line 154 of file bbgrid.cpp.

154  {
155  int min_x, min_y, max_x, max_y;
156  GridCoords(rect.left(), rect.bottom(), &min_x, &min_y);
157  GridCoords(rect.right(), rect.top(), &max_x, &max_y);
158  int total_area = 0;
159  for (int y = min_y; y <= max_y; ++y) {
160  for (int x = min_x; x <= max_x; ++x) {
161  int value = GridCellValue(x, y);
162  if (value > threshold) {
163  TBOX cell_box(x * gridsize_, y * gridsize_,
164  (x + 1) * gridsize_, (y + 1) * gridsize_);
165  cell_box &= rect; // This is in-place box intersection.
166  total_area += cell_box.area();
167  }
168  }
169  }
170  return total_area * 2 > rect.area();
171 }

◆ Rotate()

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

Definition at line 99 of file bbgrid.cpp.

99  {
100  ASSERT_HOST(rotation.x() == 0.0f || rotation.y() == 0.0f);
101  ICOORD old_bleft(bleft());
102  //ICOORD old_tright(tright());
103  int old_width = gridwidth();
104  int old_height = gridheight();
105  TBOX box(bleft(), tright());
106  box.rotate(rotation);
107  int* old_grid = grid_;
108  grid_ = nullptr;
109  Init(gridsize(), box.botleft(), box.topright());
110  // Iterate over the old grid, copying data to the rotated position in the new.
111  int oldi = 0;
112  FCOORD x_step(rotation);
113  x_step *= gridsize();
114  for (int oldy = 0; oldy < old_height; ++oldy) {
115  FCOORD line_pos(old_bleft.x(), old_bleft.y() + gridsize() * oldy);
116  line_pos.rotate(rotation);
117  for (int oldx = 0; oldx < old_width; ++oldx, line_pos += x_step, ++oldi) {
118  int grid_x, grid_y;
119  GridCoords(static_cast<int>(line_pos.x() + 0.5),
120  static_cast<int>(line_pos.y() + 0.5),
121  &grid_x, &grid_y);
122  grid_[grid_y * gridwidth() + grid_x] = old_grid[oldi];
123  }
124  }
125  delete [] old_grid;
126 }

◆ SetGridCell()

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

Definition at line 124 of file bbgrid.h.

124  {
125  ASSERT_HOST(grid_x >= 0 && grid_x < gridwidth());
126  ASSERT_HOST(grid_y >= 0 && grid_y < gridheight());
127  grid_[grid_y * gridwidth_ + grid_x] = value;
128  }

◆ ThresholdToPix()

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

Definition at line 190 of file bbgrid.cpp.

190  {
191  Pix* pix = pixCreate(tright().x() - bleft().x(),
192  tright().y() - bleft().y(), 1);
193  int cellsize = gridsize();
194  for (int y = 0; y < gridheight(); ++y) {
195  for (int x = 0; x < gridwidth(); ++x) {
196  if (GridCellValue(x, y) > threshold &&
197  GridCellValue(x - 1, y) > 0 && GridCellValue(x + 1, y) > 0 &&
198  GridCellValue(x, y - 1) > 0 && GridCellValue(x, y + 1) > 0) {
199  pixRasterop(pix, x * cellsize, tright().y() - ((y + 1) * cellsize),
200  cellsize, cellsize, PIX_SET, nullptr, 0, 0);
201  }
202  }
203  }
204  return pix;
205 }

The documentation for this class was generated from the following files:
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
FCOORD::y
float y() const
Definition: points.h:209
ICOORD
integer coordinate
Definition: points.h:30
tesseract::GridBase::gridwidth
int gridwidth() const
Definition: bbgrid.h:66
FCOORD::x
float x() const
Definition: points.h:206
tesseract::IntGrid::Clear
void Clear()
Definition: bbgrid.cpp:87
TBOX::top
int16_t top() const
Definition: rect.h:57
TBOX::area
int32_t area() const
Definition: rect.h:121
FCOORD
Definition: points.h:187
tesseract::GridBase::Init
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:40
tesseract::GridBase::tright
const ICOORD & tright() const
Definition: bbgrid.h:75
tesseract::GridBase::gridwidth_
int gridwidth_
Definition: bbgrid.h:87
TBOX::bottom
int16_t bottom() const
Definition: rect.h:64
FCOORD::rotate
void rotate(const FCOORD vec)
Definition: points.h:736
tesseract::IntGrid::GridCellValue
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:120
tesseract::IntGrid::Init
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:79
tesseract::IntGrid::IntGrid
IntGrid()
Definition: bbgrid.cpp:64
tesseract::GridBase::gridsize
int gridsize() const
Definition: bbgrid.h:63
tesseract::GridBase::gridsize_
int gridsize_
Definition: bbgrid.h:86
tesseract::GridBase::gridbuckets_
int gridbuckets_
Definition: bbgrid.h:89
tesseract::GridBase::ClipGridCoords
void ClipGridCoords(int *x, int *y) const
Definition: bbgrid.cpp:59
TBOX::left
int16_t left() const
Definition: rect.h:71
TBOX::right
int16_t right() const
Definition: rect.h:78
tesseract::GridBase::gridheight
int gridheight() const
Definition: bbgrid.h:69
tesseract::GridBase::bleft
const ICOORD & bleft() const
Definition: bbgrid.h:72
tesseract::GridBase::GridCoords
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:52
TBOX
Definition: rect.h:33