Skip to content

Commit 062298e

Browse files
committed
fix(OCR): erode and dilate function
1 parent 633a8f1 commit 062298e

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/lib_ccx/ocr.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -696,47 +696,31 @@ char *ocr_bitmap(void *arg, png_color *palette, png_byte *alpha, unsigned char *
696696
return text_out;
697697
}
698698

699-
void erode(png_color *palette, png_byte *alpha, uint8_t *bitmap, int w, int h, int nb_color)
699+
void erode(png_color *palette, png_byte *alpha, uint8_t *bitmap, int w, int h, int nb_color, int background_index)
700700
{
701-
int background_index;
702-
for (background_index = 0; background_index < nb_color; background_index++)
703-
{
704-
if (alpha[background_index])
705-
{
706-
break;
707-
}
708-
}
709701
// we will use a 2*2 kernel for the erosion
710702
for (int row = 0; row < h - 1; row++)
711703
{
712704
for (int col = 0; col < w - 1; col++)
713705
{
714-
if (alpha[bitmap[row * w + col]] || alpha[bitmap[(row + 1) * w + col]] ||
715-
alpha[bitmap[row * w + (col + 1)]] || alpha[bitmap[(row + 1) * w + (col + 1)]])
706+
if (bitmap[row * w + col] == background_index || bitmap[(row + 1) * w + col] == background_index ||
707+
bitmap[row * w + (col + 1)] == background_index || bitmap[(row + 1) * w + (col + 1)] == background_index)
716708
{
717709
bitmap[row * w + col] = background_index;
718710
}
719711
}
720712
}
721713
}
722714

723-
void dilate(png_color *palette, png_byte *alpha, uint8_t *bitmap, int w, int h, int nb_color)
715+
void dilate(png_color *palette, png_byte *alpha, uint8_t *bitmap, int w, int h, int nb_color, int foreground_index)
724716
{
725-
int foreground_index;
726-
for (foreground_index = 0; foreground_index < nb_color; foreground_index++)
727-
{
728-
if (!alpha[foreground_index])
729-
{
730-
break;
731-
}
732-
}
733717
// we will use a 2*2 kernel for the erosion
734718
for (int row = 0; row < h - 1; row++)
735719
{
736720
for (int col = 0; col < w - 1; col++)
737721
{
738-
if (!(alpha[bitmap[row * w + col]] && alpha[bitmap[(row + 1) * w + col]] &&
739-
alpha[bitmap[row * w + (col + 1)]] && alpha[bitmap[(row + 1) * w + (col + 1)]]))
722+
if ((bitmap[row * w + col] == foreground_index && bitmap[(row + 1) * w + col] == foreground_index &&
723+
bitmap[row * w + (col + 1)] == foreground_index && bitmap[(row + 1) * w + (col + 1)] == foreground_index))
740724
{
741725
bitmap[row * w + col] = foreground_index;
742726
}
@@ -767,6 +751,7 @@ static int quantize_map(png_byte *alpha, png_color *palette,
767751
*/
768752
uint32_t *mcit = NULL;
769753
struct transIntensity ti = {alpha, palette};
754+
int text_color, text_bg_color;
770755

771756
int ret = 0;
772757

@@ -833,6 +818,14 @@ static int quantize_map(png_byte *alpha, png_color *palette,
833818
max_ind = j;
834819
}
835820
}
821+
822+
// Assume second most frequent color to be text background (first is alpha channel)
823+
if (i == 1)
824+
text_bg_color = iot[max_ind];
825+
// Assume third most frequent color to be text color
826+
if (i == 2)
827+
text_color = iot[max_ind];
828+
836829
for (j = i; j > 0 && max_ind < mcit[j - 1]; j--)
837830
{
838831
mcit[j] = mcit[j - 1];
@@ -876,8 +869,8 @@ static int quantize_map(png_byte *alpha, png_color *palette,
876869
palette[iot[i]].green = palette[index].green;
877870
}
878871
}
879-
erode(palette, alpha, bitmap, w, h, nb_color);
880-
dilate(palette, alpha, bitmap, w, h, nb_color);
872+
erode(palette, alpha, bitmap, w, h, nb_color, text_bg_color);
873+
dilate(palette, alpha, bitmap, w, h, nb_color, text_color);
881874
#ifdef OCR_DEBUG
882875
ccx_common_logging.log_ftn("Colors present in quantized Image\n");
883876
for (int i = 0; i < nb_color; i++)

0 commit comments

Comments
 (0)