tesseract v5.3.3.20231005
tesseract::ImageFind Class Reference

#include <imagefind.h>

Static Public Member Functions

static Image FindImages (Image pix, DebugPixa *pixa_debug)
 
static bool BoundsWithinRect (Image pix, int *x_start, int *y_start, int *x_end, int *y_end)
 
static double ColorDistanceFromLine (const uint8_t *line1, const uint8_t *line2, const uint8_t *point)
 
static bool BlankImageInBetween (const TBOX &box1, const TBOX &box2, const TBOX &im_box, const FCOORD &rotation, Image pix)
 
static int CountPixelsInRotatedBox (TBOX box, const TBOX &im_box, const FCOORD &rotation, Image pix)
 
static void TransferImagePartsToImageMask (const FCOORD &rerotation, ColPartitionGrid *part_grid, Image image_mask)
 
static void FindImagePartitions (Image 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 38 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,
Image  pix 
)
static

Definition at line 437 of file imagefind.cpp.

438 {
439 TBOX search_box(box1);
440 search_box += box2;
441 if (box1.x_gap(box2) >= box1.y_gap(box2)) {
442 if (box1.x_gap(box2) <= 0) {
443 return true;
444 }
445 search_box.set_left(std::min(box1.right(), box2.right()));
446 search_box.set_right(std::max(box1.left(), box2.left()));
447 } else {
448 if (box1.y_gap(box2) <= 0) {
449 return true;
450 }
451 search_box.set_top(std::max(box1.bottom(), box2.bottom()));
452 search_box.set_bottom(std::min(box1.top(), box2.top()));
453 }
454 return CountPixelsInRotatedBox(search_box, im_box, rotation, pix) == 0;
455}
@ TBOX
static int CountPixelsInRotatedBox(TBOX box, const TBOX &im_box, const FCOORD &rotation, Image pix)
Definition: imagefind.cpp:459

◆ BoundsWithinRect()

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

Definition at line 343 of file imagefind.cpp.

343 {
344 Box *input_box = boxCreate(*x_start, *y_start, *x_end - *x_start, *y_end - *y_start);
345 Box *output_box = nullptr;
346 pixClipBoxToForeground(pix, input_box, nullptr, &output_box);
347 bool result = output_box != nullptr;
348 if (result) {
349 l_int32 x, y, width, height;
350 boxGetGeometry(output_box, &x, &y, &width, &height);
351 *x_start = x;
352 *y_start = y;
353 *x_end = x + width;
354 *y_end = y + height;
355 boxDestroy(&output_box);
356 }
357 boxDestroy(&input_box);
358 return result;
359}
const double y

◆ ColorDistanceFromLine()

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

Definition at line 364 of file imagefind.cpp.

365 {
366 int line_vector[kRGBRMSColors];
367 int point_vector[kRGBRMSColors];
368 for (int i = 0; i < kRGBRMSColors; ++i) {
369 line_vector[i] = static_cast<int>(line2[i]) - static_cast<int>(line1[i]);
370 point_vector[i] = static_cast<int>(point[i]) - static_cast<int>(line1[i]);
371 }
372 line_vector[L_ALPHA_CHANNEL] = 0;
373 // Now the cross product in 3d.
374 int cross[kRGBRMSColors];
375 cross[COLOR_RED] = line_vector[COLOR_GREEN] * point_vector[COLOR_BLUE] -
376 line_vector[COLOR_BLUE] * point_vector[COLOR_GREEN];
377 cross[COLOR_GREEN] = line_vector[COLOR_BLUE] * point_vector[COLOR_RED] -
378 line_vector[COLOR_RED] * point_vector[COLOR_BLUE];
379 cross[COLOR_BLUE] = line_vector[COLOR_RED] * point_vector[COLOR_GREEN] -
380 line_vector[COLOR_GREEN] * point_vector[COLOR_RED];
381 cross[L_ALPHA_CHANNEL] = 0;
382 // Now the sums of the squares.
383 double cross_sq = 0.0;
384 double line_sq = 0.0;
385 for (int j = 0; j < kRGBRMSColors; ++j) {
386 cross_sq += static_cast<double>(cross[j]) * cross[j];
387 line_sq += static_cast<double>(line_vector[j]) * line_vector[j];
388 }
389 if (line_sq == 0.0) {
390 return 0.0;
391 }
392 return cross_sq / line_sq; // This is the squared distance.
393}
const int kRGBRMSColors
Definition: colpartition.h:36

◆ CountPixelsInRotatedBox()

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

Definition at line 459 of file imagefind.cpp.

