tesseract v5.3.3.20231005
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
 
Image 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 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.

69 : grid_(nullptr) {
71}
int gridsize() const
Definition: bbgrid.h:63
const ICOORD & bleft() const
Definition: bbgrid.h:72
const ICOORD & tright() const
Definition: bbgrid.h:75
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:79

◆ ~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 173 of file bbgrid.cpp.

173 {
174 int min_x, min_y, max_x, max_y;
175 GridCoords(rect.left(), rect.bottom(), &min_x, &min_y);
176 GridCoords(rect.right(), rect.top(), &max_x, &max_y);
177 for (int y = min_y; y <= max_y; ++y) {
178 for (int x = min_x; x <= max_x; ++x) {
179 if (GridCellValue(x, y) == 0) {
180 return true;
181 }
182 }
183 }
184 return false;
185}
const double y
void GridCoords(int x, int y, int *grid_x, int *grid_y) const
Definition: bbgrid.cpp:53
int GridCellValue(int grid_x, int grid_y) const
Definition: bbgrid.h:120

◆ 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 }
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 79 of file bbgrid.cpp.

79 {
81 delete[] grid_;
82 grid_ = new int[gridbuckets_];
83 Clear();
84}
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.cpp:40

◆ NeighbourhoodSum()

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

Definition at line 131 of file bbgrid.cpp.

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

◆ 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_, (x + 1) * gridsize_, (y + 1) * gridsize_);
164 cell_box &= rect; // This is in-place box intersection.
165 total_area += cell_box.area();
166 }
167 }
168 }
169 return total_area * 2 > rect.area();
170}
@ TBOX
int value

◆ 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), static_cast<int>(line_pos.y() + 0.5),
120 &grid_x, &grid_y);
121 grid_[grid_y * gridwidth() + grid_x] = old_grid[oldi];
122 }
123 }
124 delete[] old_grid;
125}
#define ASSERT_HOST(x)
Definition: errcode.h:54

◆ 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()

Image tesseract::IntGrid::ThresholdToPix ( int  threshold) const

Definition at line 190 of file bbgrid.cpp.

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

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