tesseract  4.00.00dev
tesseract::ImageFind Class Reference

#include <imagefind.h>

Static Public Member Functions

static Pix * FindImages (Pix *pix, DebugPixa *pixa_debug)
 
static void ConnCompAndRectangularize (Pix *pix, DebugPixa *pixa_debug, Boxa **boxa, Pixa **pixa)
 
static bool pixNearlyRectangular (Pix *pix, double min_fraction, double max_fraction, double max_skew_gradient, int *x_start, int *y_start, int *x_end, int *y_end)
 
static bool BoundsWithinRect (Pix *pix, int *x_start, int *y_start, int *x_end, int *y_end)
 
static double ColorDistanceFromLine (const uinT8 *line1, const uinT8 *line2, const uinT8 *point)
 
static uinT32 ComposeRGB (uinT32 r, uinT32 g, uinT32 b)
 
static uinT8 ClipToByte (double pixel)
 
static void ComputeRectangleColors (const TBOX &rect, Pix *pix, int factor, Pix *color_map1, Pix *color_map2, Pix *rms_map, uinT8 *color1, uinT8 *color2)
 
static bool BlankImageInBetween (const TBOX &box1, const TBOX &box2, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
 
static int CountPixelsInRotatedBox (TBOX box, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
 
static void TransferImagePartsToImageMask (const FCOORD &rerotation, ColPartitionGrid *part_grid, Pix *image_mask)
 
static void FindImagePartitions (Pix *image_pix, const FCOORD &rotation, const FCOORD &rerotation, TO_BLOCK *block, TabFind *tab_grid, DebugPixa *pixa_debug, ColPartitionGrid *part_grid, ColPartition_LIST *big_parts)
 

Detailed Description

Definition at line 43 of file imagefind.h.

Member Function Documentation

◆ BlankImageInBetween()

bool tesseract::ImageFind::BlankImageInBetween ( const TBOX box1,
const TBOX box2,
const TBOX im_box,
const FCOORD rotation,
Pix *  pix 
)
static

Definition at line 570 of file imagefind.cpp.

572  {
573  TBOX search_box(box1);
574  search_box += box2;
575  if (box1.x_gap(box2) >= box1.y_gap(box2)) {
576  if (box1.x_gap(box2) <= 0)
577  return true;
578  search_box.set_left(MIN(box1.right(), box2.right()));
579  search_box.set_right(MAX(box1.left(), box2.left()));
580  } else {
581  if (box1.y_gap(box2) <= 0)
582  return true;
583  search_box.set_top(MAX(box1.bottom(), box2.bottom()));
584  search_box.set_bottom(MIN(box1.top(), box2.top()));
585  }
586  return CountPixelsInRotatedBox(search_box, im_box, rotation, pix) == 0;
587 }
static int CountPixelsInRotatedBox(TBOX box, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
Definition: imagefind.cpp:591
inT16 left() const
Definition: rect.h:68
int y_gap(const TBOX &box) const
Definition: rect.h:225
inT16 top() const
Definition: rect.h:54
#define MAX(x, y)
Definition: ndminx.h:24
Definition: rect.h:30
#define MIN(x, y)
Definition: ndminx.h:28
inT16 right() const
Definition: rect.h:75
void set_left(int x)
Definition: rect.h:71
inT16 bottom() const
Definition: rect.h:61
int x_gap(const TBOX &box) const
Definition: rect.h:217

◆ BoundsWithinRect()

bool tesseract::ImageFind::BoundsWithinRect ( Pix *  pix,
int x_start,
int y_start,
int x_end,
int y_end 
)
static

Definition at line 326 of file imagefind.cpp.

327  {
328  Box* input_box = boxCreate(*x_start, *y_start, *x_end - *x_start,
329  *y_end - *y_start);
330  Box* output_box = NULL;
331  pixClipBoxToForeground(pix, input_box, NULL, &output_box);
332  bool result = output_box != NULL;
333  if (result) {
334  l_int32 x, y, width, height;
335  boxGetGeometry(output_box, &x, &y, &width, &height);
336  *x_start = x;
337  *y_start = y;
338  *x_end = x + width;
339  *y_end = y + height;
340  boxDestroy(&output_box);
341  }
342  boxDestroy(&input_box);
343  return result;
344 }

◆ ClipToByte()

uinT8 tesseract::ImageFind::ClipToByte ( double  pixel)
static

Definition at line 390 of file imagefind.cpp.

390  {
391  if (pixel < 0.0)
392  return 0;
393  else if (pixel >= 255.0)
394  return 255;
395  return static_cast<uinT8>(pixel);
396 }
uint8_t uinT8
Definition: host.h:35

◆ ColorDistanceFromLine()

double tesseract::ImageFind::ColorDistanceFromLine ( const uinT8 line1,
const uinT8 line2,
const uinT8 point 
)
static

Definition at line 349 of file imagefind.cpp.

351  {
352  int line_vector[kRGBRMSColors];
353  int point_vector[kRGBRMSColors];
354  for (int i = 0; i < kRGBRMSColors; ++i) {
355  line_vector[i] = static_cast<int>(line2[i]) - static_cast<int>(line1[i]);
356  point_vector[i] = static_cast<int>(point[i]) - static_cast<int>(line1[i]);
357  }
358  line_vector[L_ALPHA_CHANNEL] = 0;
359  // Now the cross product in 3d.
360  int cross[kRGBRMSColors];
361  cross[COLOR_RED] = line_vector[COLOR_GREEN] * point_vector[COLOR_BLUE]
362  - line_vector[COLOR_BLUE] * point_vector[COLOR_GREEN];
363  cross[COLOR_GREEN] = line_vector[COLOR_BLUE] * point_vector[COLOR_RED]
364  - line_vector[COLOR_RED] * point_vector[COLOR_BLUE];
365  cross[COLOR_BLUE] = line_vector[COLOR_RED] * point_vector[COLOR_GREEN]
366  - line_vector[COLOR_GREEN] * point_vector[COLOR_RED];
367  cross[L_ALPHA_CHANNEL] = 0;
368  // Now the sums of the squares.
369  double cross_sq = 0.0;
370  double line_sq = 0.0;
371  for (int j = 0; j < kRGBRMSColors; ++j) {
372  cross_sq += static_cast<double>(cross[j]) * cross[j];
373  line_sq += static_cast<double>(line_vector[j]) * line_vector[j];
374  }
375  if (line_sq == 0.0) {
376  return 0.0;
377  }
378  return cross_sq / line_sq; // This is the squared distance.
379 }
const int kRGBRMSColors
Definition: colpartition.h:36

◆ ComposeRGB()

uinT32 tesseract::ImageFind::ComposeRGB ( uinT32  r,
uinT32  g,
uinT32  b 
)
static

Definition at line 383 of file imagefind.cpp.

383  {
384  l_uint32 result;
385  composeRGBPixel(r, g, b, &result);
386  return result;
387 }

◆ ComputeRectangleColors()

void tesseract::ImageFind::ComputeRectangleColors ( const TBOX rect,
Pix *  pix,
int  factor,
Pix *  color_map1,
Pix *  color_map2,
Pix *  rms_map,
uinT8 color1,
uinT8 color2 
)
static

Definition at line 408 of file imagefind.cpp.

411  {
412  ASSERT_HOST(pix != NULL && pixGetDepth(pix) == 32);
413  // Pad the rectangle outwards by 2 (scaled) pixels if possible to get more
414  // background.
415  int width = pixGetWidth(pix);
416  int height = pixGetHeight(pix);
417  int left_pad = MAX(rect.left() - 2 * factor, 0) / factor;
418  int top_pad = (rect.top() + 2 * factor + (factor - 1)) / factor;
419  top_pad = MIN(height, top_pad);
420  int right_pad = (rect.right() + 2 * factor + (factor - 1)) / factor;
421  right_pad = MIN(width, right_pad);
422  int bottom_pad = MAX(rect.bottom() - 2 * factor, 0) / factor;
423  int width_pad = right_pad - left_pad;
424  int height_pad = top_pad - bottom_pad;
425  if (width_pad < 1 || height_pad < 1 || width_pad + height_pad < 4)
426  return;
427  // Now crop the pix to the rectangle.
428  Box* scaled_box = boxCreate(left_pad, height - top_pad,
429  width_pad, height_pad);
430  Pix* scaled = pixClipRectangle(pix, scaled_box, NULL);
431 
432  // Compute stats over the whole image.
433  STATS red_stats(0, 256);
434  STATS green_stats(0, 256);
435  STATS blue_stats(0, 256);
436  uinT32* data = pixGetData(scaled);
437  ASSERT_HOST(pixGetWpl(scaled) == width_pad);
438  for (int y = 0; y < height_pad; ++y) {
439  for (int x = 0; x < width_pad; ++x, ++data) {
440  int r = GET_DATA_BYTE(data, COLOR_RED);
441  int g = GET_DATA_BYTE(data, COLOR_GREEN);
442  int b = GET_DATA_BYTE(data, COLOR_BLUE);
443  red_stats.add(r, 1);
444  green_stats.add(g, 1);
445  blue_stats.add(b, 1);
446  }
447  }
448  // Find the RGB component with the greatest 8th-ile-range.
449  // 8th-iles are used instead of quartiles to get closer to the true
450  // foreground color, which is going to be faint at best because of the
451  // pre-scaling of the input image.
452  int best_l8 = static_cast<int>(red_stats.ile(0.125f));
453  int best_u8 = static_cast<int>(ceil(red_stats.ile(0.875f)));
454  int best_i8r = best_u8 - best_l8;
455  int x_color = COLOR_RED;
456  int y1_color = COLOR_GREEN;
457  int y2_color = COLOR_BLUE;
458  int l8 = static_cast<int>(green_stats.ile(0.125f));
459  int u8 = static_cast<int>(ceil(green_stats.ile(0.875f)));
460  if (u8 - l8 > best_i8r) {
461  best_i8r = u8 - l8;
462  best_l8 = l8;
463  best_u8 = u8;
464  x_color = COLOR_GREEN;
465  y1_color = COLOR_RED;
466  }
467  l8 = static_cast<int>(blue_stats.ile(0.125f));
468  u8 = static_cast<int>(ceil(blue_stats.ile(0.875f)));
469  if (u8 - l8 > best_i8r) {
470  best_i8r = u8 - l8;
471  best_l8 = l8;
472  best_u8 = u8;
473  x_color = COLOR_BLUE;
474  y1_color = COLOR_GREEN;
475  y2_color = COLOR_RED;
476  }
477  if (best_i8r >= kMinColorDifference) {
478  LLSQ line1;
479  LLSQ line2;
480  uinT32* data = pixGetData(scaled);
481  for (int im_y = 0; im_y < height_pad; ++im_y) {
482  for (int im_x = 0; im_x < width_pad; ++im_x, ++data) {
483  int x = GET_DATA_BYTE(data, x_color);
484  int y1 = GET_DATA_BYTE(data, y1_color);
485  int y2 = GET_DATA_BYTE(data, y2_color);
486  line1.add(x, y1);
487  line2.add(x, y2);
488  }
489  }
490  double m1 = line1.m();
491  double c1 = line1.c(m1);
492  double m2 = line2.m();
493  double c2 = line2.c(m2);
494  double rms = line1.rms(m1, c1) + line2.rms(m2, c2);
495  rms *= kRMSFitScaling;
496  // Save the results.
497  color1[x_color] = ClipToByte(best_l8);
498  color1[y1_color] = ClipToByte(m1 * best_l8 + c1 + 0.5);
499  color1[y2_color] = ClipToByte(m2 * best_l8 + c2 + 0.5);
500  color1[L_ALPHA_CHANNEL] = ClipToByte(rms);
501  color2[x_color] = ClipToByte(best_u8);
502  color2[y1_color] = ClipToByte(m1 * best_u8 + c1 + 0.5);
503  color2[y2_color] = ClipToByte(m2 * best_u8 + c2 + 0.5);
504  color2[L_ALPHA_CHANNEL] = ClipToByte(rms);
505  } else {
506  // There is only one color.
507  color1[COLOR_RED] = ClipToByte(red_stats.median());
508  color1[COLOR_GREEN] = ClipToByte(green_stats.median());
509  color1[COLOR_BLUE] = ClipToByte(blue_stats.median());
510  color1[L_ALPHA_CHANNEL] = 0;
511  memcpy(color2, color1, 4);
512  }
513  if (color_map1 != NULL) {
514  pixSetInRectArbitrary(color_map1, scaled_box,
515  ComposeRGB(color1[COLOR_RED],
516  color1[COLOR_GREEN],
517  color1[COLOR_BLUE]));
518  pixSetInRectArbitrary(color_map2, scaled_box,
519  ComposeRGB(color2[COLOR_RED],
520  color2[COLOR_GREEN],
521  color2[COLOR_BLUE]));
522  pixSetInRectArbitrary(rms_map, scaled_box, color1[L_ALPHA_CHANNEL]);
523  }
524  pixDestroy(&scaled);
525  boxDestroy(&scaled_box);
526 }
double m() const
Definition: linlsq.cpp:101
void add(double x, double y)
Definition: linlsq.cpp:49
const int kMinColorDifference
Definition: imagefind.cpp:55
const double kRMSFitScaling
Definition: imagefind.cpp:53
#define ASSERT_HOST(x)
Definition: errcode.h:84
inT16 left() const
Definition: rect.h:68
uint32_t uinT32
Definition: host.h:39
static uinT8 ClipToByte(double pixel)
Definition: imagefind.cpp:390
double rms(double m, double c) const
Definition: linlsq.cpp:131
inT16 top() const
Definition: rect.h:54
double c(double m) const
Definition: linlsq.cpp:117
#define MAX(x, y)
Definition: ndminx.h:24
#define MIN(x, y)
Definition: ndminx.h:28
Definition: linlsq.h:26
inT16 right() const
Definition: rect.h:75
Definition: statistc.h:33
inT16 bottom() const
Definition: rect.h:61
static uinT32 ComposeRGB(uinT32 r, uinT32 g, uinT32 b)
Definition: imagefind.cpp:383

◆ ConnCompAndRectangularize()

void tesseract::ImageFind::ConnCompAndRectangularize ( Pix *  pix,
DebugPixa pixa_debug,
Boxa **  boxa,
Pixa **  pixa 
)
static

Definition at line 148 of file imagefind.cpp.

149  {
150  *boxa = NULL;
151  *pixa = NULL;
152 
153  if (textord_tabfind_show_images && pixa_debug != nullptr)
154  pixa_debug->AddPix(pix, "Conncompimage");
155  // Find the individual image regions in the mask image.
156  *boxa = pixConnComp(pix, pixa, 8);
157  // Rectangularize the individual images. If a sharp edge in vertical and/or
158  // horizontal occupancy can be found, it indicates a probably rectangular
159  // image with unwanted bits merged on, so clip to the approximate rectangle.
160  int npixes = 0;
161  if (*boxa != nullptr && *pixa != nullptr) npixes = pixaGetCount(*pixa);
162  for (int i = 0; i < npixes; ++i) {
163  int x_start, x_end, y_start, y_end;
164  Pix* img_pix = pixaGetPix(*pixa, i, L_CLONE);
165  if (textord_tabfind_show_images && pixa_debug != nullptr)
166  pixa_debug->AddPix(img_pix, "A component");
170  &x_start, &y_start, &x_end, &y_end)) {
171  Pix* simple_pix = pixCreate(x_end - x_start, y_end - y_start, 1);
172  pixSetAll(simple_pix);
173  pixDestroy(&img_pix);
174  // pixaReplacePix takes ownership of the simple_pix.
175  pixaReplacePix(*pixa, i, simple_pix, NULL);
176  img_pix = pixaGetPix(*pixa, i, L_CLONE);
177  // Fix the box to match the new pix.
178  l_int32 x, y, width, height;
179  boxaGetBoxGeometry(*boxa, i, &x, &y, &width, &height);
180  Box* simple_box = boxCreate(x + x_start, y + y_start,
181  x_end - x_start, y_end - y_start);
182  boxaReplaceBox(*boxa, i, simple_box);
183  }
184  pixDestroy(&img_pix);
185  }
186 }
int textord_tabfind_show_images
Definition: imagefind.cpp:38
const double kMaxRectangularFraction
Definition: imagefind.cpp:46
const double kMinRectangularFraction
Definition: imagefind.cpp:44
static bool pixNearlyRectangular(Pix *pix, double min_fraction, double max_fraction, double max_skew_gradient, int *x_start, int *y_start, int *x_end, int *y_end)
Definition: imagefind.cpp:260
const double kMaxRectangularGradient
Definition: imagefind.cpp:49

◆ CountPixelsInRotatedBox()

int tesseract::ImageFind::CountPixelsInRotatedBox ( TBOX  box,
const TBOX im_box,
const FCOORD rotation,
Pix *  pix 
)
static

Definition at line 591 of file imagefind.cpp.

592  {
593  // Intersect it with the image box.
594  box &= im_box; // This is in-place box intersection.
595  if (box.null_box())
596  return 0;
597  box.rotate(rotation);
598  TBOX rotated_im_box(im_box);
599  rotated_im_box.rotate(rotation);
600  Pix* rect_pix = pixCreate(box.width(), box.height(), 1);
601  pixRasterop(rect_pix, 0, 0, box.width(), box.height(),
602  PIX_SRC, pix, box.left() - rotated_im_box.left(),
603  rotated_im_box.top() - box.top());
604  l_int32 result;
605  pixCountPixels(rect_pix, &result, NULL);
606  pixDestroy(&rect_pix);
607  return result;
608 }
inT16 left() const
Definition: rect.h:68
bool null_box() const
Definition: rect.h:46
inT16 top() const
Definition: rect.h:54
Definition: rect.h:30
inT16 height() const
Definition: rect.h:104
inT16 width() const
Definition: rect.h:111
void rotate(const FCOORD &vec)
Definition: rect.h:189

◆ FindImagePartitions()

void tesseract::ImageFind::FindImagePartitions ( Pix *  image_pix,
const FCOORD rotation,
const FCOORD rerotation,
TO_BLOCK block,
TabFind tab_grid,
DebugPixa pixa_debug,
ColPartitionGrid part_grid,
ColPartition_LIST *  big_parts 
)
static

Definition at line 1292 of file imagefind.cpp.

1296  {
1297  int imageheight = pixGetHeight(image_pix);
1298  Boxa* boxa;
1299  Pixa* pixa;
1300  ConnCompAndRectangularize(image_pix, pixa_debug, &boxa, &pixa);
1301  // Iterate the connected components in the image regions mask.
1302  int nboxes = 0;
1303  if (boxa != nullptr && pixa != nullptr) nboxes = boxaGetCount(boxa);
1304  for (int i = 0; i < nboxes; ++i) {
1305  l_int32 x, y, width, height;
1306  boxaGetBoxGeometry(boxa, i, &x, &y, &width, &height);
1307  Pix* pix = pixaGetPix(pixa, i, L_CLONE);
1308  TBOX im_box(x, imageheight -y - height, x + width, imageheight - y);
1309  im_box.rotate(rotation); // Now matches all partitions and blobs.
1310  ColPartitionGridSearch rectsearch(part_grid);
1311  rectsearch.SetUniqueMode(true);
1312  ColPartition_LIST part_list;
1313  DivideImageIntoParts(im_box, rotation, rerotation, pix,
1314  &rectsearch, &part_list);
1315  if (textord_tabfind_show_images && pixa_debug != nullptr) {
1316  pixa_debug->AddPix(pix, "ImageComponent");
1317  tprintf("Component has %d parts\n", part_list.length());
1318  }
1319  pixDestroy(&pix);
1320  if (!part_list.empty()) {
1321  ColPartition_IT part_it(&part_list);
1322  if (part_list.singleton()) {
1323  // We didn't have to chop it into a polygon to fit around text, so
1324  // try expanding it to merge fragmented image parts, as long as it
1325  // doesn't touch strong text.
1326  ColPartition* part = part_it.extract();
1327  TBOX text_box(im_box);
1328  MaximalImageBoundingBox(part_grid, &text_box);
1329  while (ExpandImageIntoParts(text_box, &rectsearch, part_grid, &part));
1330  part_it.set_to_list(&part_list);
1331  part_it.add_after_then_move(part);
1332  im_box = part->bounding_box();
1333  }
1334  EliminateWeakParts(im_box, part_grid, big_parts, &part_list);
1335  // Iterate the part_list and put the parts into the grid.
1336  for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
1337  ColPartition* image_part = part_it.extract();
1338  im_box = image_part->bounding_box();
1339  part_grid->InsertBBox(true, true, image_part);
1340  if (!part_it.at_last()) {
1341  ColPartition* neighbour = part_it.data_relative(1);
1342  image_part->AddPartner(false, neighbour);
1343  neighbour->AddPartner(true, image_part);
1344  }
1345  }
1346  }
1347  }
1348  boxaDestroy(&boxa);
1349  pixaDestroy(&pixa);
1350  DeleteSmallImages(part_grid);
1352  ScrollView* images_win_ = part_grid->MakeWindow(1000, 400, "With Images");
1353  part_grid->DisplayBoxes(images_win_);
1354  }
1355 }
int textord_tabfind_show_images
Definition: imagefind.cpp:38
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
#define tprintf(...)
Definition: tprintf.h:31
static void ConnCompAndRectangularize(Pix *pix, DebugPixa *pixa_debug, Boxa **boxa, Pixa **pixa)
Definition: imagefind.cpp:148
Definition: rect.h:30