460 {
461 // Intersect it with the image box.
462 box &= im_box; // This is in-place box intersection.
463 if (box.null_box()) {
464 return 0;
465 }
466 box.rotate(rotation);
467 TBOX rotated_im_box(im_box);
468 rotated_im_box.rotate(rotation);
469 Image rect_pix = pixCreate(box.width(), box.height(), 1);
470 pixRasterop(rect_pix, 0, 0, box.width(), box.height(), PIX_SRC, pix,
471 box.left() - rotated_im_box.left(), rotated_im_box.top() - box.top());
472 l_int32 result;
473 pixCountPixels(rect_pix, &result, nullptr);
474 rect_pix.destroy();
475 return result;
476}

◆ FindImagePartitions()

void tesseract::ImageFind::FindImagePartitions ( Image  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 1141 of file imagefind.cpp.

1144 {
1145 int imageheight = pixGetHeight(image_pix);
1146 Boxa *boxa;
1147 Pixa *pixa;
1148 ConnCompAndRectangularize(image_pix, pixa_debug, &boxa, &pixa);
1149 // Iterate the connected components in the image regions mask.
1150 int nboxes = 0;
1151 if (boxa != nullptr && pixa != nullptr) {
1152 nboxes = boxaGetCount(boxa);
1153 }
1154 for (int i = 0; i < nboxes; ++i) {
1155 l_int32 x, y, width, height;
1156 boxaGetBoxGeometry(boxa, i, &x, &y, &width, &height);
1157 Image pix = pixaGetPix(pixa, i, L_CLONE);
1158 TBOX im_box(x, imageheight - y - height, x + width, imageheight - y);
1159 im_box.rotate(rotation); // Now matches all partitions and blobs.
1160 ColPartitionGridSearch rectsearch(part_grid);
1161 rectsearch.SetUniqueMode(true);
1162 ColPartition_LIST part_list;
1163 DivideImageIntoParts(im_box, rotation, rerotation, pix, &rectsearch, &part_list);
1164 if (textord_tabfind_show_images && pixa_debug != nullptr) {
1165 pixa_debug->AddPix(pix, "ImageComponent");
1166 tprintf("Component has %d parts\n", part_list.length());
1167 }
1168 pix.destroy();
1169 if (!part_list.empty()) {
1170 ColPartition_IT part_it(&part_list);
1171 if (part_list.singleton()) {
1172 // We didn't have to chop it into a polygon to fit around text, so
1173 // try expanding it to merge fragmented image parts, as long as it
1174 // doesn't touch strong text.
1175 ColPartition *part = part_it.extract();
1176 TBOX text_box(im_box);
1177 MaximalImageBoundingBox(part_grid, &text_box);
1178 while (ExpandImageIntoParts(text_box, &rectsearch, part_grid, &part)) {
1179 ;
1180 }
1181 part_it.set_to_list(&part_list);
1182 part_it.add_after_then_move(part);
1183 im_box = part->bounding_box();
1184 }
1185 EliminateWeakParts(im_box, part_grid, big_parts, &part_list);
1186 // Iterate the part_list and put the parts into the grid.
1187 for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
1188 ColPartition *image_part = part_it.extract();
1189 im_box = image_part->bounding_box();
1190 part_grid->InsertBBox(true, true, image_part);
1191 if (!part_it.at_last()) {
1192 ColPartition *neighbour = part_it.data_relative(1);
1193 image_part->AddPartner(false, neighbour);
1194 neighbour->AddPartner(true, image_part);
1195 }
1196 }
1197 }
1198 }
1199 boxaDestroy(&boxa);
1200 pixaDestroy(&pixa);
1201 DeleteSmallImages(part_grid);
1202#ifndef GRAPHICS_DISABLED
1203 if (textord_tabfind_show_images) {
1204 ScrollView *images_win_ = part_grid->MakeWindow(1000, 400, "With Images");
1205 part_grid->DisplayBoxes(images_win_);
1206 }
1207#endif
1208}
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:919

◆ FindImages()

Image tesseract::ImageFind::FindImages ( Image  pix,
DebugPixa pixa_debug 
)
static

Definition at line 252 of file imagefind.cpp.

