tesseract  4.00.00dev
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 ()
 
 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 66 of file bbgrid.cpp.

66  {
67  grid_ = NULL;
68 }

◆ IntGrid() [2/2]

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

Definition at line 70 of file bbgrid.cpp.

71  : grid_(NULL) {
72  Init(gridsize, bleft, tright);
73 }
int gridsize() const
Definition: bbgrid.h:64
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:82

◆ ~IntGrid()

tesseract::IntGrid::~IntGrid ( )
virtual

Definition at line 75 of file bbgrid.cpp.

75  {
76  if (grid_ != NULL)
77  delete [] grid_;
78 }

Member Function Documentation

◆ AnyZeroInRect()

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

Definition at line 178 of file bbgrid.cpp.

178  {
179  int min_x, min_y, max_x, max_y;
180  GridCoords(rect.left(), rect.bottom(), &min_x, &min_y);
181  GridCoords(rect.right(), rect.top(), &max_x, &max_y);
182  for (int y = min_y; y <= max_y; ++y) {
183  for (int x = min_x; x <= max_x; ++x) {
184  if (GridCellValue(x, y) == 0)
185  return true;
186  }
187  }
188  return false;
189 }
inT16 left() const
Definition: rect.h:68
inT16 top() const
Definition: rect.h:54
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
inT16 right() const
Definition: rect.h:75
inT16 bottom() const
Definition: rect.h:61
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:54

◆ Clear()

void tesseract::IntGrid::Clear ( )

Definition at line 91 of file bbgrid.cpp.

91  {
92  for (int i = 0; i < gridbuckets_; ++i) {
93  grid_[i] = 0;
94  }
95 }

◆ 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:61

◆ Init()

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

Definition at line 82 of file bbgrid.cpp.

82  {
83  GridBase::Init(gridsize, bleft, tright);
84  if (grid_ != NULL)
85  delete [] grid_;
86  grid_ = new int[gridbuckets_];
87  Clear();
88 }
int gridsize() const
Definition: bbgrid.h:64
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:42

◆ NeighbourhoodSum()

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

Definition at line 136 of file bbgrid.cpp.

136  {
137  IntGrid* sumgrid = new IntGrid(gridsize(), bleft(), tright());
138  for (int y = 0; y < gridheight(); ++y) {
139  for (int x = 0; x < gridwidth(); ++x) {
140  int cell_count = 0;
141  for (int yoffset = -1; yoffset <= 1; ++yoffset) {
142  for (int xoffset = -1; xoffset <= 1; ++xoffset) {
143  int grid_x = x + xoffset;
144  int grid_y = y + yoffset;
145  ClipGridCoords(&grid_x, &grid_y);
146  cell_count += GridCellValue(grid_x, grid_y);
147  }
148  }
149  if (GridCellValue(x, y) > 1)
150  sumgrid->SetGridCell(x, y, cell_count);
151  }
152  }
153  return sumgrid;
154 }
const ICOORD & bleft() const
Definition: bbgrid.h:73
int gridsize() const
Definition: bbgrid.h:64
int gridheight() const
Definition: bbgrid.h:70
int gridwidth() const
Definition: bbgrid.h:67
const ICOORD & tright() const
Definition: bbgrid.h:76
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
void ClipGridCoords(int *x, int *y) const
Definition: bbgrid.cpp:61

◆ RectMostlyOverThreshold()

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

Definition at line 158 of file bbgrid.cpp.

