All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::ParagraphModelSmearer Class Reference

#include <paragraphs_internal.h>

Public Member Functions

 ParagraphModelSmearer (GenericVector< RowScratchRegisters > *rows, int row_start, int row_end, ParagraphTheory *theory)
 
void Smear ()
 

Detailed Description

Definition at line 234 of file paragraphs_internal.h.

Constructor & Destructor Documentation

tesseract::ParagraphModelSmearer::ParagraphModelSmearer ( GenericVector< RowScratchRegisters > *  rows,
int  row_start,
int  row_end,
ParagraphTheory theory 
)

Definition at line 1309 of file paragraphs.cpp.

1312  : theory_(theory), rows_(rows), row_start_(row_start),
1313  row_end_(row_end) {
1314  if (!AcceptableRowArgs(0, 0, __func__, rows, row_start, row_end)) {
1315  row_start_ = 0;
1316  row_end_ = 0;
1317  return;
1318  }
1319  SetOfModels no_models;
1320  for (int row = row_start - 1; row <= row_end; row++) {
1321  open_models_.push_back(no_models);
1322  }
1323 }
GenericVectorEqEq< const ParagraphModel * > SetOfModels

Member Function Documentation

void tesseract::ParagraphModelSmearer::Smear ( )

Definition at line 1356 of file paragraphs.cpp.

1356  {
1357  CalculateOpenModels(row_start_, row_end_);
1358 
1359  // For each row which we're unsure about (that is, it is LT_UNKNOWN or
1360  // we have multiple LT_START hypotheses), see if there's a model that
1361  // was recently used (an "open" model) which might model it well.
1362  for (int i = row_start_; i < row_end_; i++) {
1363  RowScratchRegisters &row = (*rows_)[i];
1364  if (row.ri_->num_words == 0)
1365  continue;
1366 
1367  // Step One:
1368  // Figure out if there are "open" models which are left-alined or
1369  // right-aligned. This is important for determining whether the
1370  // "first" word in a row would fit at the "end" of the previous row.
1371  bool left_align_open = false;
1372  bool right_align_open = false;
1373  for (int m = 0; m < OpenModels(i).size(); m++) {
1374  switch (OpenModels(i)[m]->justification()) {
1375  case JUSTIFICATION_LEFT: left_align_open = true; break;
1376  case JUSTIFICATION_RIGHT: right_align_open = true; break;
1377  default: left_align_open = right_align_open = true;
1378  }
1379  }
1380  // Step Two:
1381  // Use that knowledge to figure out if this row is likely to
1382  // start a paragraph.
1383  bool likely_start;
1384  if (i == 0) {
1385  likely_start = true;
1386  } else {
1387  if ((left_align_open && right_align_open) ||
1388  (!left_align_open && !right_align_open)) {
1389  likely_start = LikelyParagraphStart((*rows_)[i - 1], row,
1390  JUSTIFICATION_LEFT) ||
1391  LikelyParagraphStart((*rows_)[i - 1], row,
1393  } else if (left_align_open) {
1394  likely_start = LikelyParagraphStart((*rows_)[i - 1], row,
1396  } else {
1397  likely_start = LikelyParagraphStart((*rows_)[i - 1], row,
1399  }
1400  }
1401 
1402  // Step Three:
1403  // If this text line seems like an obvious first line of an
1404  // open model, or an obvious continuation of an existing
1405  // modelled paragraph, mark it up.
1406  if (likely_start) {
1407  // Add Start Hypotheses for all Open models that fit.
1408  for (int m = 0; m < OpenModels(i).size(); m++) {
1409  if (ValidFirstLine(rows_, i, OpenModels(i)[m])) {
1410  row.AddStartLine(OpenModels(i)[m]);
1411  }
1412  }
1413  } else {
1414  // Add relevant body line hypotheses.
1415  SetOfModels last_line_models;
1416  if (i > 0) {
1417  (*rows_)[i - 1].StrongHypotheses(&last_line_models);
1418  } else {
1419  theory_->NonCenteredModels(&last_line_models);
1420  }
1421  for (int m = 0; m < last_line_models.size(); m++) {
1422  const ParagraphModel *model = last_line_models[m];
1423  if (ValidBodyLine(rows_, i, model))
1424  row.AddBodyLine(model);
1425  }
1426  }
1427 
1428  // Step Four:
1429  // If we're still quite unsure about this line, go through all
1430  // models in our theory and see if this row could be the start
1431  // of any of our models.
1432  if (row.GetLineType() == LT_UNKNOWN ||
1433  (row.GetLineType() == LT_START && !row.UniqueStartHypothesis())) {
1434  SetOfModels all_models;
1435  theory_->NonCenteredModels(&all_models);
1436  for (int m = 0; m < all_models.size(); m++) {
1437  if (ValidFirstLine(rows_, i, all_models[m])) {
1438  row.AddStartLine(all_models[m]);
1439  }
1440  }
1441  }
1442  // Step Five:
1443  // Since we may have updated the hypotheses about this row, we need
1444  // to recalculate the Open models for the rest of rows[i + 1, row_end)
1445  if (row.GetLineType() != LT_UNKNOWN) {
1446  CalculateOpenModels(i + 1, row_end_);
1447  }
1448  }
1449 }
int size() const
Definition: genericvector.h:72
bool ValidFirstLine(const GenericVector< RowScratchRegisters > *rows, int row, const ParagraphModel *model)
void NonCenteredModels(SetOfModels *models)
GenericVectorEqEq< const ParagraphModel * > SetOfModels
bool ValidBodyLine(const GenericVector< RowScratchRegisters > *rows, int row, const ParagraphModel *model)
bool LikelyParagraphStart(const RowScratchRegisters &before, const RowScratchRegisters &after)

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