252 {
253 auto width = pixGetWidth(pix);
254 auto height = pixGetHeight(pix);
255 // Not worth looking at small images.
256 // Leptonica will print an error message and return nullptr if we call
257 // pixGenHalftoneMask(pixr, nullptr, ...) with width or height < 100
258 // for the reduced image, so we want to bypass that, too.
259 if (width / 2 < kMinImageFindSize || height / 2 < kMinImageFindSize) {
260 return pixCreate(width, height, 1);
261 }
262
263 // Reduce by factor 2.
264 Image pixr = pixReduceRankBinaryCascade(pix, 1, 0, 0, 0);
265 if (textord_tabfind_show_images && pixa_debug != nullptr) {
266 pixa_debug->AddPix(pixr, "CascadeReduced");
267 }
268
269 // Get the halftone mask directly from Leptonica.
270 l_int32 ht_found = 0;
271 Pixa *pixadb = (textord_tabfind_show_images && pixa_debug != nullptr) ? pixaCreate(0) : nullptr;
272 Image pixht2 = pixGenerateHalftoneMask(pixr, nullptr, &ht_found, pixadb);
273 if (pixadb) {
274 Image pixdb = pixaDisplayTiledInColumns(pixadb, 3, 1.0, 20, 2);
275 if (textord_tabfind_show_images && pixa_debug != nullptr) {
276 pixa_debug->AddPix(pixdb, "HalftoneMask");
277 }
278 pixdb.destroy();
279 pixaDestroy(&pixadb);
280 }
281 pixr.destroy();
282 if (!ht_found && pixht2 != nullptr) {
283 pixht2.destroy();
284 }
285 if (pixht2 == nullptr) {
286 return pixCreate(width, height, 1);
287 }
288
289 // Expand back up again.
290 Image pixht = pixExpandReplicate(pixht2, 2);
291 if (textord_tabfind_show_images && pixa_debug != nullptr) {
292 pixa_debug->AddPix(pixht, "HalftoneReplicated");
293 }
294 pixht2.destroy();
295
296 // Fill to capture pixels near the mask edges that were missed
297 Image pixt = pixSeedfillBinary(nullptr, pixht, pix, 8);
298 pixht |= pixt;
299 pixt.destroy();
300
301 // Eliminate lines and bars that may be joined to images.
302 Image pixfinemask = pixReduceRankBinaryCascade(pixht, 1, 1, 3, 3);
303 pixDilateBrick(pixfinemask, pixfinemask, 5, 5);
304 if (textord_tabfind_show_images && pixa_debug != nullptr) {
305 pixa_debug->AddPix(pixfinemask, "FineMask");
306 }
307 Image pixreduced = pixReduceRankBinaryCascade(pixht, 1, 1, 1, 1);
308 Image pixreduced2 = pixReduceRankBinaryCascade(pixreduced, 3, 3, 3, 0);
309 pixreduced.destroy();
310 pixDilateBrick(pixreduced2, pixreduced2, 5, 5);
311 Image pixcoarsemask = pixExpandReplicate(pixreduced2, 8);
312 pixreduced2.destroy();
313 if (textord_tabfind_show_images && pixa_debug != nullptr) {
314 pixa_debug->AddPix(pixcoarsemask, "CoarseMask");
315 }
316 // Combine the coarse and fine image masks.
317 pixcoarsemask &= pixfinemask;
318 pixfinemask.destroy();
319 // Dilate a bit to make sure we get everything.
320 pixDilateBrick(pixcoarsemask, pixcoarsemask, 3, 3);
321 Image pixmask = pixExpandReplicate(pixcoarsemask, 16);
322 pixcoarsemask.destroy();
323 if (textord_tabfind_show_images && pixa_debug != nullptr) {
324 pixa_debug->AddPix(pixmask, "MaskDilated");
325 }
326 // And the image mask with the line and bar remover.
327 pixht &= pixmask;
328 pixmask.destroy();
329 if (textord_tabfind_show_images && pixa_debug != nullptr) {
330 pixa_debug->AddPix(pixht, "FinalMask");
331 }
332 // Make the result image the same size as the input.
333 Image result = pixCreate(width, height, 1);
334 result |= pixht;
335 pixht.destroy();
336 return result;
337}
const int kMinImageFindSize
Definition: imagefind.cpp:48

◆ TransferImagePartsToImageMask()

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

Definition at line 1088 of file imagefind.cpp.

1089 {
1090 // Extract the noise parts from the grid and put them on a temporary list.
1091 ColPartition_LIST parts_list;
1092 ColPartition_IT part_it(&parts_list);
1093 ColPartitionGridSearch gsearch(part_grid);
1094 gsearch.StartFullSearch();
1095 ColPartition *part;
1096 while ((part = gsearch.NextFullSearch()) != nullptr) {
1097 BlobRegionType type = part->blob_type();
1098 if (type == BRT_NOISE || type == BRT_RECTIMAGE || type == BRT_POLYIMAGE) {
1099 part_it.add_after_then_move(part);
1100 gsearch.RemoveBBox();
1101 }
1102 }
1103 // Render listed noise partitions to the image mask.
1104 MarkAndDeleteImageParts(rerotation, part_grid, &parts_list, image_mask);
1105}
BlobRegionType
Definition: blobbox.h:74
@ BRT_NOISE
Definition: blobbox.h:75
@ BRT_POLYIMAGE
Definition: blobbox.h:79
@ BRT_RECTIMAGE
Definition: blobbox.h:78
type
Definition: upload.py:458

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