22 #pragma warning(disable:4244) // Conversion warnings
26 #include "config_auto.h"
36 #include "allheaders.h"
67 if (pixGetWidth(pix) < kMinImageFindSize ||
68 pixGetHeight(pix) < kMinImageFindSize)
69 return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
71 Pix *pixr = pixReduceRankBinaryCascade(pix, 1, 0, 0, 0);
76 Pix *pixht2 = pixGenHalftoneMask(pixr,
NULL, &ht_found,
79 if (!ht_found && pixht2 !=
NULL)
82 return pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
85 Pix *pixht = pixExpandReplicate(pixht2, 2);
90 Pix *pixt = pixSeedfillBinary(
NULL, pixht, pix, 8);
91 pixOr(pixht, pixht, pixt);
95 Pix* pixfinemask = pixReduceRankBinaryCascade(pixht, 1, 1, 3, 3);
96 pixDilateBrick(pixfinemask, pixfinemask, 5, 5);
98 Pix* pixreduced = pixReduceRankBinaryCascade(pixht, 1, 1, 1, 1);
99 Pix* pixreduced2 = pixReduceRankBinaryCascade(pixreduced, 3, 3, 3, 0);
100 pixDestroy(&pixreduced);
101 pixDilateBrick(pixreduced2, pixreduced2, 5, 5);
102 Pix* pixcoarsemask = pixExpandReplicate(pixreduced2, 8);
103 pixDestroy(&pixreduced2);
106 pixAnd(pixcoarsemask, pixcoarsemask, pixfinemask);
107 pixDestroy(&pixfinemask);
109 pixDilateBrick(pixcoarsemask, pixcoarsemask, 3, 3);
110 Pix* pixmask = pixExpandReplicate(pixcoarsemask, 16);
111 pixDestroy(&pixcoarsemask);
113 pixWrite(
"junkexpandedcoarsemask.png", pixmask, IFF_PNG);
115 pixAnd(pixht, pixht, pixmask);
116 pixDestroy(&pixmask);
118 pixWrite(
"junkfinalimagemask.png", pixht, IFF_PNG);
120 Pix* result = pixCreate(pixGetWidth(pix), pixGetHeight(pix), 1);
121 pixOr(result, result, pixht);
138 pixWrite(
"junkconncompimage.png", pix, IFF_PNG);
140 *boxa = pixConnComp(pix, pixa, 8);
144 int npixes = pixaGetCount(*pixa);
145 for (
int i = 0; i < npixes; ++i) {
146 int x_start, x_end, y_start, y_end;
147 Pix* img_pix = pixaGetPix(*pixa, i, L_CLONE);
150 kMaxRectangularFraction,
151 kMaxRectangularGradient,
152 &x_start, &y_start, &x_end, &y_end)) {
153 Pix* simple_pix = pixCreate(x_end - x_start, y_end - y_start, 1);
154 pixSetAll(simple_pix);
155 pixDestroy(&img_pix);
157 pixaReplacePix(*pixa, i, simple_pix,
NULL);
158 img_pix = pixaGetPix(*pixa, i, L_CLONE);
160 l_int32 x, y, width, height;
161 boxaGetBoxGeometry(*boxa, i, &x, &y, &width, &height);
162 Box* simple_box = boxCreate(x + x_start, y + y_start,
163 x_end - x_start, y_end - y_start);
164 boxaReplaceBox(*boxa, i, simple_box);
166 pixDestroy(&img_pix);
177 static bool HScanForEdge(
uinT32* data,
int wpl,
int x_start,
int x_end,
178 int min_count,
int mid_width,
int max_count,
179 int y_end,
int y_step,
int* y_start) {
181 for (
int y = *y_start; y != y_end; y += y_step) {
184 uinT32* line = data + wpl * y;
185 for (
int x = x_start; x < x_end; ++x) {
186 if (GET_DATA_BIT(line, x))
189 if (mid_rows == 0 && pix_count < min_count)
193 if (pix_count > max_count)
196 if (mid_rows > mid_width)
209 static bool VScanForEdge(
uinT32* data,
int wpl,
int y_start,
int y_end,
210 int min_count,
int mid_width,
int max_count,
211 int x_end,
int x_step,
int* x_start) {
213 for (
int x = *x_start; x != x_end; x += x_step) {
215 uinT32* line = data + y_start * wpl;
216 for (
int y = y_start; y < y_end; ++y, line += wpl) {
217 if (GET_DATA_BIT(line, x))
220 if (mid_cols == 0 && pix_count < min_count)
224 if (pix_count > max_count)
227 if (mid_cols > mid_width)
243 double min_fraction,
double max_fraction,
244 double max_skew_gradient,
245 int* x_start,
int* y_start,
246 int* x_end,
int* y_end) {
249 *x_end = pixGetWidth(pix);
251 *y_end = pixGetHeight(pix);
253 uinT32* data = pixGetData(pix);
254 int wpl = pixGetWpl(pix);
255 bool any_cut =
false;
256 bool left_done =
false;
257 bool right_done =
false;
258 bool top_done =
false;
259 bool bottom_done =
false;
263 int width = *x_end - *x_start;
264 int min_count =
static_cast<int>(width * min_fraction);
265 int max_count =
static_cast<int>(width * max_fraction);
266 int edge_width =
static_cast<int>(width * max_skew_gradient);
267 if (HScanForEdge(data, wpl, *x_start, *x_end, min_count, edge_width,
268 max_count, *y_end, 1, y_start) && !top_done) {
273 if (HScanForEdge(data, wpl, *x_start, *x_end, min_count, edge_width,
274 max_count, *y_start, -1, y_end) && !bottom_done) {
281 int height = *y_end - *y_start;
282 min_count =
static_cast<int>(height * min_fraction);
283 max_count =
static_cast<int>(height * max_fraction);
284 edge_width =
static_cast<int>(height * max_skew_gradient);
285 if (VScanForEdge(data, wpl, *y_start, *y_end, min_count, edge_width,
286 max_count, *x_end, 1, x_start) && !left_done) {
291 if (VScanForEdge(data, wpl, *y_start, *y_end, min_count, edge_width,
292 max_count, *x_start, -1, x_end) && !right_done) {
301 return left_done && right_done && top_done && bottom_done;
309 int* x_end,
int* y_end) {
310 Box* input_box = boxCreate(*x_start, *y_start, *x_end - *x_start,
312 Box* output_box =
NULL;
313 pixClipBoxToForeground(pix, input_box,
NULL, &output_box);
314 bool result = output_box !=
NULL;
316 l_int32 x, y, width, height;
317 boxGetGeometry(output_box, &x, &y, &width, &height);
322 boxDestroy(&output_box);
324 boxDestroy(&input_box);
333 const uinT8* point) {
337 line_vector[i] =
static_cast<int>(line2[i]) - static_cast<int>(line1[i]);
338 point_vector[i] =
static_cast<int>(point[i]) - static_cast<int>(line1[i]);
340 line_vector[L_ALPHA_CHANNEL] = 0;
343 cross[COLOR_RED] = line_vector[COLOR_GREEN] * point_vector[COLOR_BLUE]
344 - line_vector[COLOR_BLUE] * point_vector[COLOR_GREEN];
345 cross[COLOR_GREEN] = line_vector[COLOR_BLUE] * point_vector[COLOR_RED]
346 - line_vector[COLOR_RED] * point_vector[COLOR_BLUE];
347 cross[COLOR_BLUE] = line_vector[COLOR_RED] * point_vector[COLOR_GREEN]
348 - line_vector[COLOR_GREEN] * point_vector[COLOR_RED];
349 cross[L_ALPHA_CHANNEL] = 0;
351 double cross_sq = 0.0;
352 double line_sq = 0.0;
354 cross_sq +=
static_cast<double>(cross[j]) * cross[j];
355 line_sq +=
static_cast<double>(line_vector[j]) * line_vector[j];
357 if (line_sq == 0.0) {
360 return cross_sq / line_sq;
367 composeRGBPixel(r, g, b, &result);
375 else if (pixel >= 255.0)
377 return static_cast<uinT8>(pixel);
391 Pix* color_map1, Pix* color_map2,
397 int width = pixGetWidth(pix);
398 int height = pixGetHeight(pix);
399 int left_pad =
MAX(rect.
left() - 2 * factor, 0) / factor;
400 int top_pad = (rect.
top() + 2 * factor + (factor - 1)) / factor;
401 top_pad =
MIN(height, top_pad);
402 int right_pad = (rect.
right() + 2 * factor + (factor - 1)) / factor;
403 right_pad =
MIN(width, right_pad);
404 int bottom_pad =
MAX(rect.
bottom() - 2 * factor, 0) / factor;
405 int width_pad = right_pad - left_pad;
406 int height_pad = top_pad - bottom_pad;
407 if (width_pad < 1 || height_pad < 1 || width_pad + height_pad < 4)
410 Box* scaled_box = boxCreate(left_pad, height - top_pad,
411 width_pad, height_pad);
412 Pix* scaled = pixClipRectangle(pix, scaled_box,
NULL);
415 STATS red_stats(0, 256);
416 STATS green_stats(0, 256);
417 STATS blue_stats(0, 256);
418 uinT32* data = pixGetData(scaled);
420 for (
int y = 0; y < height_pad; ++y) {
421 for (
int x = 0; x < width_pad; ++x, ++data) {
422 int r = GET_DATA_BYTE(data, COLOR_RED);
423 int g = GET_DATA_BYTE(data, COLOR_GREEN);
424 int b = GET_DATA_BYTE(data, COLOR_BLUE);
426 green_stats.
add(g, 1);
427 blue_stats.
add(b, 1);
434 int best_l8 =
static_cast<int>(red_stats.
ile(0.125f));
435 int best_u8 =
static_cast<int>(ceil(red_stats.
ile(0.875f)));
436 int best_i8r = best_u8 - best_l8;
437 int x_color = COLOR_RED;
438 int y1_color = COLOR_GREEN;
439 int y2_color = COLOR_BLUE;
440 int l8 =
static_cast<int>(green_stats.
ile(0.125f));
441 int u8 =
static_cast<int>(ceil(green_stats.
ile(0.875f)));
442 if (u8 - l8 > best_i8r) {
446 x_color = COLOR_GREEN;
447 y1_color = COLOR_RED;
449 l8 =
static_cast<int>(blue_stats.
ile(0.125f));
450 u8 =
static_cast<int>(ceil(blue_stats.
ile(0.875f)));
451 if (u8 - l8 > best_i8r) {
455 x_color = COLOR_BLUE;
456 y1_color = COLOR_GREEN;
457 y2_color = COLOR_RED;
459 if (best_i8r >= kMinColorDifference) {
462 uinT32* data = pixGetData(scaled);
463 for (
int im_y = 0; im_y < height_pad; ++im_y) {
464 for (
int im_x = 0; im_x < width_pad; ++im_x, ++data) {
465 int x = GET_DATA_BYTE(data, x_color);
466 int y1 = GET_DATA_BYTE(data, y1_color);
467 int y2 = GET_DATA_BYTE(data, y2_color);
472 double m1 = line1.
m();
473 double c1 = line1.
c(m1);
474 double m2 = line2.
m();
475 double c2 = line2.
c(m2);
476 double rms = line1.
rms(m1, c1) + line2.
rms(m2, c2);
480 color1[y1_color] =
ClipToByte(m1 * best_l8 + c1 + 0.5);
481 color1[y2_color] =
ClipToByte(m2 * best_l8 + c2 + 0.5);
484 color2[y1_color] =
ClipToByte(m1 * best_u8 + c1 + 0.5);
485 color2[y2_color] =
ClipToByte(m2 * best_u8 + c2 + 0.5);
492 color1[L_ALPHA_CHANNEL] = 0;
493 memcpy(color2, color1, 4);
495 if (color_map1 !=
NULL) {
496 pixSetInRectArbitrary(color_map1, scaled_box,
499 color1[COLOR_BLUE]));
500 pixSetInRectArbitrary(color_map2, scaled_box,
503 color2[COLOR_BLUE]));
504 pixSetInRectArbitrary(rms_map, scaled_box, color1[L_ALPHA_CHANNEL]);
507 boxDestroy(&scaled_box);
555 TBOX search_box(box1);
558 if (box1.
x_gap(box2) <= 0)
563 if (box1.
y_gap(box2) <= 0)
574 const FCOORD& rotation, Pix* pix) {
580 TBOX rotated_im_box(im_box);
581 rotated_im_box.
rotate(rotation);
582 Pix* rect_pix = pixCreate(box.
width(), box.
height(), 1);
583 pixRasterop(rect_pix, 0, 0, box.
width(), box.
height(),
584 PIX_SRC, pix, box.
left() - rotated_im_box.
left(),
585 rotated_im_box.
top() - box.
top());
587 pixCountPixels(rect_pix, &result,
NULL);
588 pixDestroy(&rect_pix);
596 static void AttemptToShrinkBox(
const FCOORD& rotation,
const FCOORD& rerotation,
597 const TBOX& im_box, Pix* pix,
TBOX* slice) {
598 TBOX rotated_box(*slice);
599 rotated_box.rotate(rerotation);
600 TBOX rotated_im_box(im_box);
601 rotated_im_box.rotate(rerotation);
602 int left = rotated_box.left() - rotated_im_box.left();
603 int right = rotated_box.right() - rotated_im_box.left();
604 int top = rotated_im_box.top() - rotated_box.top();
605 int bottom = rotated_im_box.top() - rotated_box.bottom();
607 top = rotated_im_box.top() - top;
608 bottom = rotated_im_box.top() - bottom;
609 left += rotated_im_box.left();
610 right += rotated_im_box.left();
611 rotated_box.set_to_given_coords(left, bottom, right, top);
612 rotated_box.rotate(rotation);
613 slice->
set_left(rotated_box.left());
641 static void CutChunkFromParts(
const TBOX& box,
const TBOX& im_box,
643 Pix* pix, ColPartition_LIST* part_list) {
645 ColPartition_IT part_it(part_list);
647 ColPartition* part = part_it.data();
648 TBOX part_box = part->bounding_box();
655 if (box.
top() < part_box.
top()) {
656 TBOX slice(part_box);
660 AttemptToShrinkBox(rotation, rerotation, im_box, pix, &slice);
661 part_it.add_before_stay_put(
668 TBOX slice(part_box);
670 if (box.
top() < part_box.
top())
676 AttemptToShrinkBox(rotation, rerotation, im_box, pix, &slice);
677 part_it.add_before_stay_put(
684 TBOX slice(part_box);
686 if (box.
top() < part_box.
top())
692 AttemptToShrinkBox(rotation, rerotation, im_box, pix, &slice);
693 part_it.add_before_stay_put(
700 TBOX slice(part_box);
704 AttemptToShrinkBox(rotation, rerotation, im_box, pix, &slice);
705 part_it.add_before_stay_put(
711 delete part_it.extract();
714 }
while (!part_it.at_first());
724 static void DivideImageIntoParts(
const TBOX& im_box,
const FCOORD& rotation,
725 const FCOORD& rerotation, Pix* pix,
727 ColPartition_LIST* part_list) {
732 ColPartition_IT part_it(part_list);
733 part_it.add_after_then_move(pix_part);
735 rectsearch->StartRectSearch(im_box);
737 while ((part = rectsearch->NextRectSearch()) !=
NULL) {
738 TBOX part_box = part->bounding_box();
741 for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
742 ColPartition* pix_part = part_it.extract();
743 pix_part->DeleteBoxes();
752 if (black_area * 2 < part_box.
area() || !im_box.
contains(part_box)) {
757 part_box.
set_top(part_box.
top() + padding / 2);
759 CutChunkFromParts(part_box, im_box, rotation, rerotation,
766 if (part_list->empty()) {
774 static int ExpandImageLeft(
const TBOX& box,
int left_limit,
775 ColPartitionGrid* part_grid) {
780 while ((part =
search.NextSideSearch(
true)) !=
NULL) {
782 const TBOX& part_box(part->bounding_box());
783 if (part_box.
y_gap(box) < 0) {
784 if (part_box.
right() > left_limit && part_box.
right() < box.
left())
785 left_limit = part_box.
right();
793 search.StartRectSearch(search_box);
794 while ((part =
search.NextRectSearch()) !=
NULL) {
796 const TBOX& part_box(part->bounding_box());
797 if (part_box.
y_gap(box) < 0) {
798 if (part_box.
right() > left_limit && part_box.
right() < box.
left()) {
799 left_limit = part_box.
right();
810 static int ExpandImageRight(
const TBOX& box,
int right_limit,
811 ColPartitionGrid* part_grid) {
816 while ((part =
search.NextSideSearch(
false)) !=
NULL) {
818 const TBOX& part_box(part->bounding_box());
819 if (part_box.
y_gap(box) < 0) {
820 if (part_box.
left() < right_limit && part_box.
left() > box.
right())
821 right_limit = part_box.
left();
829 search.StartRectSearch(search_box);
830 while ((part =
search.NextRectSearch()) !=
NULL) {
832 const TBOX& part_box(part->bounding_box());
833 if (part_box.
y_gap(box) < 0) {
834 if (part_box.
left() < right_limit && part_box.
left() > box.
right())
835 right_limit = part_box.
left();
845 static int ExpandImageBottom(
const TBOX& box,
int bottom_limit,
846 ColPartitionGrid* part_grid) {
851 while ((part =
search.NextVerticalSearch(
true)) !=
NULL) {
853 const TBOX& part_box(part->bounding_box());
854 if (part_box.
x_gap(box) < 0) {
855 if (part_box.
top() > bottom_limit && part_box.
top() < box.
bottom())
856 bottom_limit = part_box.
top();
864 search.StartRectSearch(search_box);
865 while ((part =
search.NextRectSearch()) !=
NULL) {
867 const TBOX& part_box(part->bounding_box());
868 if (part_box.
x_gap(box) < 0) {
869 if (part_box.
top() > bottom_limit && part_box.
top() < box.
bottom())
870 bottom_limit = part_box.
top();
880 static int ExpandImageTop(
const TBOX& box,
int top_limit,
881 ColPartitionGrid* part_grid) {
886 while ((part =
search.NextVerticalSearch(
false)) !=
NULL) {
888 const TBOX& part_box(part->bounding_box());
889 if (part_box.
x_gap(box) < 0) {
891 top_limit = part_box.
bottom();
899 search.StartRectSearch(search_box);
900 while ((part =
search.NextRectSearch()) !=
NULL) {
902 const TBOX& part_box(part->bounding_box());
903 if (part_box.
x_gap(box) < 0) {
905 top_limit = part_box.
bottom();
918 const TBOX& limit_box,
919 ColPartitionGrid* part_grid,
TBOX* expanded_box) {
920 *expanded_box = im_box;
923 expanded_box->
set_left(ExpandImageLeft(im_box, limit_box.
left(),
927 expanded_box->
set_right(ExpandImageRight(im_box, limit_box.
right(),
931 expanded_box->
set_top(ExpandImageTop(im_box, limit_box.
top(), part_grid));
940 return expanded_box->
area() - im_box.
area();
947 static void MaximalImageBoundingBox(ColPartitionGrid* part_grid,
TBOX* im_box) {
949 memset(dunnit, 0,
sizeof(dunnit));
950 TBOX limit_box(part_grid->bleft().x(), part_grid->bleft().y(),
951 part_grid->tright().x(), part_grid->tright().y());
952 TBOX text_box(*im_box);
953 for (
int iteration = 0; iteration <
BND_COUNT; ++iteration) {
958 for (
int dir = 0; dir <
BND_COUNT; ++dir) {
962 int area_delta = ExpandImageDir(bnd, text_box, limit_box, part_grid,
963 &expanded_boxes[bnd]);
964 if (best_delta < 0 || area_delta < best_delta) {
965 best_delta = area_delta;
971 dunnit[best_dir] =
true;
972 text_box = expanded_boxes[best_dir];
981 static void DeletePartition(ColPartition* part) {
990 part->SetBlobTypes();
1008 static bool ExpandImageIntoParts(
const TBOX& max_image_box,
1010 ColPartitionGrid* part_grid,
1011 ColPartition** part_ptr) {
1012 ColPartition* image_part = *part_ptr;
1013 TBOX im_part_box = image_part->bounding_box();
1015 tprintf(
"Searching for merge with image part:");
1016 im_part_box.
print();
1018 max_image_box.
print();
1020 rectsearch->StartRectSearch(max_image_box);
1022 ColPartition* best_part =
NULL;
1024 while ((part = rectsearch->NextRectSearch()) !=
NULL) {
1026 tprintf(
"Considering merge with part:");
1028 if (im_part_box.
contains(part->bounding_box()))
1030 else if (!max_image_box.
contains(part->bounding_box()))
1031 tprintf(
"Not within text box\n");
1041 TBOX box = part->bounding_box();
1045 rectsearch->RemoveBBox();
1046 DeletePartition(part);
1049 int x_dist =
MAX(0, box.
x_gap(im_part_box));
1050 int y_dist =
MAX(0, box.
y_gap(im_part_box));
1051 int dist = x_dist * x_dist + y_dist * y_dist;
1052 if (dist > box.
area() || dist > im_part_box.
area())
1054 if (best_part ==
NULL || dist < best_dist) {
1061 if (best_part !=
NULL) {
1063 TBOX box = best_part->bounding_box();
1065 tprintf(
"Merging image part:");
1066 im_part_box.
print();
1074 DeletePartition(image_part);
1075 part_grid->RemoveBBox(best_part);
1076 DeletePartition(best_part);
1077 rectsearch->RepositionIterator();
1085 static int IntersectArea(
const TBOX& box, ColPartition_LIST* part_list) {
1086 int intersect_area = 0;
1087 ColPartition_IT part_it(part_list);
1089 for (part_it.mark_cycle_pt(); !part_it.cycled_list();
1090 part_it.forward()) {
1091 ColPartition* image_part = part_it.data();
1093 intersect_area += intersect.
area();
1095 return intersect_area;
1103 static bool TestWeakIntersectedPart(
const TBOX& im_box,
1104 ColPartition_LIST* part_list,
1105 ColPartition* part) {
1108 TBOX part_box = part->bounding_box();
1110 int area = part_box.
area();
1111 int intersect_area = IntersectArea(part_box, part_list);
1112 if (area < 2 * intersect_area) {
1125 static void EliminateWeakParts(
const TBOX& im_box,
1126 ColPartitionGrid* part_grid,
1127 ColPartition_LIST* big_parts,
1128 ColPartition_LIST* part_list) {
1131 rectsearch.StartRectSearch(im_box);
1132 while ((part = rectsearch.NextRectSearch()) !=
NULL) {
1133 if (TestWeakIntersectedPart(im_box, part_list, part)) {
1136 rectsearch.RemoveBBox();
1137 DeletePartition(part);
1143 part->SetBlobTypes();
1147 ColPartition_IT big_it(big_parts);
1148 for (big_it.mark_cycle_pt(); !big_it.cycled_list(); big_it.forward()) {
1149 part = big_it.data();
1150 if (TestWeakIntersectedPart(im_box, part_list, part)) {
1152 DeletePartition(big_it.extract());
1161 static bool ScanForOverlappingText(ColPartitionGrid* part_grid,
TBOX* box) {
1163 TBOX padded_box(*box);
1164 padded_box.pad(kNoisePadding, kNoisePadding);
1165 rectsearch.StartRectSearch(padded_box);
1167 bool any_text_in_padded_rect =
false;
1168 while ((part = rectsearch.NextRectSearch()) !=
NULL) {
1172 any_text_in_padded_rect =
true;
1173 TBOX part_box = part->bounding_box();
1179 if (!any_text_in_padded_rect)
1188 static void MarkAndDeleteImageParts(
const FCOORD& rerotate,
1189 ColPartitionGrid* part_grid,
1190 ColPartition_LIST* image_parts,
1192 if (image_pix ==
NULL)
1194 int imageheight = pixGetHeight(image_pix);
1195 ColPartition_IT part_it(image_parts);
1196 for (; !part_it.empty(); part_it.forward()) {
1197 ColPartition* part = part_it.extract();
1198 TBOX part_box = part->bounding_box();
1200 if (!ScanForOverlappingText(part_grid, &part_box) ||
1204 part_box.
rotate(rerotate);
1205 int left = part_box.
left();
1206 int top = part_box.
top();
1207 pixRasterop(image_pix, left, imageheight - top,
1210 DeletePartition(part);
1225 ColPartition_LIST parts_list;
1226 ColPartition_IT part_it(&parts_list);
1233 part_it.add_after_then_move(part);
1238 MarkAndDeleteImageParts(rerotation, part_grid, &parts_list, image_mask);
1245 if (part_grid !=
NULL)
return;
1247 gsearch.StartFullSearch();
1249 while ((part = gsearch.NextFullSearch()) !=
NULL) {
1254 if (part_box.
width() < kMinImageFindSize ||
1257 gsearch.RemoveBBox();
1258 DeletePartition(part);
1276 const FCOORD& rerotation,
1280 ColPartition_LIST* big_parts) {
1281 int imageheight = pixGetHeight(image_pix);
1286 int nboxes = boxaGetCount(boxa);
1287 for (
int i = 0; i < nboxes; ++i) {
1288 l_int32 x, y, width, height;
1289 boxaGetBoxGeometry(boxa, i, &x, &y, &width, &height);
1290 Pix* pix = pixaGetPix(pixa, i, L_CLONE);
1291 TBOX im_box(x, imageheight -y - height, x + width, imageheight - y);
1295 ColPartition_LIST part_list;
1296 DivideImageIntoParts(im_box, rotation, rerotation, pix,
1297 &rectsearch, &part_list);
1299 pixWrite(
"junkimagecomponent.png", pix, IFF_PNG);
1300 tprintf(
"Component has %d parts\n", part_list.length());
1303 if (!part_list.empty()) {
1304 ColPartition_IT part_it(&part_list);
1305 if (part_list.singleton()) {
1310 TBOX text_box(im_box);
1311 MaximalImageBoundingBox(part_grid, &text_box);
1312 while (ExpandImageIntoParts(text_box, &rectsearch, part_grid, &part));
1313 part_it.set_to_list(&part_list);
1314 part_it.add_after_then_move(part);
1317 EliminateWeakParts(im_box, part_grid, big_parts, &part_list);
1319 for (part_it.move_to_first(); !part_it.empty(); part_it.forward()) {
1322 part_grid->
InsertBBox(
true,
true, image_part);
1323 if (!part_it.at_last()) {
1333 DeleteSmallImages(part_grid);
static Pix * FindImages(Pix *pix)
static int CountPixelsInRotatedBox(TBOX box, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
static bool BoundsWithinRect(Pix *pix, int *x_start, int *y_start, int *x_end, int *y_end)
const TBOX & bounding_box() const
const double kMaxRectangularFraction
void add(inT32 value, inT32 count)
static void ComputeRectangleColors(const TBOX &rect, Pix *pix, int factor, Pix *color_map1, Pix *color_map2, Pix *rms_map, uinT8 *color1, uinT8 *color2)
BlobRegionType blob_type() const
double rms(double m, double c) const
int textord_tabfind_show_images
const int kMinImageFindSize
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
const double kMinRectangularFraction
void InsertBBox(bool h_spread, bool v_spread, BBC *bbox)
void DisplayBoxes(ScrollView *window)
static double ColorDistanceFromLine(const uinT8 *line1, const uinT8 *line2, const uinT8 *point)
double ile(double frac) const
void SetUniqueMode(bool mode)
LIST search(LIST list, void *key, int_compare is_equal)
int y_gap(const TBOX &box) const
const double kMaxRectangularGradient
void add(double x, double y)
#define INT_VAR(name, val, comment)
static void FindImagePartitions(Pix *image_pix, const FCOORD &rotation, const FCOORD &rerotation, TO_BLOCK *block, TabFind *tab_grid, ColPartitionGrid *part_grid, ColPartition_LIST *big_parts)
const double kRMSFitScaling
const int kMinColorDifference
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)
TBOX intersection(const TBOX &box) const
ScrollView * MakeWindow(int x, int y, const char *window_name)
int x_gap(const TBOX &box) const
static uinT32 ComposeRGB(uinT32 r, uinT32 g, uinT32 b)
static void ConnCompAndRectangularize(Pix *pix, Boxa **boxa, Pixa **pixa)
static ColPartition * FakePartition(const TBOX &box, PolyBlockType block_type, BlobRegionType blob_type, BlobTextFlowType flow)
bool contains(const FCOORD pt) const
static uinT8 ClipToByte(double pixel)
static bool BlankImageInBetween(const TBOX &box1, const TBOX &box2, const TBOX &im_box, const FCOORD &rotation, Pix *pix)
static void TransferImagePartsToImageMask(const FCOORD &rerotation, ColPartitionGrid *part_grid, Pix *image_mask)
void AddPartner(bool upper, ColPartition *partner)
bool overlap(const TBOX &box) const
void rotate(const FCOORD &vec)