◆ FindImages()

Pix * tesseract::ImageFind::FindImages ( Pix *  pix,
DebugPixa pixa_debug 
)
static

Definition at line 66 of file imagefind.cpp.

66  {
67  // Not worth looking at small images.
68  if (pixGetWidth(pix) < kMinImageFindSize ||
69  pixGetHeight(pix) < kMinImageFindSize)
70  return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
71 
72  // Reduce by factor 2.
73  Pix *pixr = pixReduceRankBinaryCascade(pix, 1, 0, 0, 0);
74  if (textord_tabfind_show_images && pixa_debug != nullptr)
75  pixa_debug->AddPix(pixr, "CascadeReduced");
76 
77  // Get the halftone mask directly from Leptonica.
78  //
79  // Leptonica will print an error message and return NULL if we call
80  // pixGenHalftoneMask(pixr, NULL, ...) with too small image, so we
81  // want to bypass that.
82  if (pixGetWidth(pixr) < kMinImageFindSize ||
83  pixGetHeight(pixr) < kMinImageFindSize) {
84  pixDestroy(&pixr);
85  return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
86  }
87  l_int32 ht_found = 0;
88  Pix *pixht2 = pixGenHalftoneMask(pixr, NULL, &ht_found,
90  pixDestroy(&pixr);
91  if (!ht_found && pixht2 != NULL)
92  pixDestroy(&pixht2);
93  if (pixht2 == NULL)
94  return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
95 
96  // Expand back up again.
97  Pix *pixht = pixExpandReplicate(pixht2, 2);
98  if (textord_tabfind_show_images && pixa_debug != nullptr)
99  pixa_debug->AddPix(pixht, "HalftoneReplicated");
100  pixDestroy(&pixht2);
101 
102  // Fill to capture pixels near the mask edges that were missed
103  Pix *pixt = pixSeedfillBinary(NULL, pixht, pix, 8);
104  pixOr(pixht, pixht, pixt);
105  pixDestroy(&pixt);
106 
107  // Eliminate lines and bars that may be joined to images.
108  Pix* pixfinemask = pixReduceRankBinaryCascade(pixht, 1, 1, 3, 3);
109  pixDilateBrick(pixfinemask, pixfinemask, 5, 5);
110  if (textord_tabfind_show_images && pixa_debug != nullptr)
111  pixa_debug->AddPix(pixfinemask, "FineMask");
112  Pix* pixreduced = pixReduceRankBinaryCascade(pixht, 1, 1, 1, 1);
113  Pix* pixreduced2 = pixReduceRankBinaryCascade(pixreduced, 3, 3, 3, 0);
114  pixDestroy(&pixreduced);
115  pixDilateBrick(pixreduced2, pixreduced2, 5, 5);
116  Pix* pixcoarsemask = pixExpandReplicate(pixreduced2, 8);
117  pixDestroy(&pixreduced2);
118  if (textord_tabfind_show_images && pixa_debug != nullptr)
119  pixa_debug->AddPix(pixcoarsemask, "CoarseMask");
120  // Combine the coarse and fine image masks.
121  pixAnd(pixcoarsemask, pixcoarsemask, pixfinemask);
122  pixDestroy(&pixfinemask);
123  // Dilate a bit to make sure we get everything.
124  pixDilateBrick(pixcoarsemask, pixcoarsemask, 3, 3);
125  Pix* pixmask = pixExpandReplicate(pixcoarsemask, 16);
126  pixDestroy(&pixcoarsemask);
127  if (textord_tabfind_show_images && pixa_debug != nullptr)
128  pixa_debug->AddPix(pixmask, "MaskDilated");
129  // And the image mask with the line and bar remover.
130  pixAnd(pixht, pixht, pixmask);
131  pixDestroy(&pixmask);
132  if (textord_tabfind_show_images && pixa_debug != nullptr)
133  pixa_debug->AddPix(pixht, "FinalMask");
134  // Make the result image the same size as the input.
135  Pix* result = pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
136  pixOr(result, result, pixht);
137  pixDestroy(&pixht);
138  return result;
139 }
int textord_tabfind_show_images
Definition: imagefind.cpp:38
const int kMinImageFindSize
Definition: imagefind.cpp:51

◆ pixNearlyRectangular()

bool tesseract::ImageFind::pixNearlyRectangular ( Pix *  pix,
double  min_fraction,
double  max_fraction,
double  max_skew_gradient,
int x_start,
int y_start,
int x_end,
int y_end 
)
static

Definition at line 260 of file imagefind.cpp.

264  {
265  ASSERT_HOST(pix != NULL);
266  *x_start = 0;
267  *x_end = pixGetWidth(pix);
268  *y_start = 0;
269  *y_end = pixGetHeight(pix);
270 
271  uinT32* data = pixGetData(pix);
272  int wpl = pixGetWpl(pix);
273  bool any_cut = false;
274  bool left_done = false;
275  bool right_done = false;
276  bool top_done = false;
277  bool bottom_done = false;
278  do {
279  any_cut = false;
280  // Find the top/bottom edges.
281  int width = *x_end - *x_start;
282  int min_count = static_cast<int>(width * min_fraction);
283  int max_count = static_cast<int>(width * max_fraction);
284  int edge_width = static_cast<int>(width * max_skew_gradient);
285  if (HScanForEdge(data, wpl, *x_start, *x_end, min_count, edge_width,
286  max_count, *y_end, 1, y_start) && !top_done) {
287  top_done = true;
288  any_cut = true;
289  }
290  --(*y_end);
291  if (HScanForEdge(data, wpl, *x_start, *x_end, min_count, edge_width,
292  max_count, *y_start, -1, y_end) && !bottom_done) {
293  bottom_done = true;
294  any_cut = true;
295  }
296  ++(*y_end);
297 
298  // Find the left/right edges.
299  int height = *y_end - *y_start;
300  min_count = static_cast<int>(height * min_fraction);
301  max_count = static_cast<int>(height * max_fraction);
302  edge_width = static_cast<int>(height * max_skew_gradient);
303  if (VScanForEdge(data, wpl, *y_start, *y_end, min_count, edge_width,
304  max_count, *x_end, 1, x_start) && !left_done) {
305  left_done = true;
306  any_cut = true;
307  }
308  --(*x_end);
309  if (VScanForEdge(data, wpl, *y_start, *y_end, min_count, edge_width,
310  max_count, *x_start, -1, x_end) && !right_done) {
311  right_done = true;
312  any_cut = true;
313  }
314  ++(*x_end);
315  } while (any_cut);
316 
317  // All edges must satisfy the condition of sharp gradient in pixel density
318  // in order for the full rectangle to be present.
319  return left_done && right_done && top_done && bottom_done;
320 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
uint32_t uinT32
Definition: host.h:39

◆ TransferImagePartsToImageMask()

void tesseract::ImageFind::TransferImagePartsToImageMask ( const FCOORD rerotation,
ColPartitionGrid part_grid,
Pix *  image_mask 
)
static

Definition at line 1239 of file imagefind.cpp.

1241  {
1242  // Extract the noise parts from the grid and put them on a temporary list.
1243  ColPartition_LIST parts_list;
1244  ColPartition_IT part_it(&parts_list);
1245  ColPartitionGridSearch gsearch(part_grid);
1246  gsearch.StartFullSearch();
1247  ColPartition* part;
1248  while ((part = gsearch.NextFullSearch()) != NULL) {
1249  BlobRegionType type = part->blob_type();
1250  if (type == BRT_NOISE || type == BRT_RECTIMAGE || type == BRT_POLYIMAGE) {
1251  part_it.add_after_then_move(part);
1252  gsearch.RemoveBBox();
1253  }
1254  }
1255  // Render listed noise partitions to the image mask.
1256  MarkAndDeleteImageParts(rerotation, part_grid, &parts_list, image_mask);
1257 }
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
BlobRegionType
Definition: blobbox.h:57

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