158  {
159  int min_x, min_y, max_x, max_y;
160  GridCoords(rect.left(), rect.bottom(), &min_x, &min_y);
161  GridCoords(rect.right(), rect.top(), &max_x, &max_y);
162  int total_area = 0;
163  for (int y = min_y; y <= max_y; ++y) {
164  for (int x = min_x; x <= max_x; ++x) {
165  int value = GridCellValue(x, y);
166  if (value > threshold) {
167  TBOX cell_box(x * gridsize_, y * gridsize_,
168  (x + 1) * gridsize_, (y + 1) * gridsize_);
169  cell_box &= rect; // This is in-place box intersection.
170  total_area += cell_box.area();
171  }
172  }
173  }
174  return total_area * 2 > rect.area();
175 }
inT32 area() const
Definition: rect.h:118
inT16 left() const
Definition: rect.h:68
inT16 top() const
Definition: rect.h:54
Definition: rect.h:30
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121
inT16 right() const
Definition: rect.h:75
inT16 bottom() const
Definition: rect.h:61
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:54

◆ Rotate()

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

Definition at line 103 of file bbgrid.cpp.

103  {
104  ASSERT_HOST(rotation.x() == 0.0f || rotation.y() == 0.0f);
105  ICOORD old_bleft(bleft());
106  ICOORD old_tright(tright());
107  int old_width = gridwidth();
108  int old_height = gridheight();
109  TBOX box(bleft(), tright());
110  box.rotate(rotation);
111  int* old_grid = grid_;
112  grid_ = NULL;
113  Init(gridsize(), box.botleft(), box.topright());
114  // Iterate over the old grid, copying data to the rotated position in the new.
115  int oldi = 0;
116  FCOORD x_step(rotation);
117  x_step *= gridsize();
118  for (int oldy = 0; oldy < old_height; ++oldy) {
119  FCOORD line_pos(old_bleft.x(), old_bleft.y() + gridsize() * oldy);
120  line_pos.rotate(rotation);
121  for (int oldx = 0; oldx < old_width; ++oldx, line_pos += x_step, ++oldi) {
122  int grid_x, grid_y;
123  GridCoords(static_cast<int>(line_pos.x() + 0.5),
124  static_cast<int>(line_pos.y() + 0.5),
125  &grid_x, &grid_y);
126  grid_[grid_y * gridwidth() + grid_x] = old_grid[oldi];
127  }
128  }
129  delete [] old_grid;
130 }
Definition: points.h:189
void rotate(const FCOORD vec)
Definition: ipoints.h:471
const ICOORD & bleft() const
Definition: bbgrid.h:73
int gridsize() const
Definition: bbgrid.h:64
int gridheight() const
Definition: bbgrid.h:70
int gridwidth() const
Definition: bbgrid.h:67
#define ASSERT_HOST(x)
Definition: errcode.h:84
const ICOORD & tright() const
Definition: bbgrid.h:76
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:82
Definition: rect.h:30
float y() const
Definition: points.h:212
float x() const
Definition: points.h:209
integer coordinate
Definition: points.h:30
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:54

◆ 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 gridheight() const
Definition: bbgrid.h:70
int gridwidth() const
Definition: bbgrid.h:67
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ ThresholdToPix()

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

Definition at line 194 of file bbgrid.cpp.

194  {
195  Pix* pix = pixCreate(tright().x() - bleft().x(),
196  tright().y() - bleft().y(), 1);
197  int cellsize = gridsize();
198  for (int y = 0; y < gridheight(); ++y) {
199  for (int x = 0; x < gridwidth(); ++x) {
200  if (GridCellValue(x, y) > threshold &&
201  GridCellValue(x - 1, y) > 0 && GridCellValue(x + 1, y) > 0 &&
202  GridCellValue(x, y - 1) > 0 && GridCellValue(x, y + 1) > 0) {
203  pixRasterop(pix, x * cellsize, tright().y() - ((y + 1) * cellsize),
204  cellsize, cellsize, PIX_SET, NULL, 0, 0);
205  }
206  }
207  }
208  return pix;
209 }
const ICOORD & bleft() const
Definition: bbgrid.h:73
int gridsize() const
Definition: bbgrid.h:64
int gridheight() const
Definition: bbgrid.h:70
int gridwidth() const
Definition: bbgrid.h:67
const ICOORD & tright() const
Definition: bbgrid.h:76
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:121

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