25 const int CubeLineSegmenter::kLineSepMorphMinHgt = 20;
26 const int CubeLineSegmenter::kHgtBins = 20;
27 const double CubeLineSegmenter::kMaxValidLineRatio = 3.2;
28 const int CubeLineSegmenter::kMaxConnCompHgt = 150;
29 const int CubeLineSegmenter::kMaxConnCompWid = 500;
30 const int CubeLineSegmenter::kMaxHorzAspectRatio = 50;
31 const int CubeLineSegmenter::kMaxVertAspectRatio = 20;
32 const int CubeLineSegmenter::kMinWid = 2;
33 const int CubeLineSegmenter::kMinHgt = 2;
34 const float CubeLineSegmenter::kMinValidLineHgtRatio = 2.5;
55 if (lines_pixa_ != NULL) {
56 pixaDestroy(&lines_pixa_);
60 if (con_comps_ != NULL) {
61 pixaDestroy(&con_comps_);
65 if (columns_ != NULL) {
66 pixaaDestroy(&columns_);
72 double CubeLineSegmenter::ValidityRatio(Pix *line_mask_pix, Box *line_box) {
73 return line_box->h / est_alef_hgt_;
77 bool CubeLineSegmenter::ValidLine(Pix *line_mask_pix, Box *line_box) {
78 double validity_ratio = ValidityRatio(line_mask_pix, line_box);
80 return validity_ratio < kMaxValidLineRatio;
85 Pixa *CubeLineSegmenter::VerticalClosing(Pix *pix,
86 int threshold, Boxa **boxa) {
87 char sequence_str[16];
90 sprintf(sequence_str,
"c100.%d", threshold);
91 Pix *morphed_pix = pixMorphCompSequence(pix, sequence_str, 0);
92 if (morphed_pix == NULL) {
98 (*boxa) = pixConnComp(morphed_pix, &pixac, 8);
100 pixDestroy(&morphed_pix);
102 if ((*boxa) == NULL) {
110 static void CleanupCrackLine(
int line_cnt, Pixa **lines_pixa,
111 Boxa **line_con_comps,
112 Pixa **line_con_comps_pix) {
113 for (
int line = 0; line < line_cnt; line++) {
114 if (lines_pixa[line] != NULL) {
115 pixaDestroy(&lines_pixa[line]);
120 boxaDestroy(line_con_comps);
121 pixaDestroy(line_con_comps_pix);
125 Pixa *CubeLineSegmenter::CrackLine(Pix *cracked_line_pix,
126 Box *cracked_line_box,
int line_cnt) {
128 Pixa **lines_pixa =
new Pixa*[line_cnt];
130 memset(lines_pixa, 0, line_cnt *
sizeof(*lines_pixa));
133 Pixa *line_con_comps_pix;
134 Boxa *line_con_comps = ComputeLineConComps(cracked_line_pix,
135 cracked_line_box, &line_con_comps_pix);
137 if (line_con_comps == NULL) {
143 for (
int con = 0; con < line_con_comps->n; con++) {
144 Box *con_box = line_con_comps->box[con];
145 Pix *con_pix = line_con_comps_pix->pix[con];
146 int mid_y = (con_box->y - cracked_line_box->y) + (con_box->h / 2),
147 line_idx =
MIN(line_cnt - 1,
148 (mid_y * line_cnt / cracked_line_box->h));
151 if (lines_pixa[line_idx] == NULL) {
152 lines_pixa[line_idx] = pixaCreate(line_con_comps->n);
153 if (lines_pixa[line_idx] == NULL) {
154 CleanupCrackLine(line_cnt, lines_pixa, &line_con_comps,
155 &line_con_comps_pix);
161 if (pixaAddPix(lines_pixa[line_idx], con_pix, L_CLONE) != 0 ||
162 pixaAddBox(lines_pixa[line_idx], con_box, L_CLONE)) {
163 CleanupCrackLine(line_cnt, lines_pixa, &line_con_comps,
164 &line_con_comps_pix);
170 Pixa *lines = pixaCreate(line_cnt);
174 for (
int line = 0; line < line_cnt; line++) {
175 Pixa *line_pixa = lines_pixa[line];
178 if (line_pixa == NULL) {
185 Pix *line_pix = Pixa2Pix(line_pixa, &line_box);
186 if (line_pix == NULL ||
188 ValidLine(line_pix, line_box) ==
false ||
189 pixaAddPix(lines, line_pix, L_INSERT) != 0 ||
190 pixaAddBox(lines, line_box, L_INSERT) != 0) {
191 if (line_pix != NULL) {
192 pixDestroy(&line_pix);
195 if (line_box != NULL) {
196 boxDestroy(&line_box);
206 CleanupCrackLine(line_cnt, lines_pixa, &line_con_comps,
207 &line_con_comps_pix);
209 if (success ==
false) {
218 Pixa *CubeLineSegmenter::CrackLine(Pix *cracked_line_pix,
219 Box *cracked_line_box) {
221 int max_line_cnt =
static_cast<int>((cracked_line_box->h /
222 est_alef_hgt_) + 0.5);
223 if (max_line_cnt < 2) {
227 for (
int line_cnt = 2; line_cnt < max_line_cnt; line_cnt++) {
228 Pixa *lines = CrackLine(cracked_line_pix, cracked_line_box, line_cnt);
238 Pixa *CubeLineSegmenter::SplitLine(Pix *line_mask_pix, Box *line_box) {
240 Pix *line_pix = pixClone(line_mask_pix);
242 if (line_pix == NULL) {
247 pixRasterop(line_pix, 0, 0, line_pix->w, line_pix->h,
248 PIX_SRC & PIX_DST, img_, line_box->x, line_box->y);
252 int morph_hgt = kLineSepMorphMinHgt - 1,
253 best_threshold = kLineSepMorphMinHgt - 1,
254 max_valid_portion = 0;
260 pixac = VerticalClosing(line_pix, morph_hgt, &boxa);
268 for (line = 0; line < pixac->n; line++) {
269 boxa->box[line]->x += line_box->x;
270 boxa->box[line]->y += line_box->y;
272 if (ValidLine(pixac->pix[line], boxa->box[line]) ==
true) {
277 valid_portion += boxa->box[line]->h;
282 if (valid_line_cnt == pixac->n) {
284 pixDestroy(&line_pix);
289 if (valid_portion > max_valid_portion) {
290 max_valid_portion = valid_portion;
291 best_threshold = morph_hgt;
299 while (morph_hgt > 0);
303 pixac = CrackLine(line_pix, line_box);
305 pixDestroy(&line_pix);
311 if (max_valid_portion > 0) {
313 pixac = VerticalClosing(line_pix, best_threshold, &boxa);
317 for (
int line = 0; line < pixac->n; line++) {
318 boxa->box[line]->x += line_box->x;
319 boxa->box[line]->y += line_box->y;
322 if (ValidLine(pixac->pix[line], boxa->box[line]) ==
false) {
323 pixaRemovePix(pixac, line);
329 pixDestroy(&line_pix);
334 pixDestroy(&line_pix);
340 bool CubeLineSegmenter::SmallLine(Box *line_box) {
341 return line_box->h <= (kMinValidLineHgtRatio * est_dot_hgt_);
345 Boxa * CubeLineSegmenter::ComputeLineConComps(Pix *line_mask_pix,
347 Pixa **con_comps_pixa) {
349 Pix *line_pix = pixClone(line_mask_pix);
351 if (line_pix == NULL) {
356 pixRasterop(line_pix, 0, 0, line_pix->w, line_pix->h,
357 PIX_SRC & PIX_DST, img_, line_box->x, line_box->y);
360 Boxa *line_con_comps = pixConnComp(line_pix, con_comps_pixa, 8);
362 pixDestroy(&line_pix);
365 for (
int con = 0; con < line_con_comps->n; con++) {
366 line_con_comps->box[con]->x += line_box->x;
367 line_con_comps->box[con]->y += line_box->y;
370 return line_con_comps;
374 Pix *CubeLineSegmenter::PixUnion(Pix *dest_pix, Box *dest_box,
375 Pix *src_pix, Box *src_box) {
377 BOX *union_box = boxBoundingRegion(src_box, dest_box);
380 Pix *union_pix = pixCreate(union_box->w, union_box->h, src_pix->d);
381 if (union_pix == NULL) {
386 pixRasterop(union_pix,
387 src_box->x - union_box->x, src_box->y - union_box->y,
388 src_box->w, src_box->h, PIX_SRC | PIX_DST, src_pix, 0, 0);
390 pixRasterop(union_pix,
391 dest_box->x - union_box->x, dest_box->y - union_box->y,
392 dest_box->w, dest_box->h, PIX_SRC | PIX_DST, dest_pix, 0, 0);
395 *dest_box = *union_box;
397 boxDestroy(&union_box);
403 Pix *CubeLineSegmenter::Pixa2Pix(Pixa *pixa, Box **dest_box,
404 int start_pix,
int pix_cnt) {
411 for (
int pix_idx = start_pix; pix_idx < (start_pix + pix_cnt); pix_idx++) {
412 Box *pix_box = pixa->boxa->box[pix_idx];
414 UpdateRange(pix_box->x, pix_box->x + pix_box->w, &min_x, &max_x);
415 UpdateRange(pix_box->y, pix_box->y + pix_box->h, &min_y, &max_y);
418 (*dest_box) = boxCreate(min_x, min_y, max_x - min_x, max_y - min_y);
419 if ((*dest_box) == NULL) {
424 Pix *union_pix = pixCreate((*dest_box)->w, (*dest_box)->h, img_->d);
425 if (union_pix == NULL) {
426 boxDestroy(dest_box);
432 for (
int pix_idx = start_pix; pix_idx < (start_pix + pix_cnt); pix_idx++) {
433 Box *pix_box = pixa->boxa->box[pix_idx];
434 Pix *con_pix = pixa->pix[pix_idx];
436 pixRasterop(union_pix,
437 pix_box->x - (*dest_box)->x, pix_box->y - (*dest_box)->y,
438 pix_box->w, pix_box->h, PIX_SRC | PIX_DST, con_pix, 0, 0);
445 Pix *CubeLineSegmenter::Pixa2Pix(Pixa *pixa, Box **dest_box) {
446 return Pixa2Pix(pixa, dest_box, 0, pixa->n);
450 bool CubeLineSegmenter::MergeLine(Pix *line_mask_pix, Box *line_box,
451 Pixa *lines, Boxaa *lines_con_comps) {
453 Pixa *small_con_comps_pix;
454 Boxa *small_line_con_comps = ComputeLineConComps(line_mask_pix,
455 line_box, &small_con_comps_pix);
457 if (small_line_con_comps == NULL) {
462 for (
int con = 0; con < small_line_con_comps->n; con++) {
463 Box *small_con_comp_box = small_line_con_comps->box[con];
466 small_box_right = small_con_comp_box->x + small_con_comp_box->w,
467 small_box_bottom = small_con_comp_box->y + small_con_comp_box->h;
470 for (
int line = 0; line < lines->n; line++) {
471 if (SmallLine(lines->boxa->box[line]) ==
true) {
476 Boxa *line_con_comps = lines_con_comps->boxa[line];
478 for (
int lcon = 0; lcon < line_con_comps->n; lcon++) {
479 Box *con_comp_box = line_con_comps->box[lcon];
482 box_right = con_comp_box->x + con_comp_box->w,
483 box_bottom = con_comp_box->y + con_comp_box->h;
485 xdist =
MAX(small_con_comp_box->x, con_comp_box->x) -
486 MIN(small_box_right, box_right);
488 ydist =
MAX(small_con_comp_box->y, con_comp_box->y) -
489 MIN(small_box_bottom, box_bottom);
493 if (best_line == -1 || ydist < best_dist) {
502 if (best_line != -1 && best_dist < est_alef_hgt_) {
504 Pix *
new_line = PixUnion(lines->pix[best_line],
505 lines->boxa->box[best_line],
506 small_con_comps_pix->pix[con], small_con_comp_box);
512 pixDestroy(&lines->pix[best_line]);
517 pixaDestroy(&small_con_comps_pix);
518 boxaDestroy(&small_line_con_comps);
524 bool CubeLineSegmenter::AddLines(Pixa *lines) {
527 Boxaa *lines_con_comps = boxaaCreate(lines->n);
528 if (lines_con_comps == NULL) {
532 for (
int line = 0; line < lines->n; line++) {
534 if (ValidLine(lines->pix[line], lines->boxa->box[line]) ==
false) {
536 Pixa *split_lines = SplitLine(lines->pix[line],
537 lines->boxa->box[line]);
540 if (pixaRemovePix(lines, line) != 0) {
546 if (split_lines == NULL) {
551 for (
int s_line = 0; s_line < split_lines->n; s_line++) {
552 Pix *sp_line = pixaGetPix(split_lines, s_line, L_CLONE);
553 Box *sp_box = boxaGetBox(split_lines->boxa, s_line, L_CLONE);
555 if (sp_line == NULL || sp_box == NULL) {
560 if (pixaInsertPix(lines, ++line, sp_line, sp_box) != 0) {
566 pixaDestroy(&split_lines);
571 for (
int line = 0; line < lines->n; line++) {
572 Boxa *line_con_comps = ComputeLineConComps(lines->pix[line],
573 lines->boxa->box[line], NULL);
575 if (line_con_comps == NULL) {
580 if (boxaaAddBoxa(lines_con_comps, line_con_comps, L_INSERT) != 0) {
587 for (
int line = 0; line < lines->n; line++) {
589 if (SmallLine(lines->boxa->box[line]) ==
true) {
591 if (MergeLine(lines->pix[line], lines->boxa->box[line],
592 lines, lines_con_comps) ==
true) {
594 if (pixaRemovePix(lines, line) != 0) {
598 if (boxaaRemoveBoxa(lines_con_comps, line) != 0) {
607 boxaaDestroy(&lines_con_comps);
610 if (pixaaAddPixa(columns_, lines, L_INSERT) != 0) {
618 int *CubeLineSegmenter::IndexRTL(Pixa *pixa) {
619 int *pix_index =
new int[pixa->n];
621 for (
int pix = 0; pix < pixa->n; pix++) {
622 pix_index[pix] = pix;
625 for (
int ipix = 0; ipix < pixa->n; ipix++) {
626 for (
int jpix = ipix + 1; jpix < pixa->n; jpix++) {
627 Box *ipix_box = pixa->boxa->box[pix_index[ipix]],
628 *jpix_box = pixa->boxa->box[pix_index[jpix]];
631 if ((ipix_box->x + ipix_box->w) < (jpix_box->x + jpix_box->w)) {
632 int temp = pix_index[ipix];
633 pix_index[ipix] = pix_index[jpix];
634 pix_index[jpix] = temp;
643 bool CubeLineSegmenter::LineSegment() {
647 Pix *pix_temp1 = pixMorphCompSequence(img_,
"c5.500", 0);
648 if (pix_temp1 == NULL) {
654 Boxa *boxa = pixConnComp(pix_temp1, &pixam, 8);
660 int init_morph_min_hgt = kLineSepMorphMinHgt;
661 char sequence_str[16];
662 sprintf(sequence_str,
"c100.%d", init_morph_min_hgt);
665 Pixa *pixad = pixaMorphSequenceByRegion(img_, pixam, sequence_str, 0, 0);
671 int col_cnt = boxaGetCount(boxa);
674 columns_ = pixaaCreate(col_cnt);
675 if (columns_ == NULL) {
680 int *col_order = IndexRTL(pixad);
681 if (col_order == NULL) {
687 for (
int col_idx = 0; col_idx < col_cnt; col_idx++) {
688 int col = col_order[col_idx];
691 Pix *pixt3 = pixaGetPix(pixad, col, L_CLONE);
697 Box *col_box = pixad->boxa->box[col];
700 Boxa *boxa2 = pixConnComp(pixt3, &pixac, 8);
707 for (
int line = 0; line < pixac->n; line++) {
708 pixac->boxa->box[line]->x += col_box->x;
709 pixac->boxa->box[line]->y += col_box->y;
713 if (AddLines(pixac) ==
true) {
714 if (pixaaAddBox(columns_, col_box, L_CLONE) != 0) {
723 line_cnt_ += columns_->pixa[col_idx]->n;
731 pixDestroy(&pix_temp1);
737 bool CubeLineSegmenter::EstimateFontParams() {
738 int hgt_hist[kHgtBins];
743 memset(hgt_hist, 0,
sizeof(hgt_hist));
748 for (
int con = 0; con < con_comps_->n; con++) {
750 if (con_comps_->boxa->box[con]->h > kMaxConnCompHgt ||
751 con_comps_->boxa->box[con]->w > kMaxConnCompWid) {
755 max_hgt =
MAX(max_hgt, con_comps_->boxa->box[con]->h);
763 memset(hgt_hist, 0,
sizeof(hgt_hist));
767 for (
int con = 0; con < con_comps_->n; con++) {
769 if (con_comps_->boxa->box[con]->h > kMaxConnCompHgt ||
770 con_comps_->boxa->box[con]->w > kMaxConnCompWid) {
774 int bin =
static_cast<int>(kHgtBins * con_comps_->boxa->box[con]->h /
776 bin =
MIN(bin, kHgtBins - 1);
778 mean_hgt += con_comps_->boxa->box[con]->h;
781 mean_hgt /= con_comps_->n;
786 for (
int bin = 0; bin < kHgtBins; bin++) {
790 for (
int ibin = 0; ibin < 2; ibin++) {
791 for (
int jbin = ibin + 1; jbin < kHgtBins; jbin++) {
792 if (hgt_hist[idx[ibin]] < hgt_hist[idx[jbin]]) {
793 int swap = idx[ibin];
794 idx[ibin] = idx[jbin];
802 est_dot_hgt_ = (1.0 * (idx[0] + 1) * max_hgt / kHgtBins);
803 est_alef_hgt_ = (1.0 * (idx[1] + 1) * max_hgt / kHgtBins);
806 if (est_alef_hgt_ < (est_dot_hgt_ * 2)) {
808 est_alef_hgt_ = mean_hgt * 1.5;
809 est_dot_hgt_ = est_alef_hgt_ / 5.0;
812 est_alef_hgt_ =
MAX(est_alef_hgt_, est_dot_hgt_ * 4.0);
818 Pix *CubeLineSegmenter::CleanUp(Pix *orig_img) {
820 Pix *pix_temp0 = pixMorphCompSequence(orig_img,
"o300.2", 0);
821 pixXor(pix_temp0, pix_temp0, orig_img);
824 Pix *pix_temp1 = pixMorphCompSequence(pix_temp0,
"o2.300", 0);
825 pixXor(pix_temp1, pix_temp1, pix_temp0);
827 pixDestroy(&pix_temp0);
831 Boxa *boxa = pixConnComp(pix_temp1, &con_comps, 8);
837 for (
int con = 0; con < con_comps->n; con++) {
838 Box *box = boxa->box[con];
841 if ((box->w > (box->h * kMaxHorzAspectRatio)) ||
842 (box->h > (box->w * kMaxVertAspectRatio)) ||
843 (box->w < kMinWid && box->h < kMinHgt)) {
844 pixRasterop(pix_temp1, box->x, box->y, box->w, box->h,
845 PIX_SRC ^ PIX_DST, con_comps->pix[con], 0, 0);
849 pixaDestroy(&con_comps);
856 bool CubeLineSegmenter::Init() {
861 if (orig_img_ == NULL) {
871 if (init_ ==
false && Init() ==
false) {
875 if (line < 0 || line >= line_cnt_) {
879 (*line_box) = lines_pixa_->boxa->box[line];
880 return lines_pixa_->pix[line];
886 bool CubeLineSegmenter::FindLines() {
888 Pix *gray_scale_img = NULL;
889 if (orig_img_->d != 2 && orig_img_->d != 8) {
890 gray_scale_img = pixConvertTo8(orig_img_,
false);
891 if (gray_scale_img == NULL) {
895 gray_scale_img = orig_img_;
899 Pix *thresholded_img;
900 thresholded_img = pixThresholdToBinary(gray_scale_img, 128);
902 if (gray_scale_img != orig_img_) {
903 pixDestroy(&gray_scale_img);
906 if (thresholded_img == NULL) {
911 Pix *deskew_img = pixDeskew(thresholded_img, 2);
912 if (deskew_img == NULL) {
916 pixDestroy(&thresholded_img);
918 img_ = CleanUp(deskew_img);
919 pixDestroy(&deskew_img);
924 pixDestroy(&deskew_img);
927 Boxa *boxa = pixConnComp(img_, &con_comps_, 8);
935 if (EstimateFontParams() ==
false) {
940 if (LineSegment() ==
false) {
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
CubeLineSegmenter(CubeRecoContext *cntxt, Pix *img)
Pix * Line(int line, Box **line